三、使用 ADO.NET 串連到資料來源
在 ADO.NET 中,可以使用 Connection 對象來串連到指定的資料來源。若要串連到 Microsoft SQL Server 7.0 版或更高版本,請使用 SQL Server .NET Framework 資料提供者的 SqlConnection 對象。若要使用用於 SQL Server 的 OLE DB 提供者 (SQLOLEDB) 串連到 OLE DB 資料來源或者串連到 Microsoft SQL Server 6.x 版或較早版本,請使用 OLE DB .NET Framework 資料提供者的 OleDbConnection 對象。若要串連到 ODBC 資料來源,請使用 ODBC .NET Framework 資料提供者的 OdbcConnection 對象。若要串連到 Oracle 資料來源,請使用 Oracle .NET Framework 資料提供者的 OracleConnection 對象。
1、 使用 ADO.NET 串連到 SQL Server
SQL Server .NET Framework 資料提供者使用 SqlConnection 對象提供與 Microsoft SQL Server 7.0 版或更高版本的串連。
SQL Server .NET Framework 資料提供者支援類似於 OLE DB (ADO) 連接字串格式的連接字串格式。有關有效字串格式名稱和值,請參見附表1
以下程式碼範例示範如何建立和開啟與 SQL Server(版本 7.0 或更高版本)資料庫的串連。
[Visual Basic]
Dim myConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")
myConn.Open()
[C#]
SqlConnection nwindConn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI;" +
"Initial Catalog=northwind");
nwindConn.Open();
建議使用完 Connection 後始終將其關閉。這可以使用 Connection 對象的 Close 或 Dispose 方法來實現。
整合式安全性和 ASP.NET
SQL Server 整合式安全性(也稱為受信任的串連)是串連到 SQL Server 的最安全的方法,因為它不在連接字串中公開使用者標識和密碼。建議使用該方法對串連進行身分識別驗證。整合式安全性使用正在執行的進程的當前安全標識或標記。對於傳統型應用程式,安全標識或標記通常是當前登入的使用者的標識。
ASP.NET 應用程式的安全標識可設定為幾個不同的選項之一。若要更好地瞭解使用整合式安全性串連到 SQL Server 時 ASP.NET 應用程式所使用的安全標識,請參見我寫得Asp.net中進行安全的 ADO.NET 編碼系列或者參考msdn
Name
Default
Description
名稱
預設值
說明
Application Name
The name of the application, or '.Net SqlClient Data Provider' if no application name is provided.
應用程式名稱
應用程式的名稱,或者“.Net SqlClient Data Provider”(如果不提供應用程式名稱)。
AttachDBFilename
-or-
extended properties
-or-
Initial File Name
The name of the primary file, including the full path name, of an attachable database.
The database name must be specified with the keyword 'database'.
AttachDBFilename
- 或 -
擴充屬性
- 或 -
初始檔案名稱
可串連資料庫的主檔案的名稱,包括完整的路徑名。
必須使用關鍵字“database”來指定資料庫的名稱。
Connect Timeout
-or-
Connection Timeout
15
The length of time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.
連線逾時設定
- 或 -
連線逾時
15
在終止嘗試併產生錯誤之前,等待與伺服器的串連的時間長度(以秒為單位)。
Current Language
The SQL Server Language record name.
當前語言
SQL Server 語言記錄名稱。
Data Source
-or-
Server
-or-
Address
-or-
Addr
-or-
Network Address
The name or network address of the instance of SQL Server to which to connect.
資料來源
- 或 -
伺服器
- 或 -
地址
- 或 -
Addr
- 或 -
網路地址
要串連的 SQL Server 執行個體的名稱或網路地址。
Encrypt
'false'
When true, SQL Server uses SSL encryption for all data sent between the client and server if the server has a certificate installed. Recognized values are true, false, yes, and no.
加密
'false'
當該值為 true 時,如果伺服器端安裝了認證,則 SQL Server 將對所有在用戶端和伺服器之間傳送的資料使用 SSL 加密。可識別的值為 true、false、yes 和 no。
Initial Catalog
-or-
Database
The name of the database.
初始目錄
- 或 -
資料庫
資料庫的名稱。
Integrated Security
-or-
Trusted_Connection
'false'
When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.
Recognized values are true, false, yes, no, and sspi (strongly recommended), which is equivalent to true.
整合式安全性
- 或 -
Trusted_Connection
'false'
當為 false 時,將在串連中指定使用者識別碼 和密碼。當為 true 時,將使用當前的 Windows 帳戶憑據進行身分識別驗證。
可識別的值為 true、false、yes、no 以及與 true 等效的 sspi(強烈推薦)。
Network Library
-or-
Net
'dbmssocn'
The network library used to establish a connection to an instance of SQL Server. Supported values include dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (Apple Talk), dbmsgnet (VIA), dbmslpcn (Shared Memory) and dbmsspxn (IPX/SPX), and dbmssocn (TCP/IP).
The corresponding network DLL must be installed on the system to which you connect. If you do not specify a network and you use a local server (for example, "." or "(local)"), shared memory is used.
網路程式庫
Packet Size
8192
Size in bytes of the network packets used to communicate with an instance of SQL Server.
資料包大小
8192
用來與 SQL Server 的執行個體進行通訊的網路資料包的大小,以位元組為單位。
Password
-or-
Pwd
The password for the SQL Server account logging on (Not recommended. To maintain a high level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).
密碼
- 或 -
Pwd
SQL Server 帳戶登入的密碼(建議不要使用。為了維護最進階別的安全性,強烈建議改用 Integrated Security 或 Trusted_Connection 關鍵字)。
Persist Security Info
'false'
When set to false or no (strongly recommended), security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state. Resetting the connection string resets all connection string values including the password. Recognized values are true, false, yes, and no.
持續安全資訊
'false'
當該值設定為 false 或 no(強烈推薦)時,如果串連是開啟的或者一直處於開啟狀態,那麼安全敏感資訊(如密碼)將不會作為串連的一部分返回。重設連接字串將重設包括密碼在內的所有連接字串值。可識別的值為 true、false、yes 和 no。
User ID
The SQL Server login account (Not recommended. To maintain a high level of security, it is strongly recommended that you use the Integrated Security or Trusted_Connection keyword instead.).
使用者識別碼
SQL Server 登入帳戶(建議不要使用。為了維護最進階別的安全性,強烈建議改用 Integrated Security 或 Trusted_Connection 關鍵字)。
Workstation ID
the local computer name
The name of the workstation connecting to SQL Server.
工作站 ID
本機電腦名稱
串連到 SQL Server 的工作站的名稱。
Name
Default
Description
名稱
預設值
說明
Connection Lifetime
0
When a connection is returned to the pool, its creation time is compared with the current time, and the connection is destroyed if that time span (in seconds) exceeds the value specified by Connection Lifetime. This is useful in clustered configurations to force load balancing between a running server and a server just brought online.
A value of zero (0) causes pooled connections to have the maximum connection timeout.
串連生存期
0
當串連被返回到池時,將其建立時間與目前時間作比較,如果時間長度(以秒為單位)超出了由 Connection Lifetime 指定的值,該串連就會被銷毀。這在聚集配置中很有用(用於強制執行運行中的伺服器和剛置於聯機狀態的伺服器之間的Server Load Balancer)。
零 (0) 值將使池串連具有最大的連線逾時。
Connection Reset
'true'
Determines whether the database connection is reset when being drawn from the pool. For Microsoft SQL Server version 7.0, setting to false avoids making an additional server round trip when obtaining a connection, but you must be aware that the connection state, such as database context, is not being reset.
串連重設
'true'
確定從池中提取資料庫連接時是否重設資料庫連接。對於 Microsoft SQL Server 7.0 版,設定為 false 可避免擷取串連時再有一次額外的伺服器往返行程,但須注意此時並未重設串連狀態(如資料庫上下文)。
Enlist
'true'
When true, the pooler automatically enlists the connection in the creation thread's current transaction context. Recognized values are true, false, yes, and no.
登記
'true'
當該值為 true 時,池程式在建立線程的當前事務上下文中自動登記串連。可識別的值為 true、false、yes 和 no。
Max Pool Size
100
The maximum number of connections allowed in the pool.
最大池大小
100
池中允許的最大串連數。
Min Pool Size
0
The minimum number of connections allowed in the pool.
最小池大小
0
池中允許的最小串連數。
Pooling
'true'
When true, the SQLConnection object is drawn from the appropriate pool, or if necessary, is created and added to the appropriate pool. Recognized values are true, false, yes, and no.
池
'true'
當該值為 true 時,系統將從相應池中提取 SQLConnection 對象,或在必要時建立該對象並將其添加到相應池中。可識別的值為 true、false、yes 和 no。