我更新使用C#這樣的SQL Server 2008數據庫的記錄數:如何獲得COUNT(*)用於更新
foreach (DataRow row in dt.Rows)
{
faxstatus = row.ItemArray[5].ToString().Contains("0000") ? "Faxed" : "Error";
query =
@"update FileLog set
FaxStatus=" + "'" + faxstatus + "'," +
"FaxedPageCount=" + "'" + row.ItemArray[1] + "'," +
"dtFaxed=" + "'" + row.ItemArray[2] + "'," +
"BiscomCode=" + "'" + row.ItemArray[5] + "', " +
"RetryCount=" + "'" + row.ItemArray[4] + "' " +
"where CONVERT(VARCHAR(255), JobID) =" + "'" + row.ItemArray[3] + "'" +
" and FaxStatus<>'Faxed'";
command = new SqlCommand(query, myConnection);
command.ExecuteNonQuery();
NumberOfRecordsUpdated++;
}
我想知道是否有可能返回多少記錄已更新?
這段代碼泄漏了,你需要'Dispose''SqlCommand'或者(更好)將它包裝在'using'中。 – 2011-06-16 20:10:37
我不得不說這是一個非常骯髒的方式來更新數據庫。我真的希望你的數據中沒有任何「'」字符!至少你應該使用參數。 – 2011-06-16 20:10:43
@Joel:參數化語句永遠! – 2011-06-16 20:11:36