Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Practice Test
Tip of the Day : SQL Server Numeric Data Types
SQL Server 2008

Home > SQL Server 2008 > SqlConnection Connection String
SQL Server 2008 - SqlConnection Connection String

Standard Security (SQL Server Authentication)

Option 1 :

Data Source=yourSQLServer; Initial Catalog=yourDB; User Id=yourUserName; Password=yourPwd;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=SQLDevelopment; Initial Catalog=SQL2008; User Id=sql2008; Password=######;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Users]", theSqlServer);

Option 2 :

Server=yourServer;Database=yourDB;User ID=yourUserName;Password=yourPwd;Trusted_Connection=False;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Server=SQLDevelopment; Database=SQL2008; User ID=sql2008; Password=######; Trusted_Connection=False;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Accounts]", theSqlServer);

Trusted Connection (Windows Authentication)

Option 1 :

Data Source=yourSQLServer; Initial Catalog=yourDB; Integrated Security=SSPI;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=SQLDevelopment; Initial Catalog=SQL2008; Integrated Security=SSPI;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Emplpoyees]", theSqlServer);

Option 2 :

Server=yourSQLServer; Database=yourDB; Trusted_Connection=True;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection( "Server=SQLDevelopment; Database=SQL2008; Trusted_Connection=True");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Students]", theSqlServer);

Connecting to the Local SQL Server – Standard Security

Option 1 :

Data Source=(local); Initial Catalog=yourDB; User Id=yourUserName; Password=yourPwd;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=(local); Initial Catalog=SQL2008; User Id=sql2008; Password=######;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Companies]", theSqlServer);

Option 2 :

Server=(local); Database=yourDB; User ID=yourUserName; Password=yourPwd; Trusted_Connection=False;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Server=(local); Database=SQL2008; User ID=sql2008; Password=######;Trusted_Connection=False;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Clients]", theSqlServer);

Option 3 :

Data Source=.; Initial Catalog=yourDB; User Id=yourUserName; Password=yourPwd;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=.; Initial Catalog=SQL2008 User Id=sql2008; Password=######;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Contacts]", theSqlServer);

Option 4 :

Server=.; Database=yourDB; User ID=yourUserName; Password=yourPwd; Trusted_Connection=False;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Server=.; Database=SQL2008; User ID=sql2008; Password=######;Trusted_Connection=False;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Stores]", theSqlServer);

Connecting to the Local SQL Server – Trusted Connection

Option 1 :

Data Source=(local); Initial Catalog=yourDB; Integrated Security=SSPI;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=(local); Initial Catalog=SQL2008; Integrated Security=SSPI;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Teams]", theSqlServer);

Option 2 :

Server=(local); Database=yourDB; Trusted_Connection=True;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection( "Server=(local); Database=SQL2008; Trusted_Connection=True");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Players]", theSqlServer);

Option 3 :

Data Source=.; Initial Catalog=yourDB; Integrated Security=SSPI;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=.; Initial Catalog=SQL2008; Integrated Security=SSPI;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Scores]", theSqlServer);

Option 4 :

Server=. Database=yourDB; Trusted_Connection=True;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection( "Server=.; Database=SQL2008; Trusted_Connection=True");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Grades]", theSqlServer);

Connecting to the Default Database of the User

If the Initial Catalog or Database parameter is not supplied, the database will default to the default database set for the user.

Data Source=yourSQLServer; User Id=yourUserName; Password=yourPwd;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Data Source=SQLDevelopment; User Id=sql2008; Password=######;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Regions]", theSqlServer);

Connecting Via an IP Address

Data Source=123.456.789.012,1433; Network Library=DBMSSOCN; Initial Catalog=yourDB;
User ID=yourUserName; Password=yourPwd;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection("Server=10.0.0.1,1433; Network Library=DBMSSOCN; Initial Catalog=SQL2008; User ID=sql2008; Password=######;");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Sales]", theSqlServer);

Connecting to a SQL Server Instance

Server=yourSQLServer\yourInstanceName; Database=yourDB; Trusted_Connection=True;

SqlConnection Example :

SqlConnection theSqlServer = new SqlConnection( "Server=SQLDevelopment\SQL2008Instance; Database=SQL2008; Trusted_Connection=True");
SqlCommand theSqlCommand = new SqlCommand("SELECT * FROM [dbo].[Products]", theSqlServer);

Trusted Connection From a CE Device

Data Source=yourSQLServer; Initial Catalog=yourDB; Integrated Security=SSPI;
User ID=yourDomain\yourUserName; Password=yourPwd

Enabling MARS (Multiple Active Result Sets)

Server=yourSQLServer; Database=yourDB; Trusted_Connection=True; MultipleActiveResultSets=true;

Connecting to a Local SQL Server Express Instance

Server=.\SQLExpress; AttachDbFilename=yourMDFFile.mdf; Database=yourDB; Trusted_Connection=Yes;

Connecting to a Local SQL Server Express Instance (Database File in Data Directory)

Server=.\SQLExpress; AttachDbFilename=|DataDirectory|yourMDFFile.mdf; Database=yourDB;
Trusted_Connection=Yes;
Related Articles :