我有一个在 C# .cs 文件中运行的查询:
DataSet ds = db.ExecuteDataSet(System.Data.CommandType.Text, "SELECT count(*) as counter FROM [Table] where [Table].[Field] = 'test'");
我想做的是在此查询中检索“计数器”的值并将其“返回”为我的函数返回值。
我该怎么做?
最佳答案
由于 SQL 查询不会返回数据集而是标量,因此您应该使用 .ExecuteScalar()
方法:
int count = (int)db.ExecuteScalar(System.Data.COmmandType.Text, "SELECT count(*) as counter FROM [Table] where [Table].[Field] = 'test'");
(如果您告诉我们 db
实例是什么类型,我们会更容易提供答案...)
关于c# - ASP.net C# SQL 计数(*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1473370/