简体   繁体   English

MSACCESS日期时间SQL查询

[英]MSACCESS DateTIme SQL QUERY

I am from India (it may has something to do with culture info here). 我来自印度(这里可能与文化信息有关)。 I am building a desktop application in C#.Net 2010 Express with MS-ACCESS 2010 32 bit as backend. 我正在C#.Net 2010 Express中以32位MS-ACCESS 2010为后端构建桌面应用程序。 I am using OLEDB for db connectivity. 我正在使用OLEDB进行数据库连接。 I have a column named dt as Date/Time which has following values: 我有一列名为dt的Date / Time,具有以下值:

20-09-2016 22:53:32 20-09-2016 22:53:32
19-08-2016 22:54:24 19-08-2016 22:54:24
20-09-2016 22:56:01 20-09-2016 22:56:01
22-09-2016 22:56:27 22-09-2016 22:56:27
22-09-2016 22:56:41 22-09-2016 22:56:41

I need to fetch the records By Date, By Month and By Year. 我需要按日期,按月和按年获取记录。 Till now I am not able to complete the Date part so couldnt work on month and year. 到现在为止,我还无法完成日期部分,因此无法按月和按年工作。 Following is my code: 以下是我的代码:

b.com.CommandText = "SELECT * FROM srvtrans WHERE DateTime.Parse(dt)=@a ORDER BY sno DESC";
b.com.Parameters.Add("@a", dtp_srdmy.Value.ToShortDateString());
Show(dtp_srdmy.Value.ToShortDateString());
b.con.State == ConnectionState.Closed)
 con.Close();
mytemp = new DataTable();
da.Fill(mytemp);

I have also tried following variations: 我还尝试了以下变体:

WHERE CONVERT(VARCHAR(10),dt,111)=@a

WHERE CONVERT(VARCHAR(10),dt,101)=@a

WHERE dt LIKE '%@a%' 

WHERE DateTime.Parse(dt)=@a 

WHERE dt=DateValue(@a)

WHERE CAST(dt AS DATE)=@a

WHERE CONVERT(varchar, dt, 101)=@a

WHERE DATE(dt)=@a

WHERE dt=@a

but none of them works for me. 但它们都不适合我。 Please reply what updation should be made in the sql query to fetch records by date, by month and by year. 请回答应该在sql查询中进行哪些更新以按日期,按月和按年获取记录。 Thanks in advance. 提前致谢。

b.com.CommandText = "SELECT * FROM srvtrans WHERE dt = @a ORDER BY sno DESC";
b.com.Parameters.Add(new System.Data.OleDb.OleDbParameter("@a", OleDbType.DBDate) {Value = dtp_srdmy.Value });
con.Open();
  • You want to pass in the native types and not string representations of the types for your parameter values. 您要传递本机类型,而不要传递参数值的类型的字符串表示形式。 I am assuming that dtp_srdmy is probably a user control ( as pointed out by @Gord Thompson in the comments below probably a DateTimePicker ) or other type where Value is a property that returns a DateTime instance. 我假设dtp_srdmy可能是用户控件( @Gord Thompson在下面的注释中指出的,可能是DateTimePicker )或其他类型,其中Value是返回DateTime实例的属性。
  • You want to compare directly, the underlying store/provider will handle the comparison for you so do not try to wrap your values in ticks, quotes, or something else or try to do the conversion for the store engine. 您想直接进行比较,底层的商店/提供者将为您处理比较,因此请勿尝试将值包装在报价,引号或其他内容中,也不要尝试对商店引擎进行转换。
  • You should specify the underlying access type using the OleDbType enumeration, I guessed it was DBDate but I could be wrong, please correct it if necessary. 您应该使用OleDbType枚举指定基础访问类型,我猜想它是DBDate但是我可能是错的,请在必要时进行更正。
  • Open your connection before you execute any commands. 执行任何命令之前,请先打开连接。 I assume you are using a data adapter but did not see it, make sure it is associated with your Command. 我假设您正在使用数据适配器,但没有看到它,请确保它与您的Command相关联。 I omitted this part of the code as it was not relevant to the question. 我忽略了这部分代码,因为它与问题无关。

The following code should work definitely: 以下代码肯定可以正常工作:

  b.com.CommandText = "SELECT * FROM srvtrans WHERE DATEVALUE(dt)=DATEVALUE(@a) ORDER BY sno DESC";
  b.com.Parameters.Add("@a", dtp_srdmy.Value);

Upvote if this helps. 如果有帮助,请投票。

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

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