循环参数化查询

vjrehmav  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(322)

我一直在寻找它的解决办法已经有一段时间了,我在这里看到过类似的问题,所以那里的答案并没有解决我的问题。在我的情况下,我希望遍历datagridview值行,将其插入mysql数据库。datagridview中的行数将取决于用户。当我尝试输入2行或更多的datagridview值并尝试将其插入数据库时,只有第一行数据被成功插入。
这是我的密码:

MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = SecurityMod.dbconn();
                for (int i = 0; i < dgv_ctrl.Rows.Count; i++)
                {
                    string sql = "INSERT INTO delivery(DeliveryNumber, CreationDate, Client, Product, Price, Quantity, Total) " +
                             "VALUES(@num_Delivery, " + "@dgv_datevalue" + ", " +
                             "@name_Client" + ", " +
                             "@name_Product" + ", " +
                             "@priceof_Produt" + ", " +
                             "@quan_Product" + ", " +
                             "@price_Total" + ");";

                    cmd.Parameters.AddWithValue("@num_Delivery", num_Delivery);
                    cmd.Parameters.AddWithValue("@dgv_datevalue", DateTime.Parse(dgv_ctrl.Rows[i].Cells["Creation_Date"].Value.ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                    cmd.Parameters.AddWithValue("@name_Client", dgv_ctrl.Rows[i].Cells["Client_name"].Value);
                    cmd.Parameters.AddWithValue("@name_Product", dgv_ctrl.Rows[i].Cells["Product_name"].Value);
                    cmd.Parameters.AddWithValue("@priceof_Produt", dgv_ctrl.Rows[i].Cells["Price_ofProduct"].Value);
                    cmd.Parameters.AddWithValue("@quan_Product", dgv_ctrl.Rows[i].Cells["Quantity_ofProduct"].Value);
                    cmd.Parameters.AddWithValue("@price_Total", dgv_ctrl.Rows[i].Cells["Total_Price"].Value);
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();

错误:已定义参数'@num\u delivery'。我不太会用c语言。欢迎提出任何意见和建议。

7vux5j2d

7vux5j2d1#

移动

MySqlCommand cmd = new MySqlCommand();

在for循环中:

string sql = "INSERT INTO delivery(DeliveryNumber, CreationDate, Client, Product, Price, Quantity, Total) " +
             "VALUES(@num_Delivery, " + "@dgv_datevalue" + ", " +
             "@name_Client" + ", " +
             "@name_Product" + ", " +
             "@priceof_Produt" + ", " +
             "@quan_Product" + ", " +
             "@price_Total" + ");";
for (int i = 0; i < dgv_ctrl.Rows.Count; i++)
{
    MySqlCommand cmd = new MySqlCommand();
    cmd.Connection = SecurityMod.dbconn();
    cmd.Parameters.AddWithValue("@num_Delivery", num_Delivery);
    cmd.Parameters.AddWithValue("@dgv_datevalue", DateTime.Parse(dgv_ctrl.Rows[i].Cells["Creation_Date"].Value.ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
    cmd.Parameters.AddWithValue("@name_Client", dgv_ctrl.Rows[i].Cells["Client_name"].Value);
    cmd.Parameters.AddWithValue("@name_Product", dgv_ctrl.Rows[i].Cells["Product_name"].Value);
    cmd.Parameters.AddWithValue("@priceof_Produt", dgv_ctrl.Rows[i].Cells["Price_ofProduct"].Value);
    cmd.Parameters.AddWithValue("@quan_Product", dgv_ctrl.Rows[i].Cells["Quantity_ofProduct"].Value);
    cmd.Parameters.AddWithValue("@price_Total", dgv_ctrl.Rows[i].Cells["Total_Price"].Value);
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
}

或清除其参数集合:

string sql = "INSERT INTO delivery(DeliveryNumber, CreationDate, Client, Product, Price, Quantity, Total) " +
             "VALUES(@num_Delivery, " + "@dgv_datevalue" + ", " +
             "@name_Client" + ", " +
             "@name_Product" + ", " +
             "@priceof_Produt" + ", " +
             "@quan_Product" + ", " +
             "@price_Total" + ");";
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = SecurityMod.dbconn();
for (int i = 0; i < dgv_ctrl.Rows.Count; i++)
{
    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@num_Delivery", num_Delivery);
    cmd.Parameters.AddWithValue("@dgv_datevalue", DateTime.Parse(dgv_ctrl.Rows[i].Cells["Creation_Date"].Value.ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
    cmd.Parameters.AddWithValue("@name_Client", dgv_ctrl.Rows[i].Cells["Client_name"].Value);
    cmd.Parameters.AddWithValue("@name_Product", dgv_ctrl.Rows[i].Cells["Product_name"].Value);
    cmd.Parameters.AddWithValue("@priceof_Produt", dgv_ctrl.Rows[i].Cells["Price_ofProduct"].Value);
    cmd.Parameters.AddWithValue("@quan_Product", dgv_ctrl.Rows[i].Cells["Quantity_ofProduct"].Value);
    cmd.Parameters.AddWithValue("@price_Total", dgv_ctrl.Rows[i].Cells["Total_Price"].Value);
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
}

sql字符串可能超出循环。

byqmnocz

byqmnocz2#

你可以这样做。应该有用。

MySqlCommand cmd = new MySqlCommand();
cmd.Connection = SecurityMod.dbconn();
for (int i = 0; i < dgv_ctrl.Rows.Count; i++)
{
    string sql = "INSERT INTO delivery(DeliveryNumber, CreationDate, Client, Product, Price, Quantity, Total) " +
             "VALUES(@num_Delivery, " + "@dgv_datevalue" + ", " +
             "@name_Client" + ", " +
             "@name_Product" + ", " +
             "@priceof_Produt" + ", " +
             "@quan_Product" + ", " +
             "@price_Total" + ");";
    cmd.Parameter.Clear();
    cmd.Parameters.AddWithValue("@num_Delivery", num_Delivery);
    cmd.Parameters.AddWithValue("@dgv_datevalue", DateTime.Parse(dgv_ctrl.Rows[i].Cells["Creation_Date"].Value.ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
    cmd.Parameters.AddWithValue("@name_Client", dgv_ctrl.Rows[i].Cells["Client_name"].Value);
    cmd.Parameters.AddWithValue("@name_Product", dgv_ctrl.Rows[i].Cells["Product_name"].Value);
    cmd.Parameters.AddWithValue("@priceof_Produt", dgv_ctrl.Rows[i].Cells["Price_ofProduct"].Value);
    cmd.Parameters.AddWithValue("@quan_Product", dgv_ctrl.Rows[i].Cells["Quantity_ofProduct"].Value);
    cmd.Parameters.AddWithValue("@price_Total", dgv_ctrl.Rows[i].Cells["Total_Price"].Value);
    cmd.CommandText = sql;
    cmd.ExecuteNonQuery();
}

相关问题