【IT168 技術】一、使用OleDbConnection對象連接OLE DB數據源
1.連接Access 資料庫
Access 2000:
「provider=Microsoft.Jet.Oledb.3.5;Data Source=Access文件路徑」
Access 2003:
「provider=Microsoft.Jet.Oledb.4.0;Data Source=Access文件路徑」
Access 2007:
「provider=Microsoft.Ace.Oledb.12.0;Data Source=Access文件路徑」
備註:Access資料庫只提供兩個連接屬性provider(數據提供程序)和data source(數據源);
Access2000\2003的文件格式是「。mdb」,Access2007的文件格式是「。accdb」;
Access的數據提供程序版本是向下兼容的,在Win7下測試使用Microsoft.Jet.OLEDB.3.5提示「未在本地計算機上註冊「Microsoft.Jet.OLEDB.3.5」提供程序。」,改用Microsoft.Jet.OLEDB.4.0或者Microsoft.Ace.OLEDB12.0完全可以訪問Access2000的資料庫文件。當然也可以嘗試使用微軟提供的MDAC 來修改provider的版本。
2.連接Excel資料庫
Excel 2003:
「provider=Microsoft.Jet.OLEDB.4.0;Data Source=Access文件路徑;extended properties=excel 8.0」
Excel 2007:
「provider=Microsoft.Ace.OLEDB.12.0;Data Source=Access文件路徑;extended properties=excel 12.0」
備註:在代碼中引用工作表時,應將表名表示為「[工作表名$]」,遇到欄位為資料庫保留關鍵字時,給該欄位名加上[]以示區別,如定義select 語句時:string connStr=」select * from [login$] where username=』abc』 and [password]=』abc123』 」;
如果在數據表中用數字作為文本類型數據時,則應在數字前加單引號將默認的數值強行設定為文本類型。
3.連接SQL Server資料庫
provider=SQLOLEDB;
Data Source=伺服器名;
Initial Catalog=資料庫名;
uid=用戶;
pwd=密碼
二、使用SqlConnection對象連接SQL Server資料庫
聲明:以下連接的屬性都可以參考「SQL Server 資料庫連接字符串參數一覽表」取它的別名;除了必須設置的屬性以外還可以設置其他輔助的屬性。如Connect Timeout、Encrypt等
設置資料庫文件路徑的方法:
1.使用絕對路徑:「AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf」
2.使用伺服器相對路徑:「AttachDbFilename=」+Server.MapPath(「\\App_Data\\data.mdf」)
3.使用最簡單的相對路徑:「AttachDbFilename=|DataDirectory|\\data.mdf」
推薦使用第3種方式,「|DataDirectory|」代表ASP.NET項目裡自動創建的App_Data文件夾
1.以SQL Server驗證模式連接SQLServer
(1)以資料庫名連接方式
Server=伺服器名;
Database=資料庫名稱;
User ID=用戶名;
Password=密碼 或者(使用縮寫與別名)
Server=伺服器名;
Initial Catalog=資料庫名稱;
Uid=用戶;
Pwd=密碼
(2)以資料庫文件完整路徑連接方式
「Serve=伺服器名;AttachDbFilename=資料庫文件路徑;User ID=用戶名;Password=密碼」
示例:
Server=.\SQLEXPRESS; Database=DatabaseName;
User ID =sa; Password=abc123」 Server=.\SQLEXPRESS;
Initial Catalog =DatabaseName; Uid =sa; Pwd=abc123」
Server=(local)\SQLEXPRESS; AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf;User ID =sa; Password=abc123」
備註:密碼可以為空。
2.以Windows 驗證模式連接SQL Server
(1)以資料庫名連接方式
Server=伺服器名;
Database=資料庫名稱;
Integrated Security=SSPI
(2)以資料庫文件完整路徑連接方式
「Serve=伺服器名;AttachDbFilename=資料庫文件路徑; Integrated Security=true」
示例:
Server=伺服器名;
Database=資料庫名稱;
Integrated Security=SSPI
Server=(local)\SQLEXPRESS;
AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf; Integrated Security=true」
備註:SSPI即為true
${PageNumber}
三、使用OdbcConnection對象連接ODBC數據源
「Driver=資料庫提供程序名;Server=伺服器名; Database=資料庫名;Trusted_Connection=yes」
示例:
首先要在計算機管理à數據源à配置好相對應的數據源(選擇資料庫類型,設置資料庫文件路徑與相對應的資料庫名)
Driver= Microsoft.Jet.OLEDB.4.0;
Server=.\SQLEXPRESS;
Database=DatabaseName; Trusted_Connection=yes
四、使用OracleConnection對象連接Oracle資料庫
Data Source=Oracle8i; Integrated Security=yes
五、在ASP.NET項目中的web.config文件裡配置資料庫連接並在程序代碼中獲取連接字符串
1.在
<connectionStrings>
<add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123"
providerName="System.Data.SqlClient" />
</connectionStrings> 或者
<connectionStrings>
<add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings> 在程序代碼中獲取<connectionStrings> 標籤裡的連接字符串:
引用命名空間:
Using System.Configuration ;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
2.在標籤裡添加連接
<appSettings>
<add key="ConnectionName" value="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123" />
</appSettings> 或者
<appSettings>
<add key="ConnectionName"
value="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True" />
</appSettings> 在程序代碼中獲取<appSettings> 標籤裡的連接字符串:
引用命名空間:
Using System.Configuration ;
string connStr = ConfigurationManager.AppSettings["ConnectionName"].ToString();