[英]How to get column count in ms access database from c#
I am trying to get a sum with a column from a ms access database but so not nothing has worked this is what I have 我正在尝试从ms Access数据库中获取与列的总和,但是并非没有任何效果,这就是我所拥有的
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();
any help would be nice. 你能帮忙的话,我会很高兴。
You could try something like this, assuming you want the number of records that were matched with your query: 您可以尝试执行以下操作,假设您想要与查询匹配的记录数:
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();
EDIT: 编辑:
Misunderstood what the OP was asking for, and I also didn't catch the '<>' error. 误解了OP的要求,而且我也没有抓住'<>'错误。
I'll still keep this answer here, just for reference code. 我仍然将这个答案保留在这里,仅供参考。
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();
I had to use <> as not equals to instead of != 我必须使用<>而不是!=
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.