2013-02-01 49 views
0

我想知道这是否是一种不好的做法。我有一排50行的数据网格,每行有大约10个文本框。页面功能非常简单,只需一个按钮即可在ms sql中进行更新和保存。用户单击按钮时,需要很长时间才能保存。我正在使用隐藏ID字段ID的SQL循环中使用更新。简单更新表格的最佳方式是什么?DataGrid上的Sql数据库更新

for (int i=0; i<options_bind.Items.Count; i++) 

{ 

if (((CheckBox)options_bind.Items[i].FindControl("check_Save")).Checked) 
    { 
     call sql update one by one here 
    } 

    } 
+0

您使用的数据表中的数据绑定到datagridview的上更新事件? –

+0

DataSet DS = new DataSet(); myCommand.Fill(DS,“vendor”); options_bind.DataSource = DS;我看到代码绑定它,并在保存按钮上,它逐行循环并调用更新方法。有时,它很慢,超时 – Diggie

回答

1

使用SqlCommanBuilder将数据绑定到datagridview的

 sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection); 
     sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter); 

     dataTable = new DataTable(); 
     sqlDataAdapter.Fill(dataTable); 
     bindingSource = new BindingSource(); 
     bindingSource.DataSource = dataTable; 

     yourDataGridView.DataSource = bindingSource; 

然后

  try 
      { 
       sqlDataAdapter.Update(dataTable); 
      } 
      catch (Exception exceptionObj) 
      { 
       MessageBox.Show(exceptionObj.Message.ToString()); 
      }