如何解决“命令执行期间遇到的致命错误”?埃罗 [英] How can I solve "fatal error encountered during command execution" erro

查看:107
本文介绍了如何解决“命令执行期间遇到的致命错误”?埃罗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮助我解决这个问题吗?当我要将数据插入数据库时​​,我在命令执行期间遇到了

Can you guys please help me to solve this? When I'm going to insert data to the data base I got a "

fatal error encountered during command execution

错误。所以你可以帮我解决一下我的代码是否有任何错误吗?



我尝试了什么:



" Error. So Can You please help me on my code whether there is any mistake in it?

What I have tried:

private void btn_save_Click(object sender, EventArgs e)
{
    try
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            MySqlCommand cmd3 = new MySqlCommand();
            cmd3.Connection = con;

            cmd3.CommandText = "insert into sales-product(sales-id, product-name, qty, price, gross-total)values(@sales-id, @product-name, @qty, @price, @gross-total)";
            cmd3.Parameters.AddWithValue("@sales-id", lbl_invoice.Text);
            cmd3.Parameters.AddWithValue("@product-name", dataGridView1.Rows[i].Cells[2].Value);
            cmd3.Parameters.AddWithValue("@qty", dataGridView1.Rows[i].Cells[3].Value);
            cmd3.Parameters.AddWithValue("@price", dataGridView1.Rows[i].Cells[4].Value);
            cmd3.Parameters.AddWithValue("@gross-total", dataGridView1.Rows[i].Cells[5].Value);

            MySqlCommand cmd4 = new MySqlCommand();
            cmd4.Connection = con;

            cmd4.CommandText = "insert into sales(id, date, time, qty, gross-total)values(@id, @date, @time, @qty, @gross-total)";
            cmd4.Parameters.AddWithValue("@id", lbl_invoice.Text);
            cmd4.Parameters.AddWithValue("@date", lbl_date.Text);
            cmd4.Parameters.AddWithValue("@time", lbl_time.Text);
            cmd4.Parameters.AddWithValue("@qty", lbl_items.Text);
            cmd4.Parameters.AddWithValue("@gross-total", lbl_total.Text);

            con.Open();
            cmd3.ExecuteNonQuery();
            cmd4.ExecuteNonQuery();
            MessageBox.Show("Record Successfully Inserted !!");
            con.Close();
        }
    }

    catch (Exception ee)
    {
        MessageBox.Show(ee.Message, "Record Not Successfully Inserted !!");
    }
}

推荐答案

也许你应该一次做一个命令......并且在做第二次之前检查第一次的结果......并且可以通过不遵循正确的程序来确认是否存在违反任何参照完整性规则。



也许考虑一个交易。
Maybe you should do one command at a time ... and check the result of the first before doing the second ... and maybe confirm whether or not there are any "referential integrity" rules being violated by not following proper procedures.

Maybe consider a "transaction".


cmd3.ExecuteNonQuery();
cmd4.ExecuteNonQuery();
MessageBox.Show("Record Successfully Inserted !!");



不要这样做。您已忽略这两个命令的返回值,因此您不知道您的记录是否已成功插入。如果此代码是真实业务应用程序的一部分,那么您的公司正在前往摇滚。


Do not do this. You have ignored the return values of both commands so you have no idea whether your records were successfully inserted. If this code is part of a real business application then your company is heading for the rocks.


如果您使用自动增量 id字段,则不得包含在插入查询中。
If you are using an auto increment id field, it must not be included in the insert query.


这篇关于如何解决“命令执行期间遇到的致命错误”?埃罗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆