NET(C#)串連各類資料庫__PHP

來源:互聯網
上載者:User

1.C#串連Access

程式碼:

 

using System.Data; using System.Data.OleDb; ..

string strConnection="Provider=Microsoft.Jet.OleDb.4.0;"; strConnection+=@"Data Source=C:BegASPNETNorthwind.mdb";

OleDbConnection objConnection=new OleDbConnection(strConnection); ..

objConnection.Open(); objConnection.Close();

解釋:

串連Access資料庫需要匯入額外的命名空間,所以有了最前面的兩條using命令,這是必不可少的!

strConnection這個變數裡存放的是串連資料庫所需要的連接字串,他指定了要使用的資料提供者和要使用的資料來源。

“Provider=Microsoft.Jet.OleDb.4.0;”是指資料提供者,這裡使用的是Microsoft Jet引擎,也就是Access中的資料引擎,asp.net就是靠這個和Access的資料庫連接的。

“Data Source=C:BegASPNETNorthwind.mdb”是指明資料來源的位置,他的標準形式是“Data Source=MyDrive:MyPathMyFile.MDB”。

PS: 1.“+=”後面的“@”符號是防止將後面字串中的“”解析為逸出字元。 2.如果要串連的資料庫檔案和當前檔案在同一個目錄下,還可以使用如下的方法串連:

 

strConnection+="Data Source="; strConnection+=MapPath("Northwind.mdb");

這樣就可以省得你寫一大堆東西了!

3.要注意連接字串中的參數之間要用分號來分隔。

“OleDbConnection objConnection=new OleDbConnection(strConnection);”這一句是利用定義好的連接字串來建立了一個連結化物件,以後對資料庫的操作我們都要和這個對象打交道。

“objConnection.Open();”這用來開啟串連。至此,與Access資料庫的串連完成。

2.C#串連SQL Server

程式碼:

 

using System.Data; using System.Data.SqlClient; ..

string strConnection="user id=sa;password=;"; strConnection+="initial catalog=Northwind;Server=YourSQLServer;"; strConnection+="Connect Timeout=30";

SqlConnection objConnection=new SqlConnection(strConnection); ..

objConnection.Open(); objConnection.Close();

解釋:

串連SQL Server資料庫的機制與串連Access的機制沒有什麼太大的區別,只是改變了Connection對象和連接字串中的不同參數。

首先,串連SQL Server使用的命名空間不是“System.Data.OleDb”,而是“System.Data.SqlClient”。

其次就是他的連接字串了,我們一個一個參數來介紹(注意:參數間用分號分隔): “user id=sa”:串連資料庫的驗證使用者名稱為sa。他還有一個別名“uid”,所以這句我們還可以寫成“uid=sa”。  “password=”:串連資料庫的驗證密碼為空白。他的別名為“pwd”,所以我們可以寫為“pwd=”。 這裡注意,你的SQL Server必須已經設定了需要使用者名稱和密碼來登入,否則不能用這樣的方式來登入。如果你的SQL Server設定為Windows登入,那麼在這裡就不需要使用“user id”和“password”這樣的方式來登入,而需要使用“Trusted_Connection=SSPI”來進行登入。 “initial catalog=Northwind”:使用的資料來源為“Northwind”這個資料庫。他的別名為“Database”,本句可以寫成“Database=Northwind”。 “Server=YourSQLServer”:使用名為“YourSQLServer”的伺服器。他的別名為“Data Source”,“Address”,“Addr”。如果使用的是本機資料庫且定義了執行個體名,則可以寫為“Server=(local)執行個體名”;如果是遠程伺服器,則將“(local)”替換為遠程伺服器的名稱或IP地址。 “Connect Timeout=30”:連線逾時時間為30秒。

在這裡,建立連線物件用的建構函式為:SqlConnection。

3.C#串連Oracle

程式碼:

 

using System.Data.OracleClient; using System.Data;

//在表單上添加一個按鈕,叫Button1,雙擊Button1,輸入以下代碼

private void Button1_Click(object sender, System.EventArgs e) { string ConnectionString="Data Source=sky;user=system;password=manager;";//寫串連串 OracleConnection conn=new OracleConnection(ConnectionString);//建立一個新串連 try {conn.Open(); OracleCommand cmd=conn.CreateCommand(); cmd.CommandText="select * from MyTable";//在這兒寫sql語句 OracleDataReader odr=cmd.ExecuteReader();//建立一個OracleDateReader對象 while(odr.Read())//讀取資料,如果odr.Read()返回為false的話,就說明到記錄集的尾部了          

odr.Close(); } catch(Exception ee) { Response.Write(ee.Message); //如果有錯誤,輸出錯誤資訊 } finally { conn.Close(); //關閉串連 } }

4.C#串連MySQL

程式碼:

 

using MySQLDriverCS;

// 建立資料庫連接 MySQLConnection DBConn; DBConn = new MySQLConnection(new MySQLConnectionString

("localhost","mysql","root","",3306).AsString); DBConn.Open();

// 執行查詢語句 MySQLCommand DBComm; DBComm = new MySQLCommand("select Host,User from user",DBConn);

// 讀取資料 MySQLDataReader DBReader = DBComm.ExecuteReaderEx();

// 顯示資料 try { while (DBReader.Read()) { Console.WriteLine("Host = and User = ",

DBReader.GetString(0),DBReader.GetString(1)); } } finally

//關閉資料庫連接 DBConn.Close();

5.C#串連IBM DB2

程式碼:

 

OleDbConnection1.Open(); //開啟資料庫連接 OleDbDataAdapter1.Fill(dataSet1,"Address"); //將得來的資料填入dataSet DataGrid1.DataBind(); //綁定資料 OleDbConnection1.Close(); //關閉串連

//增加資料庫資料 在Web Form上新增對應欄位數量個數的TextBox,及一個button,為該按鍵增加Click響應事件代碼如下:

this.OleDbInsertCommand1.CommandText = "INSERTsintosADDRESS(NAME, EMAIL, AGE, ADDRESS) VALUES ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')"; OleDbInsertCommand1.Connection.Open(); //開啟串連 OleDbInsertCommand1.ExecuteNonQuery(); //執行該SQL語句 OleDbInsertCommand1.Connection.Close(); //關閉串連

6.C#串連SyBase

程式碼: (OleDb)

 

Provider=Sybase.ASEOLEDBProvider.2; Initial Catalog=資料庫名; User ID=使用者名稱;Data Source=資料來源; Extended Properties=""; Server Name=ip地址; Network Protocol=Winsock; Server Port Address=5000;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.