![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
using MySQLDriverCS;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
// 建立數據庫連接
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
MySQLConnection DBConn;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
DBConn = new MySQLConnection(new MySQLConnectionString("localhost","MySQL","root","",3306).AsString);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
DBConn.Open();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
// 執行查詢語句
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
MySQLCommand DBComm;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
DBComm = new MySQLCommand("select Host,User from user",DBConn);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
// 讀取數據
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
MySQLDataReader DBReader = DBComm.ExecuteReaderEx();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
// 顯示數據
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
try
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372473.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372477.gif)
while (DBReader.Read())
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372487.gif)
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372477.gif)
Console.WriteLine("Host = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1));
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372461.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372544.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
finally
...{
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372477.gif)
DBReader.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372477.gif)
DBConn.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372544.gif)
}
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//關閉數據庫連接
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
DBConn.Close();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//方法2 綁定控件上
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//string sqlstr = "select * from mytable";
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//MySQLConnection conn = new MySQLConnection(new MySQLConnectionString("localhost", "dr", "root", "socoSoft", 3306).AsString);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//conn.Open();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//MySQLDataAdapter myada = new MySQLDataAdapter(sqlstr, conn);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//DataTable tempTable = new DataTable();
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//myada.Fill(tempTable);
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//DataGrid1.DataSource = tempTable.DefaultVIEw;
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017011311372408.gif)
//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