本文介绍了需要使用参数c#从数据库中减去值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码减去了数据库中的值,当我想将它与datagridview一起使用时,请将它设置为参数而我无法使其成为
i had that code that minus value from database and when i want to use it with datagridview mus make it in parameter and i cant make it
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=inventory;Integrated Security=True");
con.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
SqlCommand com = new SqlCommand(" update product set quantity = quantity + '" + Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value ?? DBNull.Value) + "' where itemname = '" + dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value + "' ", con);
com.ExecuteNonQuery();
}
因为当我用这种形式写它时我得到错误字符串后面的未闭合引号'منتج3'。
'منتج3'附近的语法不正确。
i认为它给了这个错误,因为它想要它在参数
because when i write it in this form i get error Unclosed quotation mark after the character string 'منتج 3'.
Incorrect syntax near 'منتج 3'.
i think it give m this error because it want it in parameter
推荐答案
using (SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=inventory;Integrated Security=True"))
{
connnection.Open();
using (SqlTransaction transaction = connection.BeginTransaction())
using (SqlCommand command = new SqlCommand("UPDATE product SET quantity = quantity + @quantity WHERE itemname = @itemname", connection, transaction))
{
var pQuantity = command.Parameters.Add("@quantity", SqlDbType.Int);
var pItemName = command.Parameters.Add("@itemname", SqlDbType.NVarChar, 50);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
pQuantity.Value = Convert.ToInt32(dataGridView1.Rows[i].Cells[5].Value);
pItemName.Value = dataGridView1.Rows[i].Cells[0].Value ?? DBNull.Value;
command.ExecuteNonQuery();
}
transaction.Commit();
}
}
这篇关于需要使用参数c#从数据库中减去值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!