oracle  - oracle - Connect to an #Oracle #Database from C#

Connecting to an Database in c# is just as easy as MySQL or SQL server, it just requires it’s own library and a different connection string.

First, you need the following Nuget Package

Install-Package Oracle.ManagedDataAccess

Then import it using:

using Oracle.ManagedDataAccess.Client;

Here’s a code example –

public static DataTable PopulateDataTable(string command)
{
var sqlConnection = @” Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=121)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=XE)));user id=system;Password=xyz123″;
var adapter = new OracleDataAdapter(command, sqlConnection) { SelectCommand = { CommandTimeout = 0 } };
var dataSet = new DataSet();
adapter.Fill(dataSet, “sql”);
return dataSet.Tables[“sql”];
}

Here, the default service is “XE”, the TNS port is 1521, the  username is system, and password is xyx123 – and the oracle server is on the same (localhost)

It’s used as follows;

var strSQL = “select * from someTable”;
var dt = PopulateDataTable(strSQL);
foreach(var dr in dt.Rows.Cast<DataRow>())
{
Console.WriteLine(dr[“someColumn”].ToString());
}

 

-  - Connect to an #Oracle #Database from C# - b - Connect to an #Oracle #Database from C#



Source link

No tags for this post.

LEAVE A REPLY

Please enter your comment!
Please enter your name here