我這樣做:SQL C#更新表
var command = new SqlCommand(query, myConnection);
foreach (DataRow row in dt.Rows)
{
query = @"update FileLog set
FaxStatus=" + "'" + row.ItemArray[0].ToString() + "'," +
"FaxedPageCount=" + "'" + row.ItemArray[1].ToString() + "'," +
"dtFaxed=" + "'" + row.ItemArray[2].ToString() + "'," +
"RetryCount=" + "'" + row.ItemArray[4].ToString() + "' " +
"where JobID=" + "'" + row.ItemArray[3].ToString() + "'";
command = new SqlCommand(query, myConnection);
command.ExecuteNonQuery();
}
JobID
是uniqueidentifier
而且我收到此錯誤:
Conversion failed when converting from a character string to uniqueidentifier.
我在做什麼錯?
的作業ID字段是這樣的:
DB9424E5-1E73-4108-A855-B252E516A2A2
2EB17B8B-C0A1-46FE-82AF-37AEF2A8A6EC
C24F0460-7667-4A3A-8D8F-64B9728C2359
8DCDB020-8C7B-493E-9D21-719CBAFC16B6
首先,無論是使用'StringBuilder'或更好的'的String.Format(...)'來避免構建那些令人討厭的交錯字符串。你甚至可以同時使用!它比創建多個字符串對象只需返回一個字符串更有效。 – michael 2011-06-15 17:14:40
它確實是JobID列(沒有涉及其他uniqueidentifier列)? – 2011-06-15 17:15:01
另外,參數化查詢是您的朋友 - 不要將SQL構建爲字符串。寫一次查詢,然後插入參數 – 2011-06-15 17:15:52