| 導購 | 订阅 | 在线投稿
分享
 
 
 

連接MYSQL數據庫的方法及示例

來源:互聯網  2008-06-01 01:43:02  評論

連接mysql數據庫的方法及示例

方法一:

使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL

該組件爲MYSQL爲ADO.NET訪問MYSQL數據庫設計的.NET訪問組件。

安裝完成該組件後,引用命名空間MySql.Data.MySqlClient;

使用命令行編譯時:csc /r:MySql.Data.dll test.cs

方法二:

通過ODBC訪問MYSQL數據庫

訪問前要先下載兩個組件:odbc.net和MYSQL的ODBC驅動(MySQL Connector/ODBC (MyODBC) driver)目前爲3.51版

安裝完成後,即可通過ODBC訪問MYSQL數據庫

方法三:

使用CoreLab推出的MYSQL訪問組件,面向.NET

安裝完成後,引用命名空間:CoreLab.MySql;

使用命令編譯時:csc /r:CoreLab.MySql.dll test.cs以下爲訪問MYSQL數據庫實例編譯指令:csc /r:CoreLab.MySql.dll /r:MySql.Data.dll test.csusing System;

using System.Net;

using System.Text;

using CoreLab.MySql;

using System.Data.Odbc;

using MySql.Data.MySqlClient;class ConnectMySql

{

public void Connect_CoreLab()

{

string constr = "User Id=root;Host=localhost;Database=qing;passWord=qing";

MySqlConnection mycn = new MySqlConnection(constr);

mycn.Open();

MySqlCommand mycm = new MySqlCommand("select * from shop",mycn);

MySqlDataReader msdr = mycm.ExecuteReader();

while(msdr.Read())

{

if (msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0));

}

}

msdr.Close();

mycn.Close();

}

public void Connect_Odbc()

{

//string MyConString ="DSN=MySQL;UID=root;PWD=qing";

string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +

"SERVER=localhost;" +

"DATABASE=test;" +

"UID=root;" +

"PASSWORD=qing;" +

"OPTION=3";

OdbcConnection MyConn = new OdbcConnection(MyConString);

MyConn.Open();

OdbcCommand mycm = new OdbcCommand("select * from hello",MyConn);

OdbcDataReader msdr = mycm.ExecuteReader();

while(msdr.Read())

{

if (msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0));

}

}

msdr.Close();

MyConn.Close();

}

public void Connect_Net()

{

string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing";

MySqlConnection mycn = new MySqlConnection(myConnectionString);

mycn.Open();

MySqlCommand mycm = new MySqlCommand("select * from hello",mycn);

MySqlDataReader msdr = mycm.ExecuteReader();

while(msdr.Read())

{

if (msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0));

}

}

msdr.Close();

mycn.Close();

}

public static void Main()

{

ConnectMySql ms = new ConnectMySql();

ms.Connect_CoreLab();

ms.Connect_Odbc();

Connect_Net();

}

}

連接mysql數據庫的方法及示例 方法一: 使用MYSQL推出的MySQL Connector/Net is an ADO.NET driver for MySQL 該組件爲MYSQL爲ADO.NET訪問MYSQL數據庫設計的.NET訪問組件。 安裝完成該組件後,引用命名空間MySql.Data.MySqlClient; 使用命令行編譯時:csc /r:MySql.Data.dll test.cs 方法二: 通過ODBC訪問MYSQL數據庫 訪問前要先下載兩個組件:odbc.net和MYSQL的ODBC驅動(MySQL Connector/ODBC (MyODBC) driver)目前爲3.51版 安裝完成後,即可通過ODBC訪問MYSQL數據庫 方法三: 使用CoreLab推出的MYSQL訪問組件,面向.NET 安裝完成後,引用命名空間:CoreLab.MySql; 使用命令編譯時:csc /r:CoreLab.MySql.dll test.cs以下爲訪問MYSQL數據庫實例編譯指令:csc /r:CoreLab.MySql.dll /r:MySql.Data.dll test.csusing System; using System.Net; using System.Text; using CoreLab.MySql; using System.Data.Odbc; using MySql.Data.MySqlClient;class ConnectMySql { public void Connect_CoreLab() { string constr = "User Id=root;Host=localhost;Database=qing;passWord=qing"; MySqlConnection mycn = new MySqlConnection(constr); mycn.Open(); MySqlCommand mycm = new MySqlCommand("select * from shop",mycn); MySqlDataReader msdr = mycm.ExecuteReader(); while(msdr.Read()) { if (msdr.HasRows) { Console.WriteLine(msdr.GetString(0)); } } msdr.Close(); mycn.Close(); } public void Connect_Odbc() { //string MyConString ="DSN=MySQL;UID=root;PWD=qing"; string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=test;" + "UID=root;" + "PASSWORD=qing;" + "OPTION=3"; OdbcConnection MyConn = new OdbcConnection(MyConString); MyConn.Open(); OdbcCommand mycm = new OdbcCommand("select * from hello",MyConn); OdbcDataReader msdr = mycm.ExecuteReader(); while(msdr.Read()) { if (msdr.HasRows) { Console.WriteLine(msdr.GetString(0)); } } msdr.Close(); MyConn.Close(); } public void Connect_Net() { string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing"; MySqlConnection mycn = new MySqlConnection(myConnectionString); mycn.Open(); MySqlCommand mycm = new MySqlCommand("select * from hello",mycn); MySqlDataReader msdr = mycm.ExecuteReader(); while(msdr.Read()) { if (msdr.HasRows) { Console.WriteLine(msdr.GetString(0)); } } msdr.Close(); mycn.Close(); } public static void Main() { ConnectMySql ms = new ConnectMySql(); ms.Connect_CoreLab(); ms.Connect_Odbc(); Connect_Net(); } }
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有