C#链接mysql 新手容易出错的问题
1.Access denied for user 'root'@'DESKTOP-AN72KEI' (using password: YES)
出現(xiàn)這個問題的原因是因為mysql的自帶用戶root理論上是不允許對外訪問的,只能本地訪問,如果需要訪問該用戶,需要修改一些mysql的配置文件,最好是使用自己新建的用戶并且賦予管理員權限。
2.No database selected
出現(xiàn)這個錯的原因是數(shù)據(jù)庫沒有找到對應的數(shù)據(jù),這里需要對結果做一個是否為空的判斷。,并且一定要注意的是連接字符串中需要指定數(shù)據(jù)庫,不然也可能會出現(xiàn)這樣的錯誤。
3.語句的參數(shù)化
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.出現(xiàn)這個錯誤的原因是指定了錯誤的端口號或者是server的地址寫錯了,不指定端口號的話是不是默認為3306還請大神指出,本人實驗了一下連接到虛擬機的時候沒有指定端口號也并沒有報錯,完整的鏈接語句:
server=192.168.60.128;port=3306;database=mysql_test;User Id=sqlAdmin;password=123,在配置文件中可以指定屬性:
providerName="MySql.Data.MySqlClient"?
7.刪除表數(shù)據(jù)的時候不可以使用表的別名,不然報出類似如下錯誤:
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
轉載于:https://www.cnblogs.com/llcdbk/p/5915621.html
總結
以上是生活随笔為你收集整理的C#链接mysql 新手容易出错的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 王长震《非财务经理的财务管理沙盘推演课程
- 下一篇: mpeg2,mpeg4,h264编码标准