一、.NET Framework Data Provider for SQL Server 1.标准安全连接
Data Source = myServerAddress;Initial Catalog = myDataBase;User Id = myUsername;Password = myPassword;
使用服务器名\实例名作为连接指定SQL Server实例的数据源。如果你使用的是SQL Server 2008 Express版,实例名为SQLEXPRESS。 2.可替代的标准安全连接
Server = myServerAddress;Database = myDataBase;User ID = myUsername;Password = myPassword;Trusted_Connection = False;
这条连接字符串跟上一条效果一样。把这条写出来只是想说,其实很多连接字符串的关键字有多种写法。 3.信任连接
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;
可替代的信任连接
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True;
4.连接Windows CE设备的信任连接 通常一台Windows CE设备在一个域里是不能被认证和登录的。为了让一台CE设备使用SSPI或信任连接和认证,可以使用下面的连接字符串:
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;User ID = myDomain
\myUsername;Password = myPassword; 说明一下,这条语句只能在CE设备上用。 5.使用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。 6.开启MARS功能(multiple active result sets)
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True; MultipleActiveResultSets = true;
MARS不支持ADO.NET 1.0和ADO.NET 1.1。 7.在连接到SQL Server Express实例时附加一个数据库文件
Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf;Database = dbname; Trusted_Connection = Yes;
这里为什么还需要Database字段呢?因为如果指定的数据库文件已经被附加,那么SQL Server不会再附加它,而使用已附加的数据库作为默认数据库。
8.在连接到SQL Server Express实例时,从数据目录下附加一个数据库文件
Server = .\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; Database = dbname;Trusted_Connection = Yes;
9.在本地SQL Server Express实例上使用用户实例
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中有效。
10.数据库镜像 Data Source = myServerAddress;Failover Partner = myMirrorServerAddress;Initial Catalog = myDataBase;Integrated Security = True; 上面的例子只是介绍了一下如何使用数据库镜像,你可以将Failover Partner字段跟其他连接字符串功能组合使用。 11.异步处理
Server = myServerAddress;Database = myDataBase;Integrated Security = True;Asynchronous Processing = True;
二、SQL Server Native Client 10.0 OLE DB Provider
类型:OLE DB Provider
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;
2.信任连接
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;
"Integrated Security=SSPI" 和 "Trusted_Connection=yes"是等价的。 3.连接一个SQL Server实例
Provider = SQLNCLI10;Server = myServerName\theInstanceName;Database = myDataBase; Trusted_Connection = yes;
4.用户名、密码提示
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Provider = SQLNCLI10;Server = myServerAddress;DataBase = myDataBase; 5.开启MARS功能 (multiple active result sets)
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;MARS Connection = True;
6.加密模式
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;Encrypt = yes;
7.在连接到SQL Server Express实例时附加一个数据库文件
Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf; Database = dbname;
Trusted_Connection = Yes; 8.在连接到SQL Server Express实例时,从数据目录下附加一个数据库文件
Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf;
Database = dbname;Trusted_Connection = Yes; 9.数据库镜像
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
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;
四、SQL Server Native Client 10.0 ODBC Driver
类型:ODBC Driver 1.标准安全连接
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Uid = myUsername;Pwd = myPassword;
2.信任连接
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes;
3.连接一个SQL Server实例
Driver = {SQL Server Native Client 10.0};Server = myServerName\theInstanceName;
Database = myDataBase;Trusted_Connection = yes; 4.用户名、密码提示
Conn.Properties("Prompt") = adPromptAlways
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase; 5.开启MARS功能 (multiple active result sets)
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes;
MARS_Connection = yes; 6.加密模式
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;
Trusted_Connection = yes;Encrypt = yes; 7.在连接到SQL Server Express实例时附加一个数据库文件
Driver = {SQL Server Native Client 10.0};Server = .\SQLExpress; AttachDbFilename = c:\asd\qwe\mydbfile.mdf;
Database = dbname;Trusted_Connection = Yes; 8.在连接到SQL Server Express实例时,从数据目录下附加一个数据库文件
Driver={SQL Server Native Client 10.0};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;
Database=dbname;Trusted_Connection=Yes; 9.数据库镜像
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
桥接到SQL Native Client 10.0 ODBC Driver 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=SQLNCLI10;Data Source=myServerAddress;Initial Catalog=myDataBase;User
Id=myUsername;Password=myPassword;
七、Context Connection
类型:.NET Framework Class Library
通过当前CLR里的存储过程/函数来连接到“自身”。上下文连接可以让你,在你的代码第一次被调用的地方所在的上下文(连接)里,执行T-SQL语句。
using(SqlConnection connection = new SqlConnection("context connection=true"))
Using connection as new SqlConnection("context connection=true") (责任编辑:IT) |