5七月/10關閉
使用ODBC連接MySQL Database
首先,必須先安裝MySQL ODBC Driver,http://dev.mysql.com/downloads/connector/odbc/
# using Odbc libarey
using System.Data.Odbc;
# 建立ODBC connection
OdbcConnection dbconnect = new OdbcConnection();
# 設定ODBC connection字串
String dbconnstr = "Driver={MySQL ODBC 5.1 Driver};Server=127.0.0.1;Database=CSD;User=test; Password=1234;Option=3;";
dbconnect.ConnectionString = dbconnstr;
# 開啟DB連線,並執行SQL command
String sqlstr = "select pk,name from calltype limit 1";
dbconnect.Open();
OdbcCommand lv1_sqlcmd = new OdbcCommand(sqlstr, dbconnect);
# 讀取SQL command執行結果
OdbcDataReader lv1_rs = lv1_sqlcmd.ExecuteReader();
while (lv1_rs.Read()) {
String pk = lv1_rs[0].ToString();
String name = lv1_rs[1].ToString();
}
# 關閉DB連線
dbconnect.Close();