使用参数c#将数据网格视图中的值添加到数据库

时间:2015-08-10 13:46:37

标签: c# sql sql-server-2008 datagridview parameters

我想更新并添加数据网格视图中列的值,数据库中的列使用参数itemname = column 1,我得到了此代码

 using (SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=inventory;Integrated Security=True"))
        {

            connection.Open();
            using (SqlTransaction transaction = connection.BeginTransaction())
            using (SqlCommand command = new SqlCommand("UPDATE product SET quantity = quantity + @quantity where itemname = @itemname", connection, transaction))
            {
                SqlParameter pQuantity = command.Parameters.Add("@quantity", SqlDbType.Int );
                SqlParameter pItemName = command.Parameters.Add("@itemname", SqlDbType.VarChar, 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();
                }
                command.ExecuteNonQuery();
                transaction.Commit();

            }
        }

当我按下按钮时没有任何事情发生

0 个答案:

没有答案