我正在尝试从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();
任何帮助都会很好。
答案 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;不等于!=