Wednesday, September 3, 2008

Connecting to different Data sources using ADO.Net

Connect to a SQLServer datasource

using System.Data.SqlClient

string connectionString = "Data Source=(local); Initial Catalog=Adventureworks; " + "Integrated Security=SSPI; Persist Security Info=False";

SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

using (connection)
{
// use connection.
}

Connect to an OLE DB Data Source
using System.Data.OleDb

string connectionString = "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=Northwind; " + "Integrated Security=SSPI; Persist Security Info=False";

OleDbConnection connection = new OleDbConnection(connectionString);
connection.Open();

using (connection)
{
// use connection.

}

Connect to ODBC data source
using System.Data.Odbc

string connectionString = "Driver={Microsoft Access Driver (*.mdb)}; DBQ=c:\\nwind.mdb; " + "Trusted_Connection=yes";

OdbcConnection connection = new OdbcConnection(connectionString);
connection.Open();

using (connection)
{
//use connection

}

Connect to Oracle data source
using System.Data.OracleClient

string connectionString = " Data Source=MyOracleServer;Integrated Security=yes";

OracleConnection connection = new OracleConnection(connectionString);
connection.Open();

using (connection)
{
//use connection

.}


Note : If you don't want the data source to return security-sensitive information, i.e password, when the connection is open or has ever been in an open state, set persist Security Info to be false(default value).

No comments:

 
Google