建站学 - 轻松建站从此开始!

建站学-个人建站指南,网页制作,网站设计,网站制作教程

当前位置: 建站学 > 数据库 > MSSQL Server教程 >

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

时间:2012-01-11 20:15来源: 作者: 点击:
  一、.NET Framework Data Provider for SQL Server 类型:.NET Framework类库 使用:System.Data.SqlClient.SqlConnection 厂商:Microsoft 标准安全语句   Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassw

 

一、.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版,别忘了用安装了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:asdqwemydbfile.mdf;Database=dbname; Trusted_Connection=Yes;

 

这里为什么还需要Database字段呢?因为如果指定的数据库文件已经被附加,那么SQL Server不能再附加它。另外,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中有效。

 

数据库反射

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.

 

Data Source=myServerAddress;Failover Partner=myMirrorServerAddress;Initial Catalog=myDataBase;Integrated 
Security=True;

 

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.

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片