如何进行批量更新?

cho*_*bo2 6 c# sql sql-server linq-to-sql

我想知道有没有办法进行批量更新?我正在使用ms sql server 2005.

我看到了sqlDataAdaptor,但似乎你必须首先使用它的select语句,然后填充一些数据集并对数据集进行更改.

现在我使用linq to sql来做选择所以我想尝试保持这种方式.然而,进行大规模更新太慢了.那么我可以将我的linq保留到sql(对于选择部分)但使用不同的东西来进行批量更新吗?

谢谢

编辑

我对这种临时表方式感兴趣,但我不知道该怎么做,仍然不清楚如何更快,因为我不明白更新部分的工作原理.

那么有谁能告诉我这将如何工作以及如何处理并发连接?

EDIT2

这是我尝试使用xml进行大规模更新的最新尝试,但它使用了大量资源,而我的共享主机不允许它通过.所以我需要一种不同的方式,这就是为什么我不是在寻找一个临时表.

using (TestDataContext db = new TestDataContext())
            {
                UserTable[] testRecords = new UserTable[2];
                for (int count = 0; count < 2; count++)
                {
                    UserTable testRecord = new UserTable();

                    if (count == 1)
                    {
                        testRecord.CreateDate = new DateTime(2050, 5, 10);
                        testRecord.AnotherField = true;
                    }
                    else
                    {
                        testRecord.CreateDate = new DateTime(2015, 5, 10);
                        testRecord.AnotherField = false;
                    }


                    testRecords[count] = testRecord;
                }

                StringBuilder sBuilder = new StringBuilder();
                System.IO.StringWriter sWriter = new System.IO.StringWriter(sBuilder);
                XmlSerializer serializer = new XmlSerializer(typeof(UserTable[]));
                serializer.Serialize(sWriter, testRecords);             

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    string sprocName = "spTEST_UpdateTEST_TEST";

                    using (SqlCommand cmd = new SqlCommand(sprocName, con))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;

                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        SqlParameter param1 = new SqlParameter("@UpdatedProdData", SqlDbType.VarChar, int.MaxValue);
                        param1.Value = sBuilder.Remove(0, 41).ToString();
                        cmd.Parameters.Add(param1);
                        con.Open();
                        int result = cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
Run Code Online (Sandbox Code Playgroud)

@ Fredrik Johansson我不确定你的说法是什么.就像在我看来,你希望我为每条记录做一个更新声明.我不能这样做,因为我需要更新1到50,000+记录,直到那时我才知道.

编辑3

所以现在这是我的SP.我认为它应该能够并发连接,但我想确保.

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_MassUpdate]
@BatchNumber uniqueidentifier 
AS
BEGIN
    update Product
    set ProductQty = 50
    from Product prod
    join StagingTbl stage on prod.ProductId = stage.ProductId
    where stage.BatchNumber = @BatchNumber

    DELETE FROM StagingTbl
    WHERE BatchNumber = @BatchNumber

END
Run Code Online (Sandbox Code Playgroud)

apo*_*020 1

正如 allonym 所说,使用 SqlBulkCopy,速度非常快(我发现速度提高了 200 倍以上 - 从 1500 秒到 6 秒)。但是,您可以使用 DataTable 和 DataRows 类向 SQlBulkCopy 提供数据(这似乎更容易)。以这种方式使用 SqlBulkCopy 还具有兼容 .NET 3.0 的额外优势(Linq 仅在 3.5 中添加)。
查看http://msdn.microsoft.com/en-us/library/ex21zs8x%28v=VS.100%29.aspx获取一些示例代码。