1.Access denied for user 'root'@'DESKTOP-AN72KEI' (using password: YES)
出現這個問題的原因是因為mysql的自帶用戶root理論上是不允許對外訪問的,只能本地訪問,如果需要訪問該用戶,需要修改一些mysql的配置文件,最好是使用自己新建的用戶並且賦予管理員權限。
2.No database selected
出現這個錯的原因是數據庫沒有找到對應的數據,這里需要對結果做一個是否為空的判斷。,並且一定要注意的是連接字符串中需要指定數據庫,不然也可能會出現這樣的錯誤。
3.語句的參數化
DEMO如下:
string conn = "server=192.168.60.128;database=mysql_test;User Id=sqlAdmin;password=123"; using (MySqlConnection mconn = new MySqlConnection(conn)) { mconn.Open(); //string sql = "SELECT t.sName FROM student t WHERE t.sid =2"; string sql = "SELECT t.sName FROM student t WHERE t.sid =?id"; MySqlCommand mcomm = new MySqlCommand(sql, mconn); mcomm.Parameters.Add("id", MySqlDbType.Int32); mcomm.Parameters["id"].Value = 2; var res = mcomm.ExecuteScalar(); if (res == null) { return null; } else { return res; } }
4.需要引入的空間:
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
5.需要的DLL:
6.Unable to connect to any of the specified MySQL hosts.出現這個錯誤的原因是指定了錯誤的端口號或者是server的地址寫錯了,不指定端口號的話是不是默認為3306還請大神指出,本人實驗了一下連接到虛擬機的時候沒有指定端口號也並沒有報錯,完整的鏈接語句:
server=192.168.60.128;port=3306;database=mysql_test;User Id=sqlAdmin;password=123,在配置文件中可以指定屬性:
providerName="MySql.Data.MySqlClient"
7.刪除表數據的時候不可以使用表的別名,不然報出類似如下錯誤:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'T WHERE T.`sid`=8' at line 1