使用C#在MS访问中引入特定日期记录

时间:2017-11-10 07:11:22

标签: c# date ms-access record

我想在特定日期带上佣金记录。这是我的代码;

globalxx = 0;
        string month1 = dateTimePicker2.Value.Month.ToString();
        string day1 = dateTimePicker2.Value.Day.ToString();
        string year1 = dateTimePicker2.Value.Year.ToString();
        string s2 = "#" + month1 + "/" + day1 + "/" + year1 + "#";
        DataTable results = new DataTable();
        using (OleDbConnection conn = new OleDbConnection(xi))
        {
            OleDbCommand cmd = new OleDbCommand("select * from COMMISSION where DateCommission='" + s2 + "'", conn);
            cmd.Parameters.AddRange(new OleDbParameter[]
       {
           new OleDbParameter("@DateCommission", s2)
       });

            conn.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
            adapter.Fill(results);
            dataGridView2.DataSource = results;

但问题是它在

处出错
adapter.Fill(results);

说:“OleDB Excpetion已经被标准表达式中的数据类型不匹配。”

我需要帮助。

我的MS Access架构是: DateCommission:日期时间 DriverName:TEXT DriveVehicleNumber:TEXT 委托价格:NUMBER

我没有参数。

这是c#的前端; Front End

1 个答案:

答案 0 :(得分:0)

处理您的日期,然后在SQL中连接格式化的字符串表达式:

string textDate = dateTimePicker2.Value.ToString("yyyy'/'MM'/'dd");
string s2 = "#" + textDate + "#";

然后:

OleDbCommand cmd = new OleDbCommand("select * from COMMISSION where DateCommission = " + s2 + "", conn);

或(首选)使用数据类型 DateTime 参数,您可以直接传递datepicker的 Value