Monday, 24 September 2012

Sql Server Connectionstrings




Microsoft SQL Server

// ODBC DSN

using System.Data.Odbc;

OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
              "Dsn=DsnName;" +
              "Uid=UserName;" +
              "Pwd=Secret;";
conn.Open();



// ODBC -- Standard Connection

using System.Data.Odbc;

OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
              "Driver={SQL Server};" +
              "Server=ServerName;" +
              "DataBase=DataBaseName;" +
              "Uid=UserName;" +
              "Pwd=Secret;";
conn.Open();



// ODBC -- Trusted Connection

using System.Data.Odbc;

OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
              "Driver={SQL Server};" +
              "Server=ServerName;" +
              "DataBase=DataBaseName;" +
              "Uid=;" +
              "Pwd=;";
conn.Open();

// or

OdbcConnection conn = new OdbcConnection();
conn.ConnectionString =
              "Driver={SQL Server};" +
              "Server=ServerName;" +
              "DataBase=DataBaseName;" +
              "Trusted_Connection=Yes;";
conn.Open();



// OleDb -- Standard Connection

using System.Data.OleDb;

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



// OleDb -- Trusted Connection

using System.Data.OleDb;

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



// OleDb -- via IP Address

using System.Data.OleDb;

OleDbConnection 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();



// .NET DataProvider -- Standard Connection

using System.Data.SqlClient;

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



// .NET DataProvider -- Trusted Connection

using System.Data.SqlClient;

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



// .NET DataProvider -- via IP Address

using System.Data.SqlClient;

SqlConnection 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();




Microsoft Sql Express


// .NET Data Provider -- Default Relative Path
// Standard Connection

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "User Id=UserName;" +
     "Password=Secret;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();



// .NET Data Provider -- Default Relative Path
// Trusted Connection

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "Integrated Security=true;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();



// .NET Data Provider -- Custom Relative Path
// Standard Connection

using System.Data.SqlClient;

AppDomain.CurrentDomain.SetData(
     "DataDirectory", "C:\MyPath\");

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "User Id=UserName;" +
     "Password=Secret;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();




// .NET Data Provider -- Custom Relative Path
// Trusted Connection

using System.Data.SqlClient;

AppDomain.CurrentDomain.SetData(
     "DataDirectory", "C:\MyPath\");

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "Integrated Security=true;" +
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();




// .NET Data Provider -- Absolute Path
// Standard Connection

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "User Id=UserName;" +
     "Password=Secret;" +
     "AttachDbFilename=C:\MyPath\DataBaseName.mdf;"
conn.Open();




// .NET Data Provider -- Absolute Path
// Trusted Connection

using System.Data.SqlClient;

SqlConnection conn = new SqlConnection();
conn.ConnectionString =
     "Data Source=.\SQLExpress;" +
     "User Instance=true;" +
     "Integrated Security=true;" +
     "AttachDbFilename=C:\MyPath\DataBaseName.mdf;"
conn.Open();

Post a Comment