1

我们有这个 C# 代码,它将根据结构中的标志更新 SQL Server 数据库表。

public struct stSRK
{ 
    public string Sno;
    public string SClaimID;
    public bool SType;
    public string SText; 
}

结构中的数据一次最多可以包含 5000-10000 条记录。目前我们正在使用以下 C# 代码来更新数据库,它会根据结构进行“n”次数据库调用。将有助于有一种替代方法来使用相同的 struct 进行批量更新。请看下面的方法

public int UpdateClaims(List<stSRK> _lt, string strClaimType)
{
try
{
    int iCount = 0;
    for (int i = 0; i < _lt.Count; i++)
        {
            if (_lt[i].sType == true)
            {
                if (_lt[i].SText == 'A')
                {
                iCount += Convert.ToInt16(SQL.ExecuteSQL("UPDATE table SET ClaimType = '" + strClaimType + "' WHERE 
                    (Sno = '" + _lt[i].Sno + "' AND ClaimID = '" + _lt[i].SClaimID + "')"));
                }
                else
                {
                iCount += Convert.ToInt16(SQL.ExecuteSQL("UPDATE table SET ClaimType = '" + strClaimType + "' WHERE 
                    (Sno = '" + _lt[i].Sno + "' AND ClaimID = '" + _lt[i].SClaimID + "')"));
                }
            }
            else
            {
                if (_lt[i].SText == 'B')
                {
                iCount += Convert.ToInt16(SQL.ExecuteSQL("UPDATE table SET ClaimType = '" + strClaimType + "' WHERE 
                    (Sno = '" + _lt[i].Sno + "' AND ClaimID = '" + _lt[i].SClaimID + "')"));
                }
                else
                {
                iCount += Convert.ToInt16(SQL.ExecuteSQL("UPDATE table SET ClaimType = '" + strClaimType + "' WHERE 
                    (Sno = '" + _lt[i].Sno + "' AND ClaimID = '" + _lt[i].SClaimID + "')"));
                }
            }
        return iCount;
        }
catch (Exception e)
{
    throw e.Message;
}
4

3 回答 3

1

对于所有情况,您的查询似乎都是相同的..

你不能重写你的代码来只构建一个 SQL 语句吗?

像这样的东西:

public int UpdateClaims(List<stSRK> _lt, string strClaimType)
{
    try
    {
        string allSno = String.Join(",", _lt.Select(l=> l.Sno.ToString()).ToArray());
        string allClaimID = String.Join(",", _lt.Select(l=> l.SClaimID.ToString()).ToArray());

        // Warning: NOT Production code, SQLInjection hazard!
        return Convert.ToInt16(SQL.ExecuteSQL("UPDATE table SET ClaimType = '" + strClaimType + @"' WHERE 
                        (Sno IN(" + allSno + ") AND ClaimID IN(" + allClaimID + "))"));
    catch (Exception e)
    {
        //This is not a good idea, as this way you loose all innerException 
        // information about the exception. And you might just want to remove 
        // the try-catch alltogether if you just rethrow the exception.
        throw e.Message;
    }
}
于 2013-09-18T11:05:04.323 回答
1

我的代码:

 DataTable tblDetails = new DataTable("tblPlanDetail");

        tblDetails.Columns.Add("row1").DataType = typeof(Int32);
        tblDetails.Columns.Add("row2").DataType = typeof(DateTime);
        tblDetails.Columns.Add("row3").DataType = typeof(String); ;
        tblDetails.Columns.Add("row4").DataType = typeof(Int32); ;


        for (int i = 0; i < table.Rows.Count; i++)
        {

            for (int j = 1; j <= DateTime.DaysInMonth(monthYear.Year, monthYear.Month); j++)
            {
                DataRow row = tblDetails.NewRow();

                DateTime DayOfMonth = new DateTime(monthYear.Year, monthYear.Month, j);
                row["row1"] = idPlan;
                row["row2"] = DayOfMonth;
                row["row3"] = table.Rows[i][0];
                row["row4"] = Int32.Parse((string)table.Rows[i][j]);

                tblDetails.Rows.Add(row);
            }
        }

        try
        {

            SqlBulkCopy sqlbulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, transaction);
            sqlbulk.ColumnMappings.Add("row1", "row1");
            sqlbulk.ColumnMappings.Add("row2", "row2");
            sqlbulk.ColumnMappings.Add("row3", "row3");
            sqlbulk.ColumnMappings.Add("row4", "row4");

            sqlbulk.DestinationTableName = "tblPlanDescription";
            sqlbulk.BatchSize = 2;
            sqlbulk.WriteToServer(tblDetails);
            transaction.Commit();
        }
        catch (Exception exp)
        {

            transaction.Rollback();
        }
        finally
        {
            transaction.Dispose();
            connection.Close();
        }

那是批量插入。您可以将其插入临时表。然后您可以执行 SP,它将在服务器端创建必要的索引并更新必要的数据

这里

于 2013-11-13T10:16:52.090 回答
0

尝试使用 linq 查询进行批量更新。这将提高应用程序的性能。

您可以在此处获得有关大规模更新 linq 查询的帮助:

如何在 Linq 中运行批量更新/删除查询?

http://msdn.microsoft.com/en-us/library/bb399339.aspx

希望这会帮助你。

于 2013-03-11T05:01:45.653 回答