SQL Server 2008连接字符串写法大全

    文章来源:万象互联 更新时间:2012-3-12 19:10:10
分享:

一、.NET Framework Data Provider for SQL Server

类型:.NET Framework类库 使用:System.Data.SqlClient.SqlConnection 厂商:Microsoft

标准安全语句

 
1 <SPAN style="FONT-SIZE: 12px">Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
2 </SPAN>

使用服务器名实例名作为连接指定SQL Server实例的数据源。如果你使用的是SQL Server 2008 Express版,别忘了用安装了SQL Server 2008 Express的这台机器的计算机名,来替代服务器名SQLEXPRESS中的服务器名。

可替代的标准安全语句

 
1 <SPAN style="FONT-SIZE: 12px">Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;</SPAN>

这条连接字符串跟上一条效果一样。把这条写出来只是想说,其实很多连接字符串的关键字有多种写法。

可信连接语句

 
1 <SPAN style="FONT-SIZE: 12px">Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;</SPAN>

可替代的可信连接语句

 
1 <SPAN style="FONT-SIZE: 12px">Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;</SPAN>

连接Windows CE设备的可信连接语句

通常一台Windows CE设备在一个域里是不能被认证和登录的。为了让一台CE设备使用SSPI或可信的连接和认证,可以使用下面的连接字符串:

 
1 <SPAN style="FONT-SIZE: 12px">Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain 
2 myUsername;Password=myPassword;</SPAN>

说明一下,这条语句只能在CE设备上用。

使用IP地址的连接

 
1 <SPAN style="FONT-SIZE: 12px">Data Source=190.168.1.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User 
2 ID=myUsername;Password=myPassword;</SPAN>

这条语句用TCP/IP地址替代了命名管道。在Data Source字段最后的是使用的端口。SQL Server默认使用的端口是1433。

激活MARS功能(multiple active result sets)

 
1 <SPAN style="FONT-SIZE: 12px">Server=myServerAddress;Database=myDataBase;Trusted_Connection=True; MultipleActiveResultSets=true;</SPAN>

MARS不支持ADO.NET 1.0和ADO.NET 1.1。

在连接到SQL Server Express实例时附加一个数据库文件

 
1 <SPAN style="FONT-SIZE: 12px">Server=.SQLExpress;AttachDbFilename=c:asdqwemydbfile.mdf;Database=dbname; Trusted_Connection=Yes;</SPAN>

这里为什么还需要Database字段呢?因为如果指定的数据库文件已经被附加,那么SQL Server不能再附加它。另外,SQL Server使用附加的数据库作为连接的默认数据库。

在连接到SQL Server Express实例时,从数据目录下附加一个数据库文件

 
1 <SPAN style="FONT-SIZE: 12px">Server=.SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;</SPAN>

在本地SQL Server Express实例上使用用户实例

用户实例这个功能会在连接的过程中,新建一个SQL Server实例。该功能只有在本地SQL Server实例上,且连接时使用通过本地命名通道的Windows认证。这样做的好处是,可以给一个在本地计算机上只有相当有限的管理员权限的用户,新建一个拥有全部权限的SQL Server实例。

 
1 <SPAN style="FONT-SIZE: 12px">Data Source=.SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|mydb.mdf;User Instance=true;</SPAN>

若想使用用户实例这个功能,需要先在SQL Server将其开启。开启命令为:

sp_configure 'user instances enabled', '1'

取消命令为:

sp_configure 'user instances enabled', '0'

注:这两条命令只在SQL Server Express中有效。

数据库反射

If you connect with ADO.NET or the SQL Native Client to a database that is being mirrored, your application can take advantage of the drivers ability to automatically redirect connections when a database mirroring failover occurs. You must specify the initial principal server and database in the connection string and the failover partner server.

 

 
1 <SPAN style="FONT-SIZE: 12px">Data Source=myServerAddress;Failover Partner=myMirrorServerAddress;Initial Catalog=myDataBase;Integrated 
2 Security=True;</SPAN>

 

There is ofcourse many other ways to write the connection string using database mirroring, this is just one example pointing out the failover functionality. You can combine this with the other connection strings options available.

版权说明:本站原创文章,由万象互联SEO优化发表.
本文地址:https://www.hulian.top/zixun/post/4743.html
在线咨询
  • 在线时间
  • 8:00-21:00