以本地服务器(LocalHost),数据库(Northwind)为例,可以有以下一些连接方式
String str="Server=LocalHost;Integrated Security=SSPI;Database=Northwind";
Stringstr=”DataSource=LocalHost;IntegratedSecurity=SSPI;InitialCatalog=Northwind”;
String str=" Uid=sa;Pwd=***;Initial Catalog=Northwind;Data Source=LocalHost”;
注意:
1. Server和Database,Data Source和Initial Catalog配对使用的,可以互相替换
2.Integrated Security(集成安全性)
如果以Sql Server 用户身份登陆数据库此时需要提供Uid和Pwd并且将Integrated Security值设置为False,这也是它的默认值。
如果以Windows用户身份登陆数据库,必须将Integrated Security值设置为True,Yes 或 SSPI (Security Support Provider Interface,安全性支持提供者接口),并且不能出现Uid和Pwd。
所以要想使用Windows身份验证,必须在连接字符串中包括 Integrated Security 属性。如果在连接字符串中不包括 Integrated Security 属性,则默认用Sql Server 用户身份登陆。
我们建议使用 Windows 身份验证(通常称为集成安全性)连接到服务器数据库上
c. Persist Security Info(持续安全信息)
默认值为 false。
如果将该关键字设置为 true 或 yes ,将允许在打开连接后,从连接中获得涉及安全性的信息(包括用户标识和密码)。如果在建立连接时必须提供用户标识和密码,最安全的方法是在使用信息打开连接后丢弃这些信息,即将 Persist Security Info 设置为 false 或 no