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

一、.NET Framework Data Provider for SQL Server

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

标准安全连接

SQL Server 2008连接字符串写法大全{转}
Data Source = myServerAddress;Initial Catalog = myDataBase;User Id = myUsername;Password = myPassword;
SQL Server 2008连接字符串写法大全{转}

使用服务器名\实例名作为连接指定SQL Server实例的数据源。如果你使用的是SQL Server 2008 Express版,实例名为SQLEXPRESS

 

可替代的标准安全连接 

SQL Server 2008连接字符串写法大全{转}
Server = myServerAddress;Database = myDataBase;User ID = myUsername;Password = myPassword;Trusted_Connection = False;
SQL Server 2008连接字符串写法大全{转}

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

 

信任连接

SQL Server 2008连接字符串写法大全{转}
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;
SQL Server 2008连接字符串写法大全{转}

可替代的信任连接

SQL Server 2008连接字符串写法大全{转}
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True;
SQL Server 2008连接字符串写法大全{转}

 

连接Windows CE设备的信任连接

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

SQL Server 2008连接字符串写法大全{转}
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;User ID = myDomain 
\myUsername;Password = myPassword;
SQL Server 2008连接字符串写法大全{转}

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


使用IP地址的连接

SQL Server 2008连接字符串写法大全{转}
Data Source = 190.168.1.100,1433;Network Library = DBMSSOCN;Initial Catalog = myDataBase;User 
ID = myUsername;Password = myPassword;
SQL Server 2008连接字符串写法大全{转}

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


开启MARS功能(multiple active result sets)

SQL Server 2008连接字符串写法大全{转}
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True; MultipleActiveResultSets = true;
SQL Server 2008连接字符串写法大全{转}

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


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

SQL Server 2008连接字符串写法大全{转}
Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf;Database = dbname; Trusted_Connection = Yes;
SQL Server 2008连接字符串写法大全{转}

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

 

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

SQL Server 2008连接字符串写法大全{转}
Server = .\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; Database = dbname;Trusted_Connection = Yes;
SQL Server 2008连接字符串写法大全{转}

 

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

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

SQL Server 2008连接字符串写法大全{转}
Data Source = .\SQLExpress;Integrated Security = true; AttachDbFilename = |DataDirectory|\mydb.mdf;User Instance = true;
SQL Server 2008连接字符串写法大全{转}

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

sp_configure ‘user instances enabled‘, ‘1‘

取消命令为:

sp_configure ‘user instances enabled‘, ‘0‘

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

 

数据库镜像

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

SQL Server 2008连接字符串写法大全{转}
Data Source = myServerAddress;Failover Partner = myMirrorServerAddress;Initial Catalog = myDataBase;Integrated 
Security = True;
SQL Server 2008连接字符串写法大全{转}

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


异步处理

SQL Server 2008连接字符串写法大全{转}
Server = myServerAddress;Database = myDataBase;Integrated Security = True;Asynchronous Processing = True;
SQL Server 2008连接字符串写法大全{转}


二、SQL Server Native Client 10.0 OLE DB Provider

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


标准安全连接

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;
SQL Server 2008连接字符串写法大全{转}

 

信任连接

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;
SQL Server 2008连接字符串写法大全{转}

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


连接一个SQL Server实例

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = myServerName\theInstanceName;Database = myDataBase; Trusted_Connection = yes;
SQL Server 2008连接字符串写法大全{转}


用户名、密码提示

SQL Server 2008连接字符串写法大全{转}
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Provider = SQLNCLI10;Server = myServerAddress;DataBase = myDataBase;
SQL Server 2008连接字符串写法大全{转}

 

开启MARS功能 (multiple active result sets)

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;MARS Connection = True;
SQL Server 2008连接字符串写法大全{转}

 

加密模式

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;Encrypt = yes;
SQL Server 2008连接字符串写法大全{转}

 

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

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf; Database = dbname; 
Trusted_Connection = Yes;
SQL Server 2008连接字符串写法大全{转}

 

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

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; 
Database = dbname;Trusted_Connection = Yes;
SQL Server 2008连接字符串写法大全{转}

 

数据库镜像

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Data Source = myServerAddress;Failover Partner = myMirrorServerAddress;Initial 
Catalog = myDataBase;Integrated Security = True;
SQL Server 2008连接字符串写法大全{转}

 

三、.NET Framework Data Provider for OLE DB

类型:.NET Framework Wrapper Class Library
使用:System.Data.OleDb.OleDbConnection
厂商:Microsoft


桥接到SQL Native Client OLE DB

SQL Server 2008连接字符串写法大全{转}
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase;Uid = myUsername; Pwd = myPassword;
SQL Server 2008连接字符串写法大全{转}


四、SQL Server Native Client 10.0 ODBC Driver

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


标准安全连接

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Uid = myUsername;Pwd = myPassword;
SQL Server 2008连接字符串写法大全{转}

 

信任连接

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes;
SQL Server 2008连接字符串写法大全{转}

 

连接一个SQL Server实例

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 10.0};Server = myServerName\theInstanceName; 
Database = myDataBase;Trusted_Connection = yes;
SQL Server 2008连接字符串写法大全{转}


用户名、密码提示

SQL Server 2008连接字符串写法大全{转}
oConn.Properties("Prompt") = adPromptAlways
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;
SQL Server 2008连接字符串写法大全{转}

 

开启MARS功能 (multiple active result sets)

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase;Trusted_Connection = yes; 
MARS_Connection = yes;
SQL Server 2008连接字符串写法大全{转}

 

加密模式

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 10.0};Server = myServerAddress;Database = myDataBase; 
Trusted_Connection = yes;Encrypt = yes;
SQL Server 2008连接字符串写法大全{转}

 

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

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 10.0};Server = .\SQLExpress; AttachDbFilename = c:\asd\qwe\mydbfile.mdf; 
Database = dbname;Trusted_Connection = Yes;
SQL Server 2008连接字符串写法大全{转}

 

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

SQL Server 2008连接字符串写法大全{转}
Driver={SQL Server Native Client 10.0};Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; 
Database=dbname;Trusted_Connection=Yes;
SQL Server 2008连接字符串写法大全{转}

 

数据库镜像

SQL Server 2008连接字符串写法大全{转}
Driver = {SQL Server Native Client 
10.0};Server = myServerAddress;Failover_Partner = myMirrorServerAddress;Database = myDataBase; Trusted_Connection = yes;
SQL Server 2008连接字符串写法大全{转}

 

五、.NET Framework Data Provider for ODBC

类型:.NET Framework Wrapper Class Library
使用:System.Data.Odbc.OdbcConnection
厂商:Microsoft


桥接到SQL Native Client 10.0 ODBC Driver

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

SQL Server 2008连接字符串写法大全{转}
Driver={SQL Server Native Client 10.0};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
SQL Server 2008连接字符串写法大全{转}

 

六、SQLXML 4.0 OLEDB Provider

类型:OLE DB Provider
使用:Provider=SQLXMLOLEDB.4.0;Data Provider=providername
厂商:Microsoft

 

SQL Server 2008连接字符串写法大全{转}
Provider=SQLXMLOLEDB.4.0;Data Provider=SQLNCLI10;Data Source=myServerAddress;Initial Catalog=myDataBase;User 
Id=myUsername;Password=myPassword;
SQL Server 2008连接字符串写法大全{转}


七、Context Connection

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

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

SQL Server 2008连接字符串写法大全{转}
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连接字符串写法大全{转}
SQL Server 2008连接字符串写法大全{转}

SQL Server 2008连接字符串写法大全{转},布布扣,bubuko.com

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

(0)
(0)
   
举报
评论 一句话评论(0
上一篇:Windows装机注意点


下一篇:.net5 core webapi项目实战之五:管理员获取用户列表编码实现