简体   繁体   English

MS访问条件表达式中的数据类型不匹配c#

[英]MS access Data type mismatch in criteria expression c#

I have error, i want to do a filter, which consist some combobox and datetimepicker and it's will be show in dategridview.我有错误,我想做一个过滤器,它包含一些组合框和日期时间选择器,它将显示在 dategridview 中。 And when i want select date from datebase, i have this error:当我想从数据库中选择日期时,出现以下错误:

"Data type mismatch in criteria expression". “条件表达式中的数据类型不匹配”。

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();
string strSql1 = 

Better:更好:

 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();
} 
}

If that doesn't work, then please tell us what the values for param1, param2, date1, and date2 are when they enter the method.如果这不起作用,那么请告诉我们 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+"#" ;

"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+"#"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM