SQL Server连接字符串
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
连接字符串中常用的声明有:
服务器声明Data Source、Server和Addr等。
数据库声明Initial Catalog和DataBase等。
集成Windows账号的安全性声明Integrated Security和Trusted_Connection等。
使用数据库账号的安全性声明User ID和Password等。
对于访问数据库的账号来说,通常我们在一些参考资料上看到的字符串连接往往有如下写法:
复制代码代码如下:
string ConnStr = "server = localhost;
user id = sa; password = xxx; database = northwind";
对于集成Windows安全性的账号来说,其连接字符串写法一般如下:
复制代码代码如下:
string ConnStr = "server = localhost;
integrated security = sspi; database = northwind";
或string ConnStr = "server = localhost;
trusted_connection = yes; database = northwind";
使用Windows集成的安全性验证在访问数据库时具有很多优势:安全性更高、访问速度更快、减少重新设计安全架构的工作、可以硬编码连接字符串等,还是很值得使用的。
SQL Native Client ODBC Driver
标准安全连接
复制代码代码如下:Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
您是否在使用SQL Server 2005 Express?请在“Server”选项使用连接表达式“主机名称\SQLEXPRESS”。
受信的连接
复制代码代码如下:Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;
"Integrated Security=SSPI" 与"Trusted_Connection=yes" 是相同的。
连接到一个SQL Server实例
指定服务器实例的表达式和其他SQL Server的连接字符串相同。
Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase;Trusted_Connection=ye s;
指定用户名和密码
oConn.Properties("Prompt") = adPromptAlways
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;
使用MARS (multiple active result sets)
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;MARS_Conn ection=yes;
"MultipleActiveResultSets=true"与MARS_Connection=yes"是相同的。
使用 2.0作为MARS的模块。MARS不支持 1.0和 1.1。验证网络数据
Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes;Encrypt=yes;
使用附加本地数据库文件的方式连接到本地SQL Server Express实例
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=c:\asd\qwe\mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。
使用附加本地数据文件夹中的数据库文件的方式连接到本地SQL Server Express实例
Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;
Database=dbname;Trusted_Connection=Yes;
为何要使用Database参数?如果同名的数据库已经被附加,那么SQL Server将不会重新附加。
数据库镜像
Data Source=myServerAddress;Failover Partner=myMirrorServer;Initial Catalog=myDataBase;Integrated Security=True;
SQL Native Client OLE DB Provider
标准连接
Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=m yPassword;
您是否在使用SQL Server 2005 Express?请在“Server”选项使用连接表达式“主机名称\SQLEXPRESS”。
受信的连接
Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes; "Integrated Security=SSPI"与"Trusted_Connection=yes"相同
连接到SQL Server实例
指定服务器实例的表达式和其他SQL Server的连接字符串相同。
Provider=SQLNCLI;Server=myServerName\theInstanceName;Database=myDataBase;Trusted_C onnection=yes;
使用帐号和密码
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Provider=SQLNCLI;Server=myServerAddress;DataBase=myDataBase;
使用MARS (multiple active result sets)
Provider=SQLNCLI;Server=myServerAddress;Database=myDataBase;Trusted_Connection=yes; MarsConn=yes;
"MultipleActiveResultSets=true"和"MARS_Connection=yes"是相同的。