首先,下載MySQL Connector/ODBC 3.51 Downloads
下載下來,安裝完成後,到控制台--〉管理工具--〉Data Source(ODBC), 雙擊,再點擊Add按鈕,
選擇MySQL ODBC 3.51 Driver
之後,彈出串連mysql參數配置視窗,如下
把相應參數填寫好之後,點Test,若串連成功,確定就配置好了。
下面是C#代碼
Code
//添加命名空間
using System.Data.Odbc;
Code
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
string strconn = "Driver={MySQL ODBC 3.51 Driver};Server=192.168.1.105;Database=mydb;User=joe; Password=ast;Option=3;";
string sql = "select title,tid from info";
OdbcConnection myConnection = new OdbcConnection(strconn);
OdbcCommand myCommand = new OdbcCommand(sql, myConnection);
myConnection.Open();
DropDownList1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DropDownList1.DataTextField = "title";
DropDownList1.DataValueField = "tid";
DropDownList1.DataBind();
myConnection.Close();
}
MySQL Connector/ODBC 3.51 (MyODBC 3.51)
remote database串連mysql
Code
Driver={MySQL ODBC 3.51 Driver};Server=data.domain.com;Database=myDataBase;User=myUsername; Password=myPassword;Option=3;
ps:在visual studio 2003環境下,MySQL Connector/ODBC 5.1,會有如下異常
"ERROR [HY000] [MySQL][ODBC 5.1 Driver]Access denied for user 'ODBC'@'192.168.1.104' (using password: YES)"
本貼測試環境:
OS : windows xp2
開發環境: visual studio 2003 asp.net 1.1
mysql database 所在伺服器: Fedora core 9 linux
mysql版本: mysql Ver 14.12 Distrib 5.0.51a, for redhat-linux-gnu (i386) using readline 5.0