1.連接字符串定義
我們已經知道,ADO.NET類庫為不同的外部數據源提供了一致的訪問。這些數據源可以是本地的數據文件(如excel、txt、access,甚至是SQLite),也可以是遠程的數據庫服務器(如SQL Server、MySQL、DB2、Oracle等)。而我們可以選擇一種既簡單又容易操作的標識去連接不同的數據源,這時候連接字符串就起到了作用。
連接字符串是標識ADO.NET用怎樣的方式,連接哪個數據庫的字符串。連接不同的數據庫,連接字符串的格式是不同的。在創建數據庫連接的時候需要提供正確的連接字符串才能順利進行數據庫訪問。
2.連接字符串語法格式
連接字符串的格式是使用分號分隔的鍵/值參數對列表:
keyword1=value; keyword2=value
3.例舉幾種典型連接字符串
部分資料參考可米小子,寫得很好的系列。
3.1 SQL Server連接字符串
SQL Server支持Windows身份驗證模式和混合模式兩種身份驗證模式。兩種身份驗證模式區別:windows身份驗證,不驗證sa密碼,如果windows登錄密碼不正確,無法訪問SQL Server;混合模式既可以使用windows身份驗證登錄,也可以在遠程使用sa密碼登錄。在這兩種情況下,連接字符串也是有區別的。
(1)Windows身份驗證模式下的連接字符串:
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
說明:
Data Source:需要連接的服務器。需要注意的是,如果使用的時Express版本的SQL Server需要在服務器名后加\SQLEXPRESS。例如,連接本地的SQL Server 2008 Express版本的數據庫服務器,可以寫成Data Source = (local)\SQLEXPRESS或者.\SQLEXPRESS。
Initial Catalog:默認使用的數據庫名稱
Integrate Security:使用存在的windows安全證書訪問數據庫。
(2)混合模式下的連接字符串
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
說明:
Data Source:同上所述。
Initial Catalog:同上所述。
User ID:數據庫服務器賬號。
Password:數據庫服務器密碼。
(3)一般在VS的環境下,通過添加數據庫連接,會自動生成連接字符串,右擊屬性即可查到。
3.2 Access連接字符串
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
3.3 MySQL連接字符串
Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
3.4 DB2連接字符串
Server=myAddress:myPortNumber;Database=myDataBase;UID=myUsername;PWD=myPassword;
3.5 Oracle連接字符串
Data Source=TORCL;User Id=myUsername;Password=myPassword;
string connStr = "Data Source=myServerAddress;Initial Catalog=Database.mdf;Integrated Security=True;User Instance=True";
4.2 使用ADO.NET提供的DbConnectionStringBuilder類來構造連接字符串
以SQL server為例子(需引入using System.Data.SqlClient;),SqlConnectionStringBuilder類繼承與DbConnectionStringBuilder類
SqlConnectionStringBuilder build = new SqlConnectionStringBuilder(); build.DataSource = @"(local)\SQLEXPRESS"; build.InitialCatalog = "DataBase"; build.IntegratedSecurity = true;
好處:使用動態字符串串聯根據用戶輸入生成連接字符串時,可能發生連接字符串注入式攻擊。 如果未驗證字符串並且未轉義惡意文本或字符,則攻擊者可能會訪問服務器上的敏感數據或其他資源。 例如,攻擊者可以通過提供分號並追加其他值來發起攻擊。 這樣構造使連接字符串輸入的值替換為合法的值。
5.連接字符串配置
5.1 連接字符串本質是字符串,所以可以寫在代碼中的任意位置,當需要的時候直接引用,但這樣不夠方便,不利於維護,並且每次修改都需要重新編譯代碼,性能低,一般不采取。
5.2 在配置文件中存儲連接字符串
(1)webform為例:在web.config配置文件中的<configuration/>根節點下的</connectionStrings>添加連接字符串。
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="false" targetFramework="4.0"/> </system.web> <connectionStrings> <add name="connstring" connectionString="Data Source=TERRYCHAN-PC\SQLEXPRESS;Initial Catalog=DBtest;Integrated Security=True"/> </connectionStrings> </configuration>
<?xml version="1.0"?> <configuration> <connectionStrings> <add name="connstring" connectionString="Data Source=TERRYCHAN-PC\SQLEXPRESS;Initial Catalog=DBtest;Integrated Security=True"/> </connectionStrings> </configuration>
6.SqlHelper類
本例使用webform,新建Datebase數據庫,在web.config中配置好連接字符串,引入System.Configuration命名空間。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Configuration; /// <summary> ///SqlHelper create by TerryChan 2012-04-17 /// </summary> public class SqlHelper { #region 字段 /// <summary> /// 連接字符串 /// </summary> private readonly static string connectionString = ConfigurationManager.AppSettings["connstring"]; #endregion }