c#访问查询数据类型不匹配

时间:2017-02-24 16:23:37

标签: c# ms-access

我正在尝试查询一个表,但是当我执行它时出现错误,只有当我添加“Where Sdate = @ date和Staffid = @ mobile”时才会出现错误好的,这是代码

 private void button1_Click(object sender, EventArgs e)
 {
     getsum();
 }



public void getsum()
{
    string query = @"select SUM(SQuantity) AS Total FROM Sales where Sdate=@date AND Staffid=@mobile";
    using (var conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data source=|DataDirectory|\\crepeDB.accdb;"))
          using (var cmd = new System.Data.OleDb.OleDbCommand(query, conn))
          {
            cmd.Parameters.Add("@mobile", System.Data.OleDb.OleDbType.VarChar).Value = '1';
            cmd.Parameters.Add("@date", System.Data.OleDb.OleDbType.VarChar).Value = dateTimePicker1.Value.Date;

            conn.Open();

            using (var rdr = cmd.ExecuteReader())
            {
                if (rdr.Read())
                {
                    textBox1.Text = rdr["Total"].ToString();
                }
                rdr.Close();
            }
        }
}

希望有人可以帮我解决这个问题

1 个答案:

答案 0 :(得分:1)

尝试使用其他数据类型:

cmd.Parameters.Add("@mobile", System.Data.OleDb.OleDbType.Integer).Value = 1;
cmd.Parameters.Add("@date", System.Data.OleDb.OleDbType.DBDate).Value = dateTimePicker1.Value.Date;