[英]how to get a count(*) for number of records updated
i am updating a sql server 2008 database using c# like this: 我正在使用C#更新sql server 2008数据库,如下所示:
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++;
}
i would like to know whether it is possible to return how many records were updated? 我想知道是否可以返回更新了多少条记录?
Yes. 是。 Use
ExecuteNonQuery
's return value. 使用
ExecuteNonQuery
的返回值。 :-) :-)
Quoting ExecuteNonQuery
's documentation : 引用
ExecuteNonQuery
的文档 :
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.
对于UPDATE,INSERT和DELETE语句,返回值是该命令影响的行数。
Capture and use the result of ExecuteNonQuery
to an integer. 捕获
ExecuteNonQuery
并将其使用为整数。 That method returns the number of records affected by the operation. 该方法返回受操作影响的记录数。
See SqlCommand.ExecuteNonQuery Method . 请参见SqlCommand.ExecuteNonQuery方法 。
That being said, how much do you trust your datasource? 话虽这么说,您对数据源有多信任? Enough to bet your data integrity on it?
足以赌您的数据完整性吗? I'd be remissed if I didn't implore you to explore parameterized queries.
如果我不恳求您探索参数化查询,我将被撤职。 A
using
statement would also be warranted so that your disposable resources ( SqlConnection
, SqlCommand
, etc.) are properly dealt with. 还可以
using
语句,以便正确处理您的可弃资源( SqlConnection
, SqlCommand
等)。
Refering to SqlCommand.ExecuteNonQuery Method : 引用SqlCommand.ExecuteNonQuery方法 :
Return Value
返回值
Type: System.Int32类型:System.Int32
The number of rows affected.受影响的行数。
将SELECT @@ROWCOUNT
附加到您的语句,并使用ExecuteScalar
而不是ExecuteNoneQuery
。
您可以使用@@ ROWCOUNT 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.