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 = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
}
}
finally
...{
DBReader.Close();
DBConn.Close();
}
//關閉數據庫連接
DBConn.Close();
//方法2 綁定控件上
//string sqlstr = "select * from mytable";
//MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "dr", "root", "socoSoft", 3306).AsString);
//conn.Open();
//MySQLDataAdapter myada = new MySQLDataAdapter(sqlstr, conn);
//DataTable tempTable = new DataTable();
//myada.Fill(tempTable);
//DataGrid1.DataSource = tempTable.DefaultVIEw;
//DataGrid1.DataBind();
關於MySQL亂碼問題:
最終解決方案:
1、選用gb2312編碼
2、設置my.ini文件中的默認編碼
分別在[mysql]和[MySQLd]配置段中增加或修改default_charater_set=gb2312
3、創建數據庫時編碼選用gb2312
4、創建表時編碼選用gb2312,Collation選用gb2312_chines_ci(gb2312_bin沒有去試驗)
5、針對具體需要存放中文的字段設定編碼選用gb2312,Collation選用gb2312_chines_ci