[英]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();
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
实例的属性。 OleDbType
enumeration, I guessed it was DBDate
but I could be wrong, please correct it if necessary. OleDbType
枚举指定基础访问类型,我猜想它是DBDate
但是我可能是错的,请在必要时进行更正。 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.