MS SQL Server Database Connection String in C#

In this article, I have listed C# code snippets of connection string for MS SQL Server Database.


MS SQL Server Database Connection String in C#:

Connection String provide the required information to the driver that tells where to find the default connection information. Optionally, you are allowed to specify attribute=value pairs in the connection string to override the default values stored in the data source.

1. ODBC DSN ConnectionString

using System.Data.Odbc;
using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = 
              "Dsn=DsnName;" + 
              "Uid=UserName;" + 
              "Pwd=Secret;"; 
conn.Open();

2. ODBC – Standard Connection

using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = 
              "Driver={SQL Server};" + 
              "Server=DataBaseNamex;" + 
              "DataBase=DataBaseName;" + 
              "Uid=UserName;" + 
              "Pwd=Secret;"; 
conn.Open();

3. ODBC – Trusted Connection

using System.Data.Odbc;
var conn = new OdbcConnection();
conn.ConnectionString = 
              "Driver={SQL Server};" + 
              "Server=ServerName;" + 
              "DataBase=DataBaseName;" + 
              "Uid=;" + 
              "Pwd=;"; 
conn.Open();
var conn = new OdbcConnection();
conn.ConnectionString = 
              "Driver={SQL Server};" + 
              "Server=ServerName;" + 
              "DataBase=DataBaseName;" + 
              "Trusted_Connection=Yes;"; 
conn.Open();

4. OleDb – Standard Connection

using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString = 
              "Driver=SQLOLEDB;" + 
              "Data Source=ServerName;" + 
              "Initial Catalog=DataBaseName;" + 
              "User id=UserName;" + 
              "Password=Secret;"; 
conn.Open();

5. OleDb – Trusted Connection

using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString = 
              "Driver=SQLOLEDB;" + 
              "Data Source=ServerName;" + 
              "Initial Catalog=DataBaseName;" + 
              "Integrated Security=SSPI;"; 
conn.Open();

6. OleDb – via IP Address

using System.Data.OleDb;
var conn = new OleDbConnection();
conn.ConnectionString = 
              "Driver=SQLOLEDB;" + 
              "Network Library=DBMSSOCN;" + 
              "Data Source=xxx.xxx.xxx.xxx,1433;" + 
              "Initial Catalog=DataBaseName;" + 
              "User id=UserName;" + 
              "Password=Secret;"; 
conn.Open();

7. NET DataProvider – Standard Connection

using System.Data.SqlClient;
var conn = new SqlDbConnection();
conn.ConnectionString = 
              "Data Source=ServerName;" + 
              "Initial Catalog=DataBaseName;" + 
              "User id=UserName;" + 
              "Password=Secret;"; 
conn.Open();

8..NET DataProvider – Trusted Connection

using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString = 
              "Data Source=ServerName;" + 
              "Initial Catalog=DataBaseName;" + 
              "Integrated Security=SSPI;"; 
conn.Open();

9..NET DataProvider – IP Address

using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString = 
              "Network Library=DBMSSOCN;" + 
              "Data Source=xxx.xxx.xxx.xxx,1433;" + 
              "Initial Catalog=DataBaseName;" + 
              "User Id=UserName;" + 
              "Password=Secret;"; 
conn.Open();

– Article ends here –

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo