<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Oracle Tips &#187; Oracle And .Net</title>
	<atom:link href="http://www.expertsharing.com/category/oracle-and-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.expertsharing.com</link>
	<description></description>
	<pubDate>Thu, 25 Sep 2008 17:29:46 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<item>
		<title>Connect to Oracle Database from .Net</title>
		<link>http://www.expertsharing.com/2008/02/22/connect-to-oracle-database-from-net/</link>
		<comments>http://www.expertsharing.com/2008/02/22/connect-to-oracle-database-from-net/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 10:55:30 +0000</pubDate>
		<dc:creator>Praveen</dc:creator>
		
		<category><![CDATA[Oracle And .Net]]></category>

		<guid isPermaLink="false">http://www.expertsharing.com/2008/02/22/connect-to-oracle-database-from-net/</guid>
		<description><![CDATA[There exist four main methodologies to access Oracle database from a .NET application:

Microsoft&#8217;s .NET data provider for ODBC (or ODBC.NET)
Microsoft&#8217;s .NET data provider for OLEDB (or OLEDB.NET)
Microsoft&#8217;s .NET data provider for Oracle
Oracle&#8217;s data provider for .NET (or ODP.NET)

There are several ways to connect to Oracle database from within .NET. Each of those methods having its [...]]]></description>
			<content:encoded><![CDATA[<p>There exist four main methodologies to access Oracle database from a .NET application:</p>
<ul>
<li><strong>Microsoft&#8217;s .NET data provider for ODBC (or ODBC.NET)</strong></li>
<li><strong>Microsoft&#8217;s .NET data provider for OLEDB (or OLEDB.NET)</strong></li>
<li><strong>Microsoft&#8217;s .NET data provider for Oracle</strong></li>
<li><strong>Oracle&#8217;s data provider for .NET (or ODP.NET)</strong></li>
</ul>
<p>There are several ways to connect to <strong>Oracle database from within .NET</strong>. Each of those methods having its own advantages and disadvantages. But in this post we concentrate only on how to connect to Oracle database from .Net. It is necessary to have Oracle 8i, 9i or <strong>Oracle 10g client</strong> installed on your system.</p>
<p>Proper Connection descriptors are to be configured on your system to connect to Oracle. The connection descriptors for oracle lies in the file called <strong>TNSNAMES.ORA</strong> . TNS stands for <strong>Transparent Network Substrate</strong>. You can locate the tnsname.ora file in your system at the Path <strong><em>&lt;oracle_home&gt;/network/admin/tnsname.ora</em></strong>. If it is already configured and you are able to connect to Oracle 9i database thru <strong>SQL* PLUS</strong> , then there is no need to disturb this file.<br />
An example of connection descriptor in my system is</p>
<p><span id="more-51"></span></p>
<p>XE =<br />
  (DESCRIPTION =<br />
    (ADDRESS = (PROTOCOL = TCP)(HOST = LENOVO)(PORT = 1521))<br />
    (CONNECT_DATA =<br />
      (SERVER = DEDICATED)<br />
      (SERVICE_NAME = XE)<br />
    )<br />
  )</p>
<p>I have installed Oracle 10g Express edition on my system.The above configuration script shows that the Oracle database server is available at 127.0.0.1 (local host) and listening at port 1521.The service name (or SID) to connect to the server is xe. The whole description is assigned to a name XE.</p>
<p><strong><font color="#ff6600">Connecting to Oracle Using .NET Data Provider for OLEDB</font></strong><br />
This method is mostly preferred when you are trying to develop database-independent applications based on ADO.NET 1.1.<br />
The following code can be used to connect to <strong>Oracle database using .Net provider for OLEDB</strong>.<br />
<code><br />
Imports System.Data<br />
Imports System.Data.OleDb<br />
Public Class Oracle_conn<br />
Private Sub btnConn_Click(ByVal sender As<br />
System.Object, ByVal e As System.EventArgs) Handles<br />
btnConn.Click<br />
Dim conn As New OleDbConnection<br />
conn.ConnectionString = "Provider=msdaora;Data Source=xe;User Id=strinix;Password=xxxx;"</p>
<p>Try<br />
  'Catch an error when connecting to Oracle<br />
   conn.Open()<br />
  'close the connection before exiting<br />
  conn.Close()<br />
  MessageBox.Show("Connected to Oracle")<br />
Catch ex As Exception<br />
'If connection failed display an error<br />
MessageBox.Show("Unable to connect. " &amp; ex.Message)<br />
End Try<br />
End Sub<br />
End Class<br />
</code><br />
 System.Data.OleDb is the namespace used to deal .NET Data Provider for OLEDB.When we are working with <strong>OLEDB</strong> data sources, we need to connect through the OleDbConnection class. The connection string information would also be different when we deal with .NET Data Provider for OLEDB to connect to Oracle.</p>
<p><strong><font color="#ff6600">Connecting to Oracle database Using .NET Data Provider for ODBC</font></strong></p>
<p>ODBC is the slowest among all the data connector used to connect oracle. This method is used when you are trying to develop multi-platform database independent applications using ADO.NET. This method is preferable, if you want to connect to legacy systems or database systems existing on other platforms.<br />
The following is the code to connect to <strong>Oracle database using .NET data provider for ODBC</strong>:<br />
<code><br />
Imports System.Data<br />
Imports System.Data.odbc<br />
Public Class Oracle_conn<br />
Private Sub btnConn_Click(ByVal sender As<br />
System.Object, ByVal e As System.EventArgs) Handles<br />
btnConn.Click<br />
Dim conn As New OdbcConnection<br />
conn.ConnectionString = "Driver={Microsoft ODBC for Oracle};Server=xe;Uid=strinix;Pwd=xxxxx;"<br />
Try<br />
  'Catch an error when connecting to Oracle<br />
   conn.Open()<br />
  'close the connection before exiting<br />
   conn.Close()<br />
MessageBox.Show("Connected to Oracle")<br />
Catch ex As Exception<br />
'If connection failed display an error<br />
MessageBox.Show("Unable to connect. " &amp; ex.Message)<br />
End Try<br />
End Sub<br />
End Class<br />
</code><br />
System.Data.odbc namespace is used to deal with .NET Data Provider for ODBC. <strong>System.Data.OleDb</strong> is the namespace used for Oledb Connection. When we are working with ODBC data sources, we need to connect through the OdbcConnection class.</p>
<p></code><strong><font color="#ff6600">Connecting to Oracle using Microsoft&#8217;s .NET Data Provider for Oracle</font></strong></p>
<p><strong>Microsoft</strong> has finally extended its support for Oracle database in visual studio by Providing  an exclusive .Net data provider for Oracle. Microsoft has developed this driver inorder to make use of advanced and extensive features available in Oracle which were not supported by oledb and Odbc. This is the most preferred method used by Microsoft developers to connect and explore Oracle.<br />
Before using this data Provider you need to add a reference to the assembly<strong> System.Data.OracleClient</strong> as show in below picture:</p>
<p><img width="538" src="http://www.expertsharing.com/wp-content/uploads/2008/02/add-reference.GIF" alt="add-reference.GIF" height="477" /><br />
You can use the following code to connect to Oracle after making a reference<br />
Imports System.Data.OracleClient<br />
<code><br />
Public Class Oracle_conn<br />
Private Sub btnConn_Click(ByVal sender As<br />
System.Object, ByVal e As System.EventArgs) Handles<br />
btnConn.Click<br />
Dim conn As New OracleConnection<br />
conn.ConnectionString = "Data Source=xe; User Id=scott;Password=tiger;"</code><code>Try<br />
   'try connecting to oracle<br />
    conn.Open()<br />
  'close the connection before exiting<br />
   conn.Close()<br />
MessageBox.Show("Connected to Oracle")<br />
Catch ex As Exception<br />
'If connection failed display an error<br />
MessageBox.Show("Unable to connect. " &amp; ex.Message)<br />
End Try<br />
End Sub<br />
End Class</code></p>
<p><strong><font color="#ff6600"><br />
Connecting Using Oracle Data Provider for .NET (ODP.NET)</font></strong><br />
This data provider is developed by Oracle to facilitate developers connecting to <strong>Oracle databases</strong> with tight integration (along with best performance) and advanced features. This method is the best even when you are trying to access Oracle, as ODP.NET has tight integration with Oracle database. To use this method you need to download <strong>ODP.NET</strong> from web and install on your system. As specified previously you need add a reference to this assembly from visual studio as shown in the picture below:</p>
<p><img src="http://www.expertsharing.com/wp-content/uploads/2008/02/oracle-reference.PNG" alt="oracle-reference.PNG" /></p>
<p>Once  a reference has been added , you can proceed with the following code to connect to Oracle database using ODP.NET<br />
<code><br />
Imports oracle.DataAccess.Client<br />
Public Class Oracle_conn<br />
Private Sub btnConn_Click(ByVal sender As<br />
System.Object, ByVal e As System.EventArgs) Handles<br />
btnConn.Click<br />
Dim conn As New OracleConnection<br />
conn.ConnectionString = "Data Source=xe;User Id=scott;Password=tiger;"<br />
Try<br />
   'try connecting to oracle<br />
    conn.Open()<br />
  'close the connection before exiting<br />
   conn.Close()<br />
MessageBox.Show("Connected to Oracle")<br />
Catch ex As Exception<br />
'If connection failed display an error<br />
MessageBox.Show("Unable to connect. " &amp; ex.Message)<br />
End Try<br />
End Sub<br />
End Class</code><img src="http://www.expertsharing.com/wp-content/uploads/2008/02/connect-to-oracle.PNG" alt="connect-to-oracle.PNG" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.expertsharing.com/2008/02/22/connect-to-oracle-database-from-net/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
