如何将sql表行计数存储在字符串变量中?

时间:2012-10-23 16:36:13

标签: asp.net sql-server

我正在尝试存储查询字符串的结果

q = "select count(*) from test" (asp.net c#)

在一个字符串变量中,这样我就可以在点击按钮时显示标签上的计数。

2 个答案:

答案 0 :(得分:5)

    int RecordCount;
    RecordCount = Convert.ToInt32(cmd.ExecuteScalar());
   string myString =  RecordCount.ToString();

答案 1 :(得分:1)

ExecuteScalar can be null的值,请确保检查为空

int RecordCount = 0;
object retVal = cmd.ExecuteScalar();
if(retVal != null)
    RecordCount = Convert.ToInt32(cmd.ExecuteScalar());