一、.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設備在一個域裡是不能被認證和登錄的。為了讓一台CE設備使用SSPI或信任連接和認證,可以使用下面的連接字符串:
Data Source = myServerAddress;Initial Catalog = myDataBase;Integrated Security = SSPI;User ID = myDomain
\myUsername;Password = myPassword;
說明一下,這條語句只能在CE設備上用。
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。
Server = myServerAddress;Database = myDataBase;Trusted_Connection = True; MultipleActiveResultSets = true;
MARS不支持ADO.NET 1.0和ADO.NET 1.1。
Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf;Database = dbname; Trusted_Connection = Yes;
這裡為什麼還需要Database字段呢?因為如果指定的數據庫文件已經被附加,那麼SQL Server不會再附加它,而使用已附加的數據庫作為默認數據庫。
Server = .\SQLExpress;AttachDbFilename = |DataDirectory|mydbfile.mdf; Database = dbname;Trusted_Connection = Yes;
用戶實例這個功能會在連接的過程中,新建一個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"是等價的。
Provider = SQLNCLI10;Server = myServerName\theInstanceName;Database = myDataBase; Trusted_Connection = yes;
oConn.Properties("Prompt") = adPromptAlways
oConn.Open "Provider = SQLNCLI10;Server = myServerAddress;DataBase = myDataBase;
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;MARS Connection = True;
Provider = SQLNCLI10;Server = myServerAddress;Database = myDataBase; Trusted_Connection = yes;Encrypt = yes;
Provider = SQLNCLI10;Server = .\SQLExpress;AttachDbFilename = c:\asd\qwe\mydbfile.mdf; Database = dbname;
Trusted_Connection = Yes;
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
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;
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;
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;
Driver = {SQL Server Native Client 10.0};Server = .\SQLExpress; AttachDbFilename = c:\asd\qwe\mydbfile.mdf;
Database = dbname;Trusted_Connection = Yes;
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
下面的語句只是一條例子,不同廠商的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語句。
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