使用c#windows窗体中的datagridview更新数据库 [英] update database using datagridview in c# windows form

查看:174
本文介绍了使用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();

}
}
}


这篇关于使用c#windows窗体中的datagridview更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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