简体   繁体   English

使用C#连接到MYSQL数据库

[英]Connection to MYSQL database using C#

I have a C# application with MYSQL database. 我有一个带有MYSQL数据库的C#应用​​程序。 I was work without any problem but suddenly a problem appear: 我的工作没有任何问题,但突然出现了问题:

A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll MySql.Data.dll中发生类型为'MySql.Data.MySqlClient.MySqlException'的第一次机会异常

I didn't change any thing in my program !!, also i checked the connection from PHP page and it was work without problem, the problem just appear in my c# application. 我没有在程序中更改任何内容!!,我也检查了PHP页面的连接,并且工作正常,问题仅出现在我的C#应用​​程序中。 In catch part, i changed it to: ee.HResult instead of "ee.message" and i got this number: -2147467259 .. !! 在捕获部分,我将其更改为:ee.HResult而不是“ ee.message”,我得到了这个数字:-2147467259 .. !! Here the code: 这里的代码:

public static string conString = "";
    public MySqlConnection objConn;
    string server = "www.******.com";
    string database = "******";
    string uid = "******";
    string password = "******";
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            conString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + "; CharSet=utf8;";
            objConn = new MySqlConnection(conString);
            objConn.Open();
            MySqlCommand cmd = new MySqlCommand("select * from sett", objConn);
            MySqlDataReader dataReader = cmd.ExecuteReader();
            dataReader.Read();
            int x = int.Parse(dataReader.GetDecimal(0).ToString());
            MessageBox.Show(x.ToString());
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }

The problem is because the "Remote MySQL" in the domain. 问题是因为域中的“远程MySQL”。 I added % mark to allow all remote connection to the database. 我添加了%标记,以允许所有远程连接到数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM