使用c#windows窗体中的datagridview更新数据库
问题描述:
我使用下面的代码更新数据库值,但它不能正常工作
请帮助
我是c的新手#
i am using below code for updating database value, but it is not working
please help
I am new to c#
private void button1_Click(object sender, EventArgs e)
{
if (objConn1.State != ConnectionState.Open)
{
objConn1.Open();
}
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
DialogResult result;
result = MessageBox.Show("Are You sure want to update?", "Conformation", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//string sql = "update [SAPProduction].[ProductionData] set Rejection " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "' ";
// Jobcard = your desire Rows Jobcard Id
SqlCommand command = new SqlCommand("UPDATE ProductionData_BK SET Rejection = @Reject Where id = '" + dataGridView1.CurrentRow.Cells["id"].Value.ToString() + "' ", objConn1);
command.Parameters.AddWithValue("@Reject",dataGridView1.SelectedCells[13].ToString());
//objConn1.Open();
command.ExecuteNonQuery();
objConn1.Close();
}
}
}
拼写和索引[/ edit]
[edit]spelling and indexation[/edit]
答
hi
访问此链接
http://social.msdn .microsoft.com /论坛/ windows / en-US / 48f171c1-c4ff-47b4-954d-0a93fdc4085c / updates-database-from-datagridview-in-c [ ^ ]
//将此c#代码替换为正确的位置...
//您可以使用调试器查看问题究竟是什么...
string query =UPDATE ProductionData_BK+
SET Rejection = @ Reject;+
其中id ='dataGridView1。 CurrentRow.Cells ['id']。Value.ToString()',objConn1
SqlCommand insertCommand = new SqlCommand(query,objConn1);
//replace this c# code to the right place...
// you can use the debugger to see were exactly the problem is...
string query = "UPDATE ProductionData_BK " +
"SET Rejection=@Reject; " +
"Where id = 'dataGridView1.CurrentRow.Cells['id'].Value.ToString()', objConn1"
SqlCommand insertCommand = new SqlCommand(query, objConn1);
private void button1_Click(object sender,EventArgs e)
{
if(objConn1.State!= ConnectionState.Open)
$
objConn1.Open();
}
for(int i = 0;我< dataGridView1.SelectedRows.Count; i ++)
{
DialogResult结果;
result = MessageBox.Show(你确定要删除?,Conformation, MessageBoxButtons.YesNo);
if(result == System.Windows.Forms.DialogResult.Yes)
{
// string sql = update [SAPProduction]。[ProductionData] set Rejection+where id ='+ dataGridView2.CurrentRow.Cells [id]。Value.ToString()+';
/ / Jobcard =您的愿望行作业卡ID
SqlCommand command = new SqlCommand(UPDATE SAPProduction.ProductionData_BK SET Rejection = @Reject Where id ='+ dataGridView1.CurrentRow.Cells [id]。Value .ToString()+',objConn1);
command.Parameters.AddWithValue(@ Reject,dataGridView1.CurrentRow.Cells [Rejection]。 Value.ToString());
command.E xecuteNonQuery();
MessageBox.Show(Row Updated ...);
objConn1.Close();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (objConn1.State != ConnectionState.Open)
{
objConn1.Open();
}
for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
{
DialogResult result;
result = MessageBox.Show("Are You sure want to Delete?", "Conformation", MessageBoxButtons.YesNo);
if (result == System.Windows.Forms.DialogResult.Yes)
{
//string sql = "update [SAPProduction].[ProductionData] set Rejection " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "' ";
// Jobcard = your desire Rows Jobcard Id
SqlCommand command = new SqlCommand("UPDATE SAPProduction.ProductionData_BK SET Rejection = @Reject Where id = '" + dataGridView1.CurrentRow.Cells["id"].Value.ToString() + "' ", objConn1);
command.Parameters.AddWithValue("@Reject", dataGridView1.CurrentRow.Cells["Rejection"].Value.ToString());
command.ExecuteNonQuery();
MessageBox.Show("Row Updated...");
objConn1.Close();
}
}
}