SQL資料庫連接

來源:互聯網
上載者:User

一、使用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資料庫

 
  1. provider=SQLOLEDB;  
  2. Data Source=伺服器名;  
  3. Initial Catalog=資料庫名;  
  4. uid=使用者;  
  5. 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)以資料庫名串連方式

 
  1. Server=伺服器名;  
  2. Database=資料庫名稱;  
  3. User ID=使用者名稱;  
  4. Password=密碼 

或者(使用縮寫與別名)

 
  1. Server=伺服器名;   
  2. Initial Catalog=資料庫名稱;  
  3. Uid=使用者;  
  4. Pwd=密碼 

(2)以資料庫檔案完整路徑串連方式

“Serve=伺服器名;AttachDbFilename=資料庫檔案路徑;User ID=使用者名稱;Password=密碼”

樣本:

 
  1. Server=.\SQLEXPRESS; Database=DatabaseName; User ID =sa; Password=abc123”  
  2. Server=.\SQLEXPRESS; Initial Catalog =DatabaseName; Uid =sa; Pwd=abc123”  
  3. Server=(local)\SQLEXPRESS; AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf;
  4. User ID =sa; Password=abc123” 

備忘:密碼可以為空白。

2.以Windows 驗證模式串連SQL Server

(1)以資料庫名串連方式

 
  1. Server=伺服器名;  
  2. Database=資料庫名稱;  
  3. Integrated Security=SSPI 

(2)以資料庫檔案完整路徑串連方式

“Serve=伺服器名;AttachDbFilename=資料庫檔案路徑; Integrated Security=true”

樣本:

 
  1. Server=伺服器名;  
  2. Database=資料庫名稱;  
  3. Integrated Security=SSPI  
  4. Server=(local)\SQLEXPRESS;   
  5. AttachDbFilename=D:\\Solution1\\Web\\App_Data\\data.mdf;  
  6. Integrated Security=true” 

備忘:SSPI即為true

三、使用OdbcConnection對象串連ODBC資料來源

“Driver=資料庫提供者名;Server=伺服器名; Database=資料庫名;Trusted_Connection=yes”
樣本:

首先要在電腦管理à資料來源à配置好相對應的資料來源(選擇資料庫類型,設定資料庫檔案路徑與相對應的資料庫名)

 
  1. Driver= Microsoft.Jet.OLEDB.4.0;  
  2. Server=.\SQLEXPRESS;   
  3. Database=DatabaseName;  
  4. Trusted_Connection=yes 

四、使用OracleConnection對象串連Oracle資料庫

 
  1. Data Source=Oracle8i;  
  2. Integrated Security=yes 

五、在ASP.NET項目中的web.config檔案裡設定資料庫串連並在程式碼中擷取連接字串

1.在<connectionStrings> 標籤裡添加串連

 
  1. <connectionStrings>   
  2. <add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;
  3. User ID=sa;Password=abc123"   
  4. providerName="System.Data.SqlClient" />   
  5. </connectionStrings> 

或者

 
  1. <connectionStrings>   
  2. <add name="ConnectionName" 
  3. connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;
  4. Integrated Security=true" 
  5. providerName="System.Data.SqlClient" />   
  6. </connectionStrings> 

在程式碼中擷取<connectionStrings> 標籤裡的連接字串:

引用命名空間:

 
  1. Using System.Configuration ;  
  2. string connStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString(); 

2.在<appSettings>標籤裡添加串連

 
  1. <appSettings>  
  2. <add key="ConnectionName" 
  3. value="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123" />  
  4. </appSettings> 

或者

 
  1. <appSettings>  
  2. <add key="ConnectionName"   
  3. value="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True" />  
  4. </appSettings> 

在程式碼中擷取<appSettings> 標籤裡的連接字串:

引用命名空間:

 
  1. Using System.Configuration ;  
  2. string connStr = ConfigurationManager.AppSettings["ConnectionName"].ToString();  

原文連結:http://edu.codepub.com/2011/0516/31624.php

 

 

 

在這我要針對此問題進行講解,串連資料庫一般有三種方式

1.      server = 伺服器名;database = 資料庫名;UID = 使用者名稱;pwd = 密碼

2.      Data Source = 伺服器名;Initial Catalog = 資料庫名;User ID = 使用者名稱;pwd = 密碼

現在要介紹的正是第三種方式

大家都知道,只要建立一個網站(動態網站),總是會自動產生一個web。Config檔案,這個檔案是一個xml檔案,它用來儲存web應用程式的配置資訊,例如建立一個節儲存資料庫連接字串,這樣當ASP.NET頁面需要與資料庫進行互動時,就不需要每次進行資料庫連接,並且當資料庫遷移到另一個不同的伺服器上或整個網站進行遷移時,只修改web。Config檔案中的資料庫連接配置資訊即可,並不需要對每個頁面 進行資料庫配置資訊的修改。

3.      串連步驟:

(1)      開啟自動產生的檔案web。Config檔案,找到配置節<configuration>下的子配置節<connectionstrings/>,使用name屬性和<connectionstring/>屬性設定資料庫串連資訊,將<connectionstring/>用下面的代碼替換。

 <connectionStrings>

        <add name="heyjudeConnectionString(自訂的名字,為了以後串連。)" connectionString="Data Source=172.16.100.1(伺服器位址);Initial Catalog=heyjude(要串連的資料庫名稱); User ID=sa;Password=123.com />

</connectionStrings>

(2)      在需要串連資料庫的頁面中串連

在串連前需要用using引用命名空間using System.Data.SqlClient;

在visual studio 2005中需要引用命名空間using System.Configuration;

protected void Page_Load(object sender, EventArgs e)

    {

        string connection = ConfigurationManager.ConnectionStrings["heyjudeConnectionString"].ConnectionString;

        SqlConnection sqlstr = new SqlConnection(connection);

        sqlstr.Open();

        Response.Write("資料庫連接成功!");

        sqlstr.Close();

}

結果:

 

4.      這樣就可以了,雖然在每個頁面中還是要進行串連資料庫,但是當資料庫的地址發生改變時,我們只需要在web。Config中改變伺服器的地址即可。如果嫌給web。Config中添加代碼比較麻煩,我們可以直接在預設頁面中添加一個sqldatasource控制項即可,按照步驟進行配置即可在web。Config中自動出現相應的代碼。在其他頁面只需串連即可。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.