我们有这个C#代码将根据结构中的标志更新SQL Server数据库表。C#代码批量更新SQL服务器
public struct stSRK
{
public string Sno;
public string SClaimID;
public bool SType;
public string SText;
}
中的数据结构可以在time.Currently去高达5000-10000记录,我们使用的是下面的C#代码更新数据库,它使基于该结构的数据库调用的“N”数字。将有助于有一个替代方案使用相同的结构进行批量更新。请参阅下面的方法
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;
}
值得注意的是:这种方法不能很好地适应更大的更新 - 理想情况下应该是一种混合方法,在这种方法中,您可以批量处理1,000条记录(或者对于所处理的记录/环境类型有意义)。如果你用200,000条记录尝试上面的内容,SQL服务器可能会在查询分析中窒息。 – Tao 2013-09-18 12:10:46
此外,在你提出的where子句中可能存在一个逻辑缺陷:如果我想删除一个'Sno = 1 AND ClaimID = 2'的记录,并且我想删除'Sno = 2 AND ClaimID = 1'的另一个记录, ,我也会意外删除'Sno = 1 AND ClaimID = 1'或'Sno = 2 AND ClaimID = 2'的记录。 – Tao 2013-09-18 12:12:28
感谢陶,你说得很对,我应该在我的回答中提到这一点。我跳到了可能错误的假设,即他们总是独一无二的。由于我不知道Sno的含义,所以由OP来完成。 – Peter 2013-09-18 12:58:49