【问题标题】:MS access Data type mismatch in criteria expression c#MS访问条件表达式c#中的数据类型不匹配
【发布时间】:2017-01-14 03:30:36
【问题描述】:

我有错误,我想做一个过滤器,它由一些组合框和 datetimepicker 组成,它将显示在 dategridview 中。当我想从数据库中选择日期时,我有这个错误:

“标准表达式中的数据类型不匹配”。

string strSql1 = @"Select * from GGG where 
                   device_id LIKE '%" + metroComboBox1.Text + "%' 
                   AND parameter_id LIKE '%" + metroComboBox3.Text + "%' 
                   AND time_id Between 'date1' AND 'date2'";`

string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Ирина\\Desktop\\Visual Studio 2013\\Projects\\WindowsFormsApplication1\\WindowsFormsApplication1\\BD3.accdb;Persist Security Info=False";

string date1 = metroDateTime1.Value.ToString("MM/dd/yyyy H:mm:ss", CultureInfo.InvariantCulture).Replace('.', '/');
string date2 = metroDateTime3.Value.ToString("MM/dd/yyyy H:mm:ss", CultureInfo.InvariantCulture).Replace('.', '/');

//string strSql = ("SELECT * FROM GGG where device_id LIKE '%" + metroComboBox1.Text + "%' AND parameter_id LIKE '%" + metroComboBox3.Text + "%' ");
string strSql1 = "Select * from table1 where device_id LIKE '%" + metroComboBox1.Text + "%' AND parameter_id LIKE '%" + metroComboBox3.Text + "%' AND time_id Between 'date1' AND 'date2'";

//string strSql = "SELECT event_id, device_id, parameter_id, parameter_int_id, time_id, user_id FROM table1 where time_id between #" + metroDateTime1.Value.ToString("yyyy'/'MM'/'dd") + "# AND #" + metroDateTime3.Value.ToString("yyyy'/'MM'/'dd") + "" "# AND #" device_id LIKE '%" + comboBox2.Text + "%' "# AND #" parameter_id LIKE '%" + comboBox3.Text + "%' ");
//string strSql = "SELECT event_id, device_id, parameter_id, date_id, time_id, user_id FROM GGG where device_id like '%" + metroComboBox1.Text + "%' AND parameter_id LIKE '%" + metroComboBox3.Text + "%' ";



OleDbConnection con = new OleDbConnection(constr);
OleDbCommand cmd = new OleDbCommand(strSql1, con);
con.Open();
cmd.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable GGG = new DataTable();
da.Fill(GGG);
metroGrid1.DataSource = GGG;
con.Close();

【问题讨论】:

    标签: c# sql ms-access select


    【解决方案1】:
    string strSql1 = 
    

    更好:

     performSelect(metroComboBox1.Text, metroComboBox3.Text, date1, date2);
    private void performSelect(string param1, string param2, DateTime Date1,DateTime Date2){ 
    using(OleDbCommand com = Conn.CreateCommand()){
       com.CommandText = "select * from GGG where device like %@mcb1% and parameter like %@mcb3% and time between @date1 and @date2";
       com.Parameters.AddWithValue("@mcb1",param1);
       com.Parameters.AddWithValue("@mcb3",param2);
       com.Parameters.AddWithValue("@date1",Date1);
       com.Parameters.AddWithValue("@date2",Date2);
       com.ExecuteReader();
    } 
    }
    

    如果这不起作用,请告诉我们param1、param2、date1和date2在进入方法时的值是什么。 "Select * from GGG where device_id LIKE '%" + metroComboBox1.Text + "%' AND parameter_id LIKE '%" + metroComboBox3.Text + "%' AND time_id between #"+date1+"# AND #"+date2+"#" ;

    "Select * from GGG where device_id LIKE '%" + metroComboBox1.Text + "%' AND parameter_id LIKE '%" + metroComboBox3.Text + "%' AND time_id Between #"+date1+"# AND #"+date2+"# AND clock_id between #"+date3+"# and #"+date4+"#"
    

    【讨论】:

    • 对不起 - 习惯。我改变了答案
    • 请参阅我第一行的编辑。对于 Acess,将日期的单引号更改为 # 符号
    • 谢谢,第一线工作!!! ComboBox 工作,和 datetimepicker,但只有日期,而不是时间,也可能有一些错误
    • 我不记得在 Acess 中,但请确保您的日期字段是 dateTIME 而不仅仅是日期。在 SQL 中,两者都有 - 因此,如果您添加日期字段而不是 dateTIME,您将丢失时间部分
    • 我可以同时添加 date3 和 date4,例如?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多