使用ADO .Net批量更新 [英] Batch Update Using ADO .Net
问题描述
下午好,
我想根据用户选择的记录对SQL Server数据库中的表进行批量更新. 用户可以选择多个记录,然后单击以执行数据库更新.我目前正在迭代 DataGridView中的每个选定行并填充List< int>.每个记录ID. 我使用List< int>创建具有单个列"record_id"的数据表.包含我想更新的记录ID. 然后我使用以下代码块 进行更新的代码:
I would like to preform a batch update on a table in my SQL Server database based upon the records a user selects. The user has the ability to select multiple records and then click to preform the database update. I'm currently iterating through each selected row in a DataGridView and populating a List<int> with each record id. I use the List<int> to create a DataTable with a single column "record_id" containing the record ids I'd like to update. Then I use the following block of code to preform the update:
string updateSql = @"update Email_Notifications
set time_email_sent = null
where record_id = @recordId";
using (SqlConnection conn = new SqlConnection(Properties.MySettings.Default.CallOffConnection))
{
SqlDataAdapter da = new SqlDataAdapter();
da.UpdateCommand = new SqlCommand(updateSql, conn);
da.UpdateCommand.Parameters.Add("@recordId", SqlDbType.Int);
da.UpdateCommand.Parameters["@recordId"].SourceColumn = "record_id";
conn.Open();
da.Update(recordIds);
}
推荐答案
您可能需要SqlCommandBuilder.
You might need a SqlCommandBuilder.
http://www.codeproject.com/KB/database/sqldawithoutsqlcb.aspx
chanmm
这篇关于使用ADO .Net批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!