SQLCommand批量更新查询

时间:2017-04-25 11:34:46

标签: c# asp.net asp.net-mvc asp.net-mvc-4 c#-4.0

我写了如下代码:

 for (int i = 0; i < ListToUpdate.Count; i = i + 500)
 {
   SqlCommand command = new SqlCommand();
   command.Connection = new SqlConnection("Data Source=.;Initial Catalog=mydb;Integrated Security=SSPI");
   command.Connection.Open();
   var batchList = ListToUpdate.Skip(i).Take(500);
   for (int j = 0; j < batchList.Count(); j++)
   {
      command.CommandText += string.Format("update mytable set column=@s_id{0} where columnid = @id{0};", j);
      command.Parameters.AddWithValue("@s_id" + j, ListToUpdate[j].QuantitySoldTotal);
      command.Parameters.AddWithValue("@id" + j, ListToUpdate[j].ItemId);
   }
   command.ExecuteNonQuery();
   command.Connection.Close();
 }

这应该批量执行我的表的更新,但奇怪的是查询只是第一次执行...

如果列表中有1500个项目,则只有第一批更新...而其他项目不是......

我在这里做错了什么???

3 个答案:

答案 0 :(得分:2)

我认为您需要使用batchList。

for (int i = 0; i < ListToUpdate.Count; i = i + 500)
{
  SqlCommand command = new SqlCommand();
  command.Connection = new SqlConnection("Data Source=.;Initial 
  Catalog=mydb;Integrated Security=SSPI");
  command.Connection.Open();
  var batchList = ListToUpdate.Skip(i).Take(500);
  for (int j = 0; j < batchList.Count(); j++)
  {
       command.CommandText += string.Format("update mytable set 
       column=@s_id{0} where columnid = @id{0};", j);
       command.Parameters.AddWithValue("@s_id" + j, 
       batchList[j].QuantitySoldTotal);
       command.Parameters.AddWithValue("@id" + j, batchList[j].ItemId);
       command.ExecuteNonQuery();
  }

  command.Connection.Close();
}

答案 1 :(得分:2)

每次迭代时都必须关闭并重新打开连接吗?一旦打开连接,只需在每次批量更新后使用

创建一个新的sql命令文本
command.CommandText = CommandType.Text;

我建议这个 -

SqlCommand command = new SqlCommand();
command.Connection = new SqlConnection("Data Source=.;Initial Catalog=mydb;Integrated Security=SSPI");
command.Connection.Open();

for (int i = 0; i < ListToUpdate.Count; i = i + 500) {
    var batchList = ListToUpdate.Skip(i).Take(500);
    for (int j = 0; j < batchList.Count(); j++) {
        command.CommandText += string.Format("update mytable set column=@s_id{0} where columnid = @id{0};", j);
        command.Parameters.AddWithValue("@s_id" + j, batchList[j].QuantitySoldTotal);
        command.Parameters.AddWithValue("@id" + j, batchList[j].ItemId)
    }
    command.ExecuteNonQuery();
    command.CommandText = CommandType.Text;
}
command.Connection.Close();

从生成的新列表中选择属性,例如batchList[j]

答案 2 :(得分:1)

我认为command.ExecuteNonQuery();的展示位置不正确

请检查以下代码,如果有帮助

for (int i = 0; i < ListToUpdate.Count; i = i + 500)
 {
   SqlCommand command = new SqlCommand();
   command.Connection = new SqlConnection("Data Source=.;Initial Catalog=mydb;Integrated Security=SSPI");
   command.Connection.Open();
   var batchList = ListToUpdate.Skip(i).Take(500);
   for (int j = 0; j < batchList.Count(); j++)
   {
   command.CommandText += string.Format("update mytable set column=@s_id{0} where columnid = @id{0};", j);
   command.Parameters.AddWithValue("@s_id" + j, ListToUpdate[j].QuantitySoldTotal);
   command.Parameters.AddWithValue("@id" + j, ListToUpdate[j].ItemId);


   command.ExecuteNonQuery();
   }
   command.Connection.Close();
 }