如何从c#中获取ms访问数据库中的列数

时间:2017-07-05 02:10:59

标签: c#

我正在尝试从ms访问数据库中获取一个列的总和,但所以没有任何工作,这就是我所拥有的

      Conn.Open();
            Command.CommandText = "Select SUM(c_qty) from COLLVAULT_COINS WHERE c_type!='US Currency' AND c_type!='World Currency' AND c_type!='World Coins' AND c_listName = '" + activeCollection + "'";
            int total_1 = Convert.ToInt32(Command.ExecuteScalar());
            //total sum - get how many match the query
            lblcollectedcount.Text = Convert.ToString(total_1);
            Conn.Close();

任何帮助都会很好。

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的方法,假设您需要与查询匹配的记录数量:

Conn.Open();
Command.CommandText = "Select SUM(c_qty) as sum, Count(*) as count from COLLVAULT_COINS WHERE c_type <> 'US Currency' AND c_type <> 'World Currency' AND c_type <> 'World Coins' AND c_listName = '" + activeCollection + "'";    //

int total_matches = 0;

using (MySqlDataReader dataReader = Command.ExecuteReader())
{
    if (dataReader.Read()) 
    {
        lblcollectedcount.Text = Convert.ToString(dataReader["sum"]);
        total_matches = Convert.ToInt32(dataReader["count"]);
    } 
    else 
    {
        //nothing was read, handle that case
    }
}

Conn.Close();

编辑:

误解了OP要求的内容,而且我也没有抓住'&lt;&gt;'错误。

我仍然会在这里保留这个答案,仅供参考代码使用。

答案 1 :(得分:1)

      Conn.Open();
            Command.CommandText = "Select SUM(c_qty) from COLLVAULT_COINS WHERE c_type <>'US Currency' AND c_type<>'World Currency' AND c_type<>'World Coins' AND c_listName = '" + activeCollection + "'";
            int total_1 = Convert.ToInt32(Command.ExecuteScalar());
            //total records
            lblcollectedcount.Text = Convert.ToString(total_1);
            Conn.Close();

我不得不使用&lt;&gt;不等于!=