当前位置:Gxlcms > 数据库问题 > C#链接mysql 新手容易出错的问题

C#链接mysql 新手容易出错的问题

时间:2021-07-01 10:21:17 帮助过:2人阅读

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" 

C#链接mysql 新手容易出错的问题

标签:

人气教程排行