执行Mysql查询时在命令执行期间遇到致命错误

时间:2018-12-19 09:09:11

标签: c# mysql

在C#中执行Mysql查询时,在命令执行过程中遇到致命错误

db.cs

 public  static void openconn()
    {
        try
        {

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }



        }
        catch (MySqlException ex)
        {
            if (conn.State != ConnectionState.Open)
            {

                conn.Dispose();

            }
            else
            {
                throw;
            }


        }

    }



    public  static  DataTable selectdata(string select)
    {
        DataTable tbl = new DataTable();
        try
        {

            if (CheckForInternetConnection())
            {
                openconn();
                if (conn.State == ConnectionState.Open)
                {
                    dbcomm.CommandText = select;
                    tbl.Load(dbcomm.ExecuteReader());
                }


                return tbl;
            }
            else
            {

                return tbl;
            }

        }
        catch (MySqlException ex)
        {

            MessageBox.Show(ex.Message);
            return tbl;

        }

    }



    public async static  void runq(string sql)
    {


        try
        {

            if (conn.State == ConnectionState.Open)
            {
                dbcomm.CommandText = sql;
                dbcomm.ExecuteNonQuery();
                addActions(sql);
            }
            else
            {
                MessageBox.Show("خطأ بالاتصال بقاعدة البيانات");
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show( ex.Message);;
        }






    }

调用此函数时,会收到错误消息 在C#中执行Mysql查询时在命令执行期间遇到致命错误

   private void loadcolor()
    {
        try
        {
            if (!this.DesignMode)
            {
                db.openconn();
                DataTable tbl = db.selectdata("select color_no,color_name from color");
                cbxcolor.DataSource = tbl;
                cbxcolor.ValueMember = "color_no";
                cbxcolor.DisplayMember = "color_name";

            }
        }
        catch (Exception)
        {
   `enter code here`////
        }
    }

如何解决此问题? 该消息显示函数调用命令执行量的大约90%

1 个答案:

答案 0 :(得分:0)

尝试:

    public DataTable selectdata(string Qry)
    {
        try
        {
        var datatable = new DataTable();
        conn.Open();
        MySqlDataAdapter obj = new MySqlDataAdapter(Qry, conn);
        obj.Fill(datatable);
        conn.Close();
        return datatable;
        }
        catch (MySqlException)
        {
            return new DataTable();
        }
    }