在c#asp.net中连接到mysql时出错

jhkqcmku  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(306)

我在将我的项目连接到mysql数据库时出错,所以我将下面的代码添加到我的按钮中,以获取所有行并在gridview中显示它

protected void Button1_Click(object sender, EventArgs e)
        {
            MySqlConnection myconn = new MySqlConnection("server=localhost;uid=root;password=;database=y;");
            string strSQL = "select * from welcome";
            myconn.Open();
            MySqlDataAdapter mydata = new MySqlDataAdapter(strSQL, myconn);
            MySqlCommandBuilder cBuilder = new MySqlCommandBuilder(mydata);
            System.Data.DataSet ds = new System.Data.DataSet();
            mydata.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
            myconn.Close();
        }

运行应用程序时,在myconn.open()中显示错误;

An exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll but was not handled in user code

附加信息:主机localhost不支持ssl连接。
它指向myconn.open();

u4dcyp6a

u4dcyp6a1#

改变 myconn 收件人:

MySqlConnection myconn = new MySqlConnection("server=localhost;uid=root;password=;database=y;sslmode=none");

确保您正在添加 sslmode=nonemyconn

unhi4e5o

unhi4e5o2#

最好尝试实体框架
添加ado.net实体模型,如果需要连接数据库并提供一些名称(例如:userentities)
然后,在编码中使用下面的代码
userentities context=new userentities();

protected void Button1_Click(object sender, EventArgs e)
        {
            using (var dbTransactionContext = context.Database.BeginTransaction())
            {
                //then do the operation
            }
        }

这真的很简单
或者按照以下步骤1。在web配置中添加连接字符串

<add name="UserContext" connectionString="server=localhost;User Id=root;password='';database=databasename;convert zero datetime=True" providerName="MySql.Data.MySqlClient" />

在代码中添加以下代码
专用mysqlconnection con;con=新建mysqlconnection();con.connectionstring=configurationmanager.connectionstring[“usercontext”].connectionstring;con.open();

相关问题