Tutorial – Using Oracle Managed Data Access in ASP.NET and C#

1. Install  Oracle.ManagedDataAccess via NuGet package managernuget.PNG

2. Define your data source

Open your Web.config and add to the configuration the following:

<oracle.manageddataaccess.client>
<version number=”*”>
<dataSources>
<dataSource alias=”EXAMPLE” descriptor=”(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=YOUR_HOST_NAME)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=YOUR_SERVICE_NAME)))” />
</dataSources>
</version>
</oracle.manageddataaccess.client>

Here you can read, how to get your HOST_NAME and SERVICE_NAME.

3. Connect to your Oracle

using Oracle.ManagedDataAccess.Client;
using Oracle.ManagedDataAccess.Types;

….

public OracleConnection connectOracle()
{
OracleConnection con = null;
OracleCommand cmd = null;
OracleDataReader reader = null;
try
{
string constr = “user id=YYYYYYY;password=XXXXXXX;data source=EXAMPLE”;
con = new OracleConnection(constr);
con.Open();
cmd = new OracleCommand(“select * from SOMETABLE t”, con);
cmd.CommandType = System.Data.CommandType.Text;

reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetString(0)) //Just example
}
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace.ToString());
}
finally
{
if (reader != null && !reader.IsClosed)
reader.Close();
if (con != null && con.State == System.Data.ConnectionState.Open)
con.Close();
con.Dispose();
}
}

Fin.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s