【问题标题】:Get a value of aggregate function count() from datareader从 datareader 获取聚合函数 count() 的值
【发布时间】:2018-02-01 05:54:34
【问题描述】:

用 C# 编码,我的查询是,

query = "select COUNT(*) as rowsCount from employee_leaves where PARTY_ID ='10'";

执行后,

OracleDataReader dr = command.ExecuteReader();

我现在如何才能将计数计入int

我从数据库中得到的表有 1 行包含 3 个 as,

我试过了,int i = dr["rowsCount"];

还有这个,int i = dr.GetInt32(0);

但没有成功。

【问题讨论】:

    标签: c# sql datareader


    【解决方案1】:

    由于您只需要获取一个值,请使用ExecuteScalar()。示例

    string sqlQuery = "select COUNT(*) as rowsCount from employee_leaves where PARTY_ID ='10'";
    OracleCommand command = new OracleCommand(sqlQuery, connection);
    // other codes here such as opening the connection
    int count = Convert.ToInt32(command.ExecuteScalar());
    

    【讨论】:

      【解决方案2】:

      假设您使用 C# 编写代码。请在dr.Read() 之前使用(它应该返回true),然后使用dr 从第一行读取值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        • 1970-01-01
        • 2012-03-10
        相关资源
        最近更新 更多