Saturday, 6 April 2013

Microsoft SQL Server Connection Strings


Microsoft SQL Server Connection Strings

Sql Server database is more compatible database with Ado.Net. Since Sql Server and Ado.Net, both are the product of Microsoft. Basically it is not easy to remember different database connection strings in Ado.Net. In Ado.net to make connection to Sql Server we have multiple options. We have different connection string to connect to the Sql Server database. So I am sharing some connection strings to connect to the Sql Server database using different drivers.

Using ODBC

  1. // ODBC -- Standard Connection
  2. using System.Data.Odbc;
  3. OdbcConnection conn = new OdbcConnection();
  4. conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Uid=UserName; Pwd=Secret";
  5. conn.Open();
  6. // ODBC -- Trusted Connection
  7. using System.Data.Odbc;
  8. OdbcConnection conn = new OdbcConnection();
  9. conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Uid=admin; Pwd=password";
  10. conn.Open();
  11. // or
  12. OdbcConnection conn = new OdbcConnection();
  13. conn.ConnectionString = "Driver={SQL Server}; Server=ServerName; DataBase=DataBaseName; Trusted_Connection=Yes;";
  14. conn.Open();

Using OLEDB

  1. // OleDb -- Standard Connection
  2. using System.Data.OleDb;
  3. OleDbConnection conn = new OleDbConnection();
  4. conn.ConnectionString = "Driver=SQLOLEDB; Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";
  5. conn.Open();
  6. // OleDb -- Trusted Connection
  7. using System.Data.OleDb;
  8. OleDbConnection conn = new OleDbConnection();
  9. conn.ConnectionString = "Driver=SQLOLEDB; Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;";
  10. conn.Open();

Using .Net DataProvider

  1. // .NET DataProvider -- Standard Connection
  2. using System.Data.SqlClient;
  3. SqlConnection conn = new SqlDbConnection();
  4. conn.ConnectionString ="Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;";
  5. conn.Open();
  6. // .NET DataProvider -- Trusted Connection
  7. using System.Data.SqlClient;
  8. SqlConnection conn = new SqlConnection();
  9. conn.ConnectionString = "Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;";
  10. conn.Open();
 

No comments:

Post a Comment