SQLServer2008连接字符串写法⼤全
⼀、.NET Framework Data Provider for SQL Server类型:.NET Framework类库
使⽤:System.Data.SqlClient.SqlConnection⼚商:Microsoft
标准安全连接
Data Source = myServerAddress;Initial Catalog = myDataBase;User Id = myUsername;Password = myPassword;
使⽤服务器名\\实例名作为连接指定SQL Server实例的数据源。如果你使⽤的是SQL Server 2008 Express版,实例名为SQLEXPRESS。
可替代的标准安全连接可替代的标准安全连接
Server = myServerAddress;Database = myDataBase;User ID = myUsername;Password = myPassword;Trusted_Connection = False;
这条连接字符串跟上⼀条效果⼀样。把这条写出来只是想说,其实很多连接字符串的关键字有多种写法。
信任连接
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;
可替代的信任连接
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True;
连接Windows CE设备的信任连接
通常⼀台Windows CE设备在⼀个域⾥是不能被认证和登录的。为了让⼀台CE设备使⽤SSPI或信任连接和认证,可以使⽤下⾯的连接字符串:
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;User ID = myDomain \\myUsername;Password = myPassword;
说明⼀下,这条语句只能在CE设备上⽤。
使⽤IP地址的连接
Data Source = 190.168.1.100,1433;Network Library = DBMSSOCN;Initial Catalog = myDataBase;User ID = myUsername;Password = myPassword;
这条语句⽤TCP/IP地址替代了命名管道。在Data Source字段最后的是使⽤的端⼝。SQL Server默认使⽤的端⼝是1433。
开启MARS功能(multiple active result sets)
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True; MultipleActiveResultSets = true;
MARS不⽀持ADO.NET 1.0和ADO.NET 1.1。
在连接到SQL Server Express实例时附加⼀个数据库⽂件
Server = .\\SQLExpress;AttachDbFilename = c:\\asd\\qwe\\mydbfile.mdf;Database = dbname; Trusted_Connection = Yes;
这⾥为什么还需要Database字段呢?因为如果指定的数据库⽂件已经被附加,那么SQL Server不会再附加它,⽽使⽤已附加的数据库作为默认数据库。
在连接到SQL Server Express实例时,从数据⽬录下附加⼀个数据库⽂件
Server = .\\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; Database = dbname;Trusted_Connection = Yes;
在本地SQL Server Express实例上使⽤⽤户实例
⽤户实例这个功能会在连接的过程中,新建⼀个SQL Server实例。该功能只有在本地SQL Server实例上,且连接时使⽤通过本地命名通道的Windows认证。这样做的好处是,可以给⼀个在本地计算机上只有相当有限的管理员权限的⽤户,新建⼀个拥有全部权限的SQL Server实例。
Data Source = .\\SQLExpress;Integrated Security = true; AttachDbFilename = |DataDirectory|\\mydb.mdf;User Instance = true;
若想使⽤⽤户实例这个功能,需要先在SQL Server将其开启。开启命令为:sp_configure 'user instances enabled', '1'取消命令为:
sp_configure 'user instances enabled', '0'注:这两条命令只在SQL Server Express中有效。
数据库镜像
如果你⽤ADO.NET或者SQL Native Client去连接⼀个数据库镜像,当这个数据库镜像进⾏故障切换的时候,你的应⽤程序可以利⽤驱动器的特性去⾃动重定向连接。当然,你必须在连接字段⾥指定初始的主服务器和数据库,以及⽤于故障切换的镜像服务器。
Data Source = myServerAddress;Failover Partner = myMirrorServerAddress;Initial Catalog = myDataBase;Integrated Security = True;
上⾯的例⼦只是介绍了⼀下如何使⽤数据库镜像,你可以将Failover Partner字段跟其他连接字符串功能组合使⽤。
异步处理
Server = myServerAddress;Database = myDataBase;Integrated Security = True;Asynchronous Processing = True;
⼆、SQL Server Native Client 10.0 OLE DB Provider类型:OLE DB Provider
使⽤:Provider=SQLNCLI10⼚商:Microsoft
标准安全连接
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;
信任连接
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;
\"Integrated Security=SSPI\" 和 \"Trusted_Connection=yes\"是等价的。
连接⼀个SQL Server实例
Provider = SQLNCLI10;Server = myServerName\heInstanceName;Database = myDataBase; Trusted_Connection = yes;
⽤户名、密码提⽰
oConn.Properties(\"Prompt\") = adPromptAlways
oConn.Open \"Provider = SQLNCLI10;Server = myServerAddress;DataBase = myDataBase;
开启MARS功能 (multiple active result sets)
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;MARS Connection = True;
加密模式
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;Encrypt = yes;
在连接到SQL Server Express实例时附加⼀个数据库⽂件
Provider = SQLNCLI10;Server = .\\SQLExpress;AttachDbFilename = c:\\asd\\qwe\\mydbfile.mdf; Database = dbname; Trusted_Connection = Yes;
在连接到SQL Server Express实例时,从数据⽬录下附加⼀个数据库⽂件
Provider = SQLNCLI10;Server = .\\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; Database = dbname;Trusted_Connection = Yes;
数据库镜像
Provider = SQLNCLI10;Data Source = myServerAddress;Failover Partner = myMirrorServerAddress;Initial Catalog = myDataBase;Integrated Security = True;
三、.NET Framework Data Provider for OLE DB类型:.NET Framework Wrapper Class Library使⽤:System.Data.OleDb.OleDbConnection⼚商:Microsoft
桥接到SQL Native Client OLE DB
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;
四、SQL Server Native Client 10.0 ODBC Driver类型:ODBC Driver
使⽤:Driver={SQL Server Native Client 10.0}⼚商:Microsoft
标准安全连接
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Uid = myUsername;Pwd = myPassword;
信任连接
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes;
连接⼀个SQL Server实例
Driver = {SQL Server Native Client 10.0};Server = myServerName\heInstanceName; Database = myDataBase;Trusted_Connection = yes;
⽤户名、密码提⽰
oConn.Properties(\"Prompt\") = adPromptAlways
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;
开启MARS功能 (multiple active result sets)
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes; MARS_Connection = yes;
加密模式
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;Encrypt = yes;
在连接到SQL Server Express实例时附加⼀个数据库⽂件
Driver = {SQL Server Native Client 10.0};Server = .\\SQLExpress; AttachDbFilename = c:\\asd\\qwe\\mydbfile.mdf; Database = dbname;Trusted_Connection = Yes;
在连接到SQL Server Express实例时,从数据⽬录下附加⼀个数据库⽂件
Driver={SQL Server Native Client 10.0};Server=.\\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
数据库镜像
Driver = {SQL Server Native Client
10.0};Server = myServerAddress;Failover_Partner = myMirrorServerAddress;Database = myDataBase; Trusted_Connection = yes;
五、.NET Framework Data Provider for ODBC类型:.NET Framework Wrapper Class Library使⽤:System.Data.Odbc.OdbcConnection⼚商:Microsoft
桥接到SQL Native Client 10.0 ODBC Driver
下⾯的语句只是⼀条例⼦,不同⼚商的ODBC驱动不同。
Driver={SQL Server Native Client 10.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
六、SQLXML 4.0 OLEDB Provider
类型:OLE DB Provider
使⽤:Provider=SQLXMLOLEDB.4.0;Data Provider=providername⼚商:Microsoft
Provider=SQLXMLOLEDB.4.0;Data Provider=SQLNCLI10;Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
七、Context Connection
类型:.NET Framework Class Library使⽤:
⼚商:Microsoft
通过当前CLR⾥的存储过程/函数来连接到“⾃⾝”。上下⽂连接可以让你,在你的代码第⼀次被调⽤的地⽅所在的上下⽂(连接)⾥,执⾏T-SQL语句。
C#
using(SqlConnection connection = new SqlConnection(\"context connection=true\")){
connection.Open(); // Use the connection}
VB.Net
Using connection as new SqlConnection(\"context connection=true\") connection.Open() ' Use the connectionEnd Using