How to perform batch update in Sql through C# code

后端 未结 4 462
北恋
北恋 2020-12-29 09:39

I want to update multiple rows like below

update mytable set s_id = {0} where id = {1}

(Here s_id is evaluated based on some

4条回答
  •  隐瞒了意图╮
    2020-12-29 10:12

    Yes, you can build a plain-text SQL command (parameterized for security), like this:

    SqlCommand command = new SqlCommand();
    // Set connection, etc.
    for(int i=0; i< items.length; i++) {
        command.CommandText += string.Format("update mytable set s_id=@s_id{0} where id = @id{0};", i);
        command.Parameters.Add("@s_id" + i, items[i].SId);
        command.Parameters.Add("@id" + i, items[i].Id);
    }
    command.ExecuteNonQuery();
    

提交回复
热议问题