简体   繁体   English

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

[英]SQL statement in MS Access with parameters of type date

I have a SQL statement for querying in MS Access. 我有一个用于在MS Access中查询的SQL语句。 I want to get the result of the transaction between dates. 我想获取日期之间的交易结果。

This is my code: 这是我的代码:

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

What is wrong with this statement? 这句话有什么问题?

I always get this error: 我总是收到此错误:

DATA Type mismatch in criteria expression. 准则表达式中的DATA类型不匹配。

Remove the single quotes you have in your string... I will show you... 删除字符串中的单引号...我将告诉您...

This line: 这行:

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

Should be: 应该:

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

The reason is that you already concatenated a # symbol around your date strings--and the single quote thus is not needed. 原因是您已经在日期字符串周围连接了一个#符号-因此不需要单引号。

You are making this too complicated, converting back and forth three times. 您使它变得太复杂了,来回转换了三遍。

It can be reduced to: 可以简化为:

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

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

相关问题 MS Access SQL语句中迄今为止的字符串给出类型不匹配错误 - String to date in MS Access SQL statement gives type mismatch error SQL Update语句(MS ACCESS) - SQL Update statement (MS ACCESS) 如何使用C#和SQL将日期添加到列类型为“日期/类型”的MS Access数据库中? - How do I add a date using C# & SQL to an MS Access database with column type 'Date/Type'? 用于访问SQL语句的OleDb参数 - OleDb parameters for SQL statement to Access 从C#运行SQL语句时,如何忽略MS Access“类型转换错误”? - How to ignore MS Access “Type Conversion Error” when running SQL statement from C#? C# 和 MS Access SQL 带日期分隔符 - C# and MS Access SQL with Date Delimiter 使用ODBC连接时如何在MS Access或SQL Server中使用日期数据类型 - How to use date data type in either MS Access or SQL Server when using ODBC connection 案例陈述-使用BETWEEN。 女士访问SQL Server - Case Statement - using BETWEEN. Ms Access to SQL Server 对于MS Access,SQL Server的“输出已删除”语句是否具有等效项? - Is there an equivalent for SQL server's 'output deleted' statement for MS Access? SQL Server:使用参数“ date”搜索参数“ varchar”类型的值 - SQL Server: Search values of type “varchar” with parameters of type “date”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM