SQL Server 2008 Connection String Samples

Developers community number 1 connection string reference
Reference
Articles
Forums
Contributors
About
Retro
Connection strings for SQL Server 2008
Providers for SQL Server 2008
Sign­in | Join
Community Forums
» .NET Framework Data Provider for SQL Server (SqlC onnection)
» SQL Server Native C lient 10.0 OLE DB Provider
» .NET Framework Data Provider for OLE DB (OleDbC onnection)
» SQL Server Native C lient 10.0 ODBC Driver
» .NET Framework Data Provider for ODBC (OdbcC onnection)
Share
S e le c t e d A r t ic le s
Find solutions and post
questions regarding connection
string related issues.
What is a connection
string?
Forum for SQL Server 2008
>>
Rules for connection
strings
» SQLXML 4.0 OLEDB Provider
All SQL Server
SqlConnection properties
» C ontext C onnection
SQL Server Data Types
Reference
. NET Fr a m e w o r k D a t a P r o v i de r fo r SQ L Se r v e r
Type: .NET Framework C lass Library
Usage: System.Data.SqlC lient.SqlC onnection
Advertise
Manufacturer: Microsoft
More info about this class library »
C ustomize string
example values »
Standard Security
Data Source = myServerAddress; Initial Catalog = myDataBase; User Id = myUsername; Password = myPassword;
Use se rve rNam e \instance Nam e as Data Source to conne ct to a spe cific SQ L Se rve r
instance .
Are you using SQ L Se rve r 2008 Ex pre ss? Don't m iss the se rve r nam e syntax
Se rve rnam e \SQ LEXPR ESS whe re you substitute Se rve rnam e with the nam e of the
open in browser customize
free license
pdfcrowd.com
com pute r whe re the SQ L Se rve r Ex pre ss installation re side s.
Standard Security alternative syntax
This conne ction string produce the sam e re sult as the pre vious one . The re ason to include it is to point out that
som e conne ction string k e ywords have m any e quivale nts.
Server = myServerAddress; Database = myDataBase; User
ID = myUsername; Password = myPassword; Trusted_Connection = False;
Trusted Connection
Data Source = myServerAddress; Initial Catalog = myDataBase; Integrated Security = SSPI;
Trusted Connection alternative syntax
This conne ction string produce the sam e re sult as the pre vious one . The re ason to include it is to point out that
som e conne ction string k e ywords have m any e quivale nts.
Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;
Connecting to an SQL Server instance
The syntax of spe cifying the se rve r instance in the value of the se rve r k e y is the sam e for all conne ction strings for
SQ L Se rve r.
Server = myServerName\theInstanceName; Database = myDataBase; Trusted_Connection = True;
Trusted Connection from a CE device
O fte n a W indows C E de vice is not authe nticate d and logge d in to a dom ain. To use SSPI or truste d conne ction /
authe ntication from a C E de vice , use this conne ction string.
Data Source = myServerAddress; Initial Catalog = myDataBase; Integrated Security = SSPI; User
ID = myDomain\myUsername; Password = myPassword;
open in browser customize
free license
pdfcrowd.com
Note that this will only work on a C E de vice .
R e ad m ore about conne cting to SQ L Se rve r from C E de vice s he re
Loading ad...
Connect via an IP address
Data Source = 190.190.200.100,1433; Network Library = DBMSSOCN; Initial Catalog = myDataBase; User
ID = myUsername; Password = myPassword;
DBMSSO C N=TC P/IP. This is how to use TC P/IP inste ad of Nam e d Pipe s. At the e nd of the
Data Source is the port to use . 1433 is the de fault port for SQ L Se rve r.
How to de fine which ne twork protocol to use
Enabling MARS (multiple active result sets)
Server = myServerAddress; Database = myDataBase; Trusted_Connection = True;
MultipleActiveResultSets = true;
Use ADO .NET for MAR S functionality. MAR S is not supporte d in ADO .NET 1.0 nor ADO .NET
1.1.
open in browser customize
free license
pdfcrowd.com
Stre am line your Data C onne ctions by Moving to MAR S, by Laure nce Morone y, De vX.com
Attach a database file on connect to a local SQL Server Express instance
Server = .\SQLExpress; AttachDbFilename = c:\asd\qwe\mydbfile.mdf; Database = dbname;
Trusted_Connection = Yes;
W hy is the Database param e te r ne e de d? If the nam e d database have alre ady be e n
attache d, SQ L Se rve r doe s not re attach it. It use s the attache d database as the de fault
for the conne ction.
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Server = .\SQLExpress; AttachDbFilename = |DataDirectory|mydbfile.mdf;
Database = dbname; Trusted_Connection = Yes;
W hy is the Database param e te r ne e de d? If the nam e d database have alre ady be e n
attache d, SQ L Se rve r doe s not re attach it. It use s the attache d database as the de fault
for the conne ction.
Using an User Instance on a local SQL Server Express instance
The Use r Instance functionality cre ate s a ne w SQ L Se rve r instance on the fly during conne ct. This work s only on a
local SQ L Se rve r instance and only whe n conne cting using windows authe ntication ove r local nam e d pipe s. The
purpose is to be able to cre ate a full rights SQ L Se rve r instance to a use r with lim ite d adm inistrative rights on the
com pute r.
Data Source = .\SQLExpress; Integrated Security = true; AttachDbFilename = |DataDirectory|\mydb.mdf; User
Instance = true;
To use the Use r Instance functionality you ne e d to e nable it on the SQ L Se rve r. This is
done by e x e cuting the following com m and: sp_configure 'use r instance s e nable d', '1'. To
disable the functionality e x e cute sp_configure 'use r instance s e nable d', '0'.
Database mirroring
If you conne ct with ADO .NET or the SQ L Native C lie nt to a database that is be ing m irrore d, your application can tak e
advantage of the drive rs ability to autom atically re dire ct conne ctions whe n a database m irroring failove r occurs. You
m ust spe cify the initial principal se rve r and database in the conne ction string and the failove r partne r se rve r.
Data Source = myServerAddress; Failover Partner = myMirrorServerAddress; Initial
Catalog = myDataBase; Integrated Security = True;
open in browser customize
free license
pdfcrowd.com
The re is ofcourse m any othe r ways to write the conne ction string using database
m irroring, this is just one e x am ple pointing out the failove r functionality. You can
com bine this with the othe r conne ction strings options available .
Asynchronous processing
A conne ction to SQ L Se rve r that allows for the issuing of async re que sts through ADO .NET obje cts.
Server = myServerAddress; Database = myDataBase; Integrated Security = True; Asynchronous
Processing = True;
SQ L Se r v e r Na t i v e C l i e nt 10. 0 O L E D B P r o v i de r
Type: OLE DB Provider
Usage: Provider=SQLNC LI10
Manufacturer: Microsoft
More info about this provider »
C ustomize string
example values »
Standard security
Provider = SQLNCLI10; Server = myServerAddress; Database = myDataBase; Uid = myUsername; Pwd = myPassword;
Are you using SQ L Se rve r 2008 Ex pre ss? Don't m iss the se rve r nam e syntax
Se rve rnam e \SQ LEXPR ESS whe re you substitute Se rve rnam e with the nam e of the
com pute r whe re the SQ L Se rve r 2008 Ex pre ss installation re side s.
Trusted connection
Provider = SQLNCLI10; Server = myServerAddress; Database = myDataBase; Trusted_Connection = yes;
Equivale nt k e y­value pair: "Inte grate d Se curity=SSPI" e quals "Truste d_C onne ction=ye s"
Connecting to an SQL Server instance
The syntax of spe cifying the se rve r instance in the value of the se rve r k e y is the sam e for all conne ction strings for
SQ L Se rve r.
Provider = SQLNCLI10; Server = myServerName\theInstanceName; Database = myDataBase;
Trusted_Connection = yes;
open in browser customize
free license
pdfcrowd.com
Prompt for username and password
This one is a bit trick y. First you ne e d to se t the conne ction obje ct's Prom pt prope rty to adProm ptAlways. The n use
the conne ction string to conne ct to the database .
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Provider = SQLNCLI10; Server = myServerAddress; DataBase = myDataBase;
Enabling MARS (multiple active result sets)
Provider = SQLNCLI10; Server = myServerAddress; Database = myDataBase; Trusted_Connection = yes; MARS
Connection = True;
Use ADO .NET for MAR S functionality. MAR S is not supporte d in ADO .NET 1.0 nor ADO .NET
1.1.
Using MAR S with SQ L Native C lie nt, by C hris Le e
Encrypt data sent over network
Provider = SQLNCLI10; Server = myServerAddress; Database = myDataBase;
Trusted_Connection = yes; Encrypt = yes;
Attach a database file on connect to a local SQL Server Express instance
Provider = SQLNCLI10; Server = .\SQLExpress; AttachDbFilename = c:\asd\qwe\mydbfile.mdf;
Database = dbname; Trusted_Connection = Yes;
W hy is the Database param e te r ne e de d? If the nam e d database have alre ady be e n
attache d, SQ L Se rve r doe s not re attach it. It use s the attache d database as the de fault
for the conne ction.
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Provider = SQLNCLI10; Server = .\SQLExpress; AttachDbFilename = |DataDirectory|mydbfile.mdf;
Database = dbname; Trusted_Connection = Yes;
W hy is the Database param e te r ne e de d? If the nam e d database have alre ady be e n
open in browser customize
free license
pdfcrowd.com
attache d, SQ L Se rve r doe s not re attach it. It use s the attache d database as the de fault
for the conne ction.
Database mirroring
If you conne ct with ADO .NET or the SQ L Native C lie nt to a database that is be ing m irrore d, your application can tak e
advantage of the drive rs ability to autom atically re dire ct conne ctions whe n a database m irroring failove r occurs. You
m ust spe cify the initial principal se rve r and database in the conne ction string and the failove r partne r se rve r.
Provider = SQLNCLI10; Data Source = myServerAddress; Failover Partner = myMirrorServerAddress; Initial
Catalog = myDataBase; Integrated Security = True;
The re is ofcourse m any othe r ways to write the conne ction string using database
m irroring, this is just one e x am ple pointing out the failove r functionality. You can
com bine this with the othe r conne ction strings options available .
. NET Fr a m e w o r k D a t a P r o v i de r fo r O L E D B
Type: .NET Framework Wrapper C lass Library
Usage: System.Data.OleDb.OleDbC onnection
Manufacturer: Microsoft
More info about this wrapper class library »
C ustomize string
example values »
Bridging to SQL Native Client OLE DB
This is just one conne ction string sam ple for the wrapping O le DbC onne ction class that calls the unde rlying O LEDB
provide r. Se e re spe ctive O LE DB provide r for m ore conne ction strings to use with this class.
Provider = SQLNCLI10; Server = myServerAddress; Database = myDataBase; Uid = myUsername; Pwd = myPassword;
SQ L Se r v e r Na t i v e C l i e nt 10. 0 O D BC D r i v e r
Type: ODBC Driver
Usage: Driver={SQL Server Native C lient 10.0}
Manufacturer: Microsoft
More info about this driver »
C ustomize string
example values »
Standard security
Driver = {SQL Server Native Client
10.0}; Server = myServerAddress; Database = myDataBase; Uid = myUsername; Pwd = myPassword;
Are you using SQ L Se rve r 2008 Ex pre ss? Don't m iss the se rve r nam e syntax
open in browser customize
free license
pdfcrowd.com
Se rve rnam e \SQ LEXPR ESS whe re you substitute Se rve rnam e with the nam e of the
com pute r whe re the SQ L Se rve r 2008 Ex pre ss installation re side s.
Trusted Connection
Driver = {SQL Server Native Client
10.0}; Server = myServerAddress; Database = myDataBase; Trusted_Connection = yes;
Equivale nt k e y­value pair: "Inte grate d Se curity=SSPI" e quals "Truste d_C onne ction=ye s"
Connecting to an SQL Server instance
The syntax of spe cifying the se rve r instance in the value of the se rve r k e y is the sam e for all conne ction strings for
SQ L Se rve r.
Driver = {SQL Server Native Client 10.0}; Server = myServerName\theInstanceName;
Database = myDataBase; Trusted_Connection = yes;
Prompt for username and password
This one is a bit trick y. First you ne e d to se t the conne ction obje ct's Prom pt prope rty to adProm ptAlways. The n use
the conne ction string to conne ct to the database .
oConn.Properties("Prompt") = adPromptAlways
Driver = {SQL Server Native Client 10.0}; Server = myServerAddress; Database = myDataBase;
Enabling MARS (multiple active result sets)
Driver = {SQL Server Native Client
10.0}; Server = myServerAddress; Database = myDataBase; Trusted_Connection = yes; MARS_Connection = yes;
Use ADO .NET for MAR S functionality. MAR S is not supporte d in ADO .NET 1.0 nor ADO .NET
1.1.
Using MAR S with SQ L Native C lie nt, by C hris Le e
Encrypt data sent over network
open in browser customize
free license
pdfcrowd.com
Driver = {SQL Server Native Client 10.0}; Server = myServerAddress; Database = myDataBase;
Trusted_Connection = yes; Encrypt = yes;
Attach a database file on connect to a local SQL Server Express instance
Driver = {SQL Server Native Client 10.0}; Server = .\SQLExpress;
AttachDbFilename = c:\asd\qwe\mydbfile.mdf; Database = dbname; Trusted_Connection = Yes;
W hy is the Database param e te r ne e de d? If the nam e d database have alre ady be e n
attache d, SQ L Se rve r doe s not re attach it. It use s the attache d database as the de fault
for the conne ction.
Attach a database file, located in the data directory, on connect to a local SQL Server Express instance
Driver = {SQL Server Native Client
10.0}; Server = .\SQLExpress; AttachDbFilename = |DataDirectory|mydbfile.mdf;
Database = dbname; Trusted_Connection = Yes;
W hy is the Database param e te r ne e de d? If the nam e d database have alre ady be e n
attache d, SQ L Se rve r doe s not re attach it. It use s the attache d database as the de fault
for the conne ction.
Database mirroring
If you conne ct with ADO .NET or the SQ L Native C lie nt to a database that is be ing m irrore d, your application can tak e
advantage of the drive rs ability to autom atically re dire ct conne ctions whe n a database m irroring failove r occurs. You
m ust spe cify the initial principal se rve r and database in the conne ction string and the failove r partne r se rve r.
Driver = {SQL Server Native Client
10.0}; Server = myServerAddress; Failover_Partner = myMirrorServerAddress; Database = myDataBase;
Trusted_Connection = yes;
The re is ofcourse m any othe r ways to write the conne ction string using database
m irroring, this is just one e x am ple pointing out the failove r functionality. You can
com bine this with the othe r conne ction strings options available .
Ple ase note if you are using TC P/IP (using the ne twork library param e te r) and database
m irroring, including port num be r in the addre ss (form e d as se rve rnam e ,portnum be r) for
booth the m ain se rve r and the failove r partne r can solve som e re porte d issue s.
open in browser customize
free license
pdfcrowd.com
. NET Fr a m e w o r k D a t a P r o v i de r fo r O D BC
Type: .NET Framework Wrapper C lass Library
Usage: System.Data.Odbc.OdbcC onnection
Manufacturer: Microsoft
More info about this wrapper class library »
C ustomize string
example values »
Bridging to SQL Native Client 10.0 ODBC Driver
This is just one conne ction string sam ple for the wrapping O dbcC onne ction class that calls the unde rlying O DBC
Drive r. Se e re spe ctive O DBC drive r for m ore conne ction strings to use with this class.
Driver = {SQL Server Native Client
10.0}; Server = myServerAddress; Database = myDataBase; Uid = myUsername; Pwd = myPassword;
SQ L XM L 4. 0 O L ED B P r o v i de r
Type: OLE DB Provider
Usage: Provider=SQLXMLOLEDB.4.0;Data
Provider=providername
Manufacturer: Microsoft
More info about this provider »
C ustomize string
example values »
Using SQL Server Native Client provider
Provider = SQLXMLOLEDB.4.0; Data Provider = SQLNCLI10; Data Source = myServerAddress; Initial
Catalog = myDataBase; User Id = myUsername; Password = myPassword;
C o nt e x t C o nne ct i o n
Type: .NET Framework C lass Library
Usage:
Manufacturer: Microsoft
More info about this class library »
C ustomize string
example values »
Context Connection
C onne cting to "se lf" from within your C LR store d prode dure /function. The conte x t conne ction le ts you e x e cute
Transact­SQ L state m e nts in the sam e conte x t (conne ction) that your code was invok e d in the first place .
C#
using(SqlConnection connection = new SqlConnection("context connection=true"))
{
open in browser customize
free license
pdfcrowd.com
connection.Open();
// Use the connection
}
VB.Net
Using connection as new SqlConnection("context connection=true")
connection.Open()
' Use the connection
End Using
W he n to use SQ L Native C lie nt?
List of all SqlC onne ction conne ction string prope rtie s
C opyright © 2011 C onnectionStrings.com
open in browser customize
free license
|
All Rights Reserved
|
Powered by C SAS
|
Send feedback, articles, requests and more connection strings here .
pdfcrowd.com
Download PDF