Ara*_*ash 5 c# sql-server visual-studio
如何获取受此声明影响的记录数:
select * from x_table where column1 = 5
Run Code Online (Sandbox Code Playgroud)
我认为ExecuteNonQuery是我需要的,但它返回-1.我期待2,因为column1 = 5我的表中有两条记录.我如何获得正确的计数?
你在打电话ExecuteNonQuery- 但这是一个查询!没有行会受到您的语句的影响,因为它只是一个查询.您需要将计数部分放入查询中,如下所示:
select count(*) from x_table where column1 = 5
Run Code Online (Sandbox Code Playgroud)
然后获得结果的最简单方法是使用ExecuteScalar:
int count = (int) command.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)
您可以将其作为读者执行并获得唯一的结果,但ExecuteScalar更简单.
select count(*) from x_table where column1=5
Run Code Online (Sandbox Code Playgroud)
将其作为普通查询执行.结果将是一行,其中一列包含匹配记录的数量.
| 归档时间: |
|
| 查看次数: |
5627 次 |
| 最近记录: |