两个日期之间的条件不匹配

时间:2018-11-17 15:26:50

标签: c# sql access

它始终在条件表达式中显示条件不匹配。这是我的代码。

OleDbCommand pending = new OleDbCommand("SELECT * FROM businesses WHERE business_active = 0 AND date_of_application BETWEEN '" + FromDateTxt.Value.ToString("MM/dd/yyyy") + "' AND '" + ToDateTxt.Value.ToString("MM/dd/yyyy") + "'", cn.con);
        OleDbDataReader dr_pending = pending.ExecuteReader();

1 个答案:

答案 0 :(得分:1)

请尝试以下操作,它可以避免数据类型不匹配。如果您的列不仅是数据,请将参数设置为datatime:

            SqlParameter fromDate = new SqlParameter();
            fromDate.ParameterName = "@FromDate";
            fromDate.SqlDbType = SqlDbType.Date;
            fromDate.Direction = ParameterDirection.Input;
            fromDate.Value = FromDateTxt.Value;

            SqlParameter toDate = new SqlParameter();
            toDate.ParameterName = "@ToDate";
            toDate.SqlDbType = SqlDbType.Date;
            toDate.Direction = ParameterDirection.Input;
            toDate.Value = ToDateTxt.Value;

            OleDbCommand pending = new OleDbCommand("SELECT * FROM businesses WHERE business_active = 0 AND date_of_application BETWEEN @FromDate AND @ToDate", cn.con);
            OleDbDataReader dr_pending = pending.ExecuteReader();