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

时间:2023-03-08 17:07:17
SQL Server 2008连接字符串写法大全{转}

一、.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版,实例名为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:\asd\qwe\mydbfile.mdf;Database = dbname; Trusted_Connection = Yes;

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

数据库镜像

如果你用ADO.NET或者SQL Native Client去连接一个数据库镜像,当这个数据库镜像进行故障切换的时候,你的应用程序可以利用驱动器的特性去自动重定向连接。当然,你必须在连接字段里指定初始的主服务器和数据库,以及用于故障切换的镜像服务器。

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

上面的例子只是介绍了一下如何使用数据库镜像,你可以将Failover Partner字段跟其他连接字符串功能组合使用。

异步处理

Server = myServerAddress;Database = myDataBase;Integrated Security = True;Asynchronous Processing = True;

二、SQL Server Native Client 10.0 OLE DB Provider

类型:OLE DB Provider
使用:Provider=SQLNCLI10
厂商:Microsoft

标准安全连接

Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;

信任连接

Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;

"Integrated Security=SSPI" 和 "Trusted_Connection=yes"是等价的。

连接一个SQL Server实例

Provider = SQLNCLI10;Server = myServerName\theInstanceName;Database = myDataBase; Trusted_Connection = yes;

用户名、密码提示

oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Provider = SQLNCLI10;Server = myServerAddress;DataBase = myDataBase;

开启MARS功能 (multiple active result sets)

Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;MARS Connection = True;

加密模式

Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;Encrypt = yes;

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

Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf; Database = dbname; 
Trusted_Connection = Yes;

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

Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; 
Database = dbname;Trusted_Connection = Yes;

数据库镜像

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
使用:System.Data.OleDb.OleDbConnection
厂商:Microsoft

桥接到SQL Native Client OLE DB

Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;

四、SQL Server Native Client 10.0 ODBC Driver

类型:ODBC Driver
使用:Driver={SQL Server Native Client 10.0}
厂商:Microsoft

标准安全连接

Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Uid = myUsername;Pwd = myPassword;

信任连接

Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes;

连接一个SQL Server实例

Driver = {SQL Server Native Client 10.0};Server = myServerName\theInstanceName; 
Database = myDataBase;Trusted_Connection = yes;

用户名、密码提示

oConn.Properties("Prompt") = adPromptAlways
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;

开启MARS功能 (multiple active result sets)

Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes; 
MARS_Connection = yes;

加密模式

Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase; 
Trusted_Connection = yes;Encrypt = yes;

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

Driver = {SQL Server Native Client 10.0};Server = .\SQLExpress; AttachDbFilename = c:\asd\qwe\mydbfile.mdf; 
Database = dbname;Trusted_Connection = Yes;

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

Driver={SQL Server Native Client 10.0};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; 
Database=dbname;Trusted_Connection=Yes;

数据库镜像

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
使用:System.Data.Odbc.OdbcConnection
厂商:Microsoft

桥接到SQL Native Client 10.0 ODBC Driver

下面的语句只是一条例子,不同厂商的ODBC驱动不同。

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=providername
厂商:Microsoft

Provider=SQLXMLOLEDB.4.0;Data Provider=SQLNCLI10;Data Source=myServerAddress;Initial Catalog=myDataBase;User 
Id=myUsername;Password=myPassword;

七、Context Connection

类型:.NET Framework Class Library
使用: 
厂商:Microsoft

通过当前CLR里的存储过程/函数来连接到“自身”。上下文连接可以让你,在你的代码第一次被调用的地方所在的上下文(连接)里,执行T-SQL语句。

SQL Server 2008连接字符串写法大全{转}
C#

using(SqlConnection connection = new SqlConnection("context connection=true"))
{
connection.Open();
// Use the connection
} VB.Net Using connection as new SqlConnection("context connection=true")
connection.Open()
' Use the connection
End Using
SQL Server 2008连接字符串写法大全{转}