Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MS Access中带有日期类型参数的SQL语句_C#_Ms Access_Port - Fatal编程技术网

C# MS Access中带有日期类型参数的SQL语句

C# MS Access中带有日期类型参数的SQL语句,c#,ms-access,port,C#,Ms Access,Port,我有一个用于在MS Access中查询的SQL语句。我想得到日期之间的交易结果 这是我的代码: DateTime pFromNew = Convert.ToDateTime(this.dateTimePicker1.Value.ToString("yyyy-MM-dd")); DateTime pToNew = Convert.ToDateTime(this.dateTimePicker2.Value.ToString("yyyy-MM-dd")); string pFrom = "#"

我有一个用于在MS Access中查询的SQL语句。我想得到日期之间的交易结果

这是我的代码:

 DateTime pFromNew = Convert.ToDateTime(this.dateTimePicker1.Value.ToString("yyyy-MM-dd"));
 DateTime pToNew = Convert.ToDateTime(this.dateTimePicker2.Value.ToString("yyyy-MM-dd"));

 string pFrom = "#" + pFromNew.ToString() + "#";
 string pTo = "#" + pToNew.ToString() + "#";

 chrTrans.Series["Class"].Points.Clear();

 oconn.Open();

 OleDbCommand cmd = oconn.CreateCommand();
 cmd.CommandType = CommandType.Text;

 cmd.CommandText = "Select ClassType,Nametree,TransWeight,Valuedate  from tblTrans where Nametree = '" + this.cboNametree.Text.Trim() + "' and valuedate between '" + pFrom + "' and '" + pTo + "'";
 //+ "' and valuedate between '"+ this.dateTimePicker1.Text +"' and '"+ this.dateTimePicker2.Text +"'";

 cmd.ExecuteNonQuery();
这句话有什么不对

我总是会遇到这样的错误:

条件表达式中的数据类型不匹配


删除字符串中的单引号。。。我会给你看

这一行:

cmd.CommandText = "Select ClassType,Nametree,TransWeight,Valuedate  from tblTrans where Nametree = '" + this.cboNametree.Text.Trim() + "' and valuedate between '" + pFrom + "' and '" + pTo + "'";
应该是:

cmd.CommandText = "Select ClassType,Nametree,TransWeight,Valuedate  from tblTrans where Nametree = '" + this.cboNametree.Text.Trim() + "' and valuedate between " + pFrom + " and " + pTo;

原因是您已经在日期字符串周围连接了一个#符号,因此不需要单个引号。

您将此操作变得太复杂,来回转换三次

它可以简化为:

string pFrom = "#" + this.dateTimePicker1.Value.ToString("yyyy'/'MM'/'dd") + "#";
string pTo = "#" + this.dateTimePicker2.Value.ToString("yyyy'/'MM'/'dd") + "#";

// snip

cmd.CommandText = "Select ClassType,Nametree,TransWeight,Valuedate from tblTrans where Nametree = '" + this.cboNametree.Text.Trim() + "' and valuedate between " + pFrom + " and " + pTo + "";

我已经有一段时间没有这样做了,但我认为你不需要在日期前后使用
。您将执行
#1/1/2018#
而不是
#1/1/2018#
-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-检查这是否有帮助请单击左侧的绿色复选标记将其标记为答案。谢谢