【问题标题】:How to query sum from access db by date如何按日期从访问数据库中查询总和
【发布时间】:2020-06-15 23:42:37
【问题描述】:

我的表单加载事件有查询,将显示总销售额,

given dbase value

lbldate.Text = DateTime.Now.ToString("dd/M/yyyy");
try
{
    MyConN.Open();
    OleDbCommand Cmd = new OleDbCommand();
    Cmd.Connection = MyConN;
    Cmd.CommandText = "Select Sum(TotalSales) from LSales where DISTINCT SDate='" + "label7.Text" + "'";
    //OleDbDataReader ReadeR = Cmd.ExecuteReader();
}
catch(Exception ex)
{
    MessageBox.Show("Error:  " + ex.Message);
}


but this code throwing 
>"syntax error (missing operator) in query expression `'DISTINCT SDate='16/6/2020''` 

i want to sum all `totalsales` base on date

【问题讨论】:

  • 您应该从查询中删除 DISTINCT。
  • @chetan,删除 DISTINCT 错误仍然存​​在,数据类型不匹配
  • 你试过sql server中的查询吗?
  • @sajid,因为我没有尝试使用 access 作为数据库
  • 对我来说,你有两个问题,第一个提到了@ChetanRanpariya,第二个是label7.Text 周围的双引号,这应该可以:SELECT SUM(TotalSales) as Total FROM LSales WHERE SDate='" + label7.Text + "'"

标签: c# winforms ms-access


【解决方案1】:
  1. distinctselect 一起使用,而不是where

  2. SDate='" + "label7.Text" + "'" 不好。如果sdate 是日期时间,请执行

Cmd.CommandText = "Select Sum(TotalSales) from LSales where SDate=?";
Cmd.Parameters.Add(new OleDbParameter("@1", DateTime.Parse(label7.Text)));

换句话说,请确保您通过日期。那你就不用担心类型转换了。您可能需要确保比较日期。记住可能存在的分钟和秒数。 This is good reference for you

【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2018-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多