1

I'm experiencing difficulties filtering a set of data between two DateTime values.

For example: Retrieve all records From: 24/04/2013 3:54 PM; To: 24/04/2013 4:30 PM.

I'm programming in C# and using OLE DB to pull data from a Access Database. The 'To' and 'From' DateTime values are retrieved from DateTimePicker controls on a GUI. I'm trying to query data in 'receiveDateTime' field of my data source - it is stored in DateTime format in Access.

My code appears as follows:

string SQLQuery = "SELECT EmailID, ServerName, receiveDateTime, Type, status, received, processed"
                + "FROM EmailTable, EmailTypesTable, ServerTable, StatusTable"
                + "WHERE EmailTypesTable.emailTypeID = EmailTypesTable.EmailType "
                + "AND ServerTable.ServerID = EmailTable.serverID "
                + "AND StatusTable.statusID = EmailTable.statusID "
                + "AND EmailTable.receiveDateTime BETWEEN " 
                + fromDateTime.Value.ToString("g") + "AND " + toDateTime.Value.ToString("g")";

loadDataGrid(SQLQuery);

Any solutions or advice would be much appreciate.

Thanks,

Allan.

4

2 回答 2

2

1-您似乎忘记了日期值之间的单引号:

string SQLQuery = "SELECT EmailID, ServerName, receiveDateTime, Type, status, received, processed"
                + "FROM EmailTable, EmailTypesTable, ServerTable, StatusTable"
                + "WHERE EmailTypesTable.emailTypeID = EmailTypesTable.EmailType "
                + "AND ServerTable.ServerID = EmailTable.serverID "
                + "AND StatusTable.statusID = EmailTable.statusID "
                + "AND EmailTable.receiveDateTime BETWEEN '" 
                + fromDateTime.Value.ToString("g") + "' AND '" + toDateTime.Value.ToString("g") +"' ";

2-如果您也使用参数化参数会更好:

SqlConnection con = new SqlConnection(MyconnectionString);
con.Open();
string SQLQuery = "SELECT EmailID, ServerName, receiveDateTime, Type, status, received, processed"
            + "FROM EmailTable, EmailTypesTable, ServerTable, StatusTable"
            + "WHERE EmailTypesTable.emailTypeID = EmailTypesTable.EmailType "
            + "AND ServerTable.ServerID = EmailTable.serverID "
            + "AND StatusTable.statusID = EmailTable.statusID "
            + "AND EmailTable.receiveDateTime BETWEEN @dateFrom AND @dateTo";

SqlCommand cmd = new SqlCommand(SQLQuery );
cmd.Parameters.AddWithValue("@dateFrom", fromDateTime.Value.ToString("g"));
cmd.Parameters.AddWithValue("@dateTo", toDateTime.Value.ToString("g"));
SqlDataReader reader = cmd.ExecuteReader();
//...

通过尝试直接在数据库中执行此查询,您可能已经猜到了这个问题

(我在这里使用过 SQLConnection、SQLCommand...,您需要根据您使用的连接更改该部分。)

于 2013-04-24T06:29:37.397 回答
1

对于将来在比较 DateTime 值时遇到此问题的任何人,将 C# DateTime 作为 OLE 自动化日期传递给数据库是可行的!

为了访问这个值,您使用 ToOADate() 方法。

例如:

SqlConnection con = new SqlConnection(MyconnectionString);
con.Open();
string SQLQuery = "SELECT EmailID, receiveDateTime " 
                + "WHERE EmailTable.receiveDateTime " 
                + "BETWEEN @dateFrom AND @dateTo";

SqlCommand cmd = new SqlCommand(SQLQuery );
cmd.Parameters.AddWithValue("@dateFrom", fromDateTime.Value.ToOADate());
cmd.Parameters.AddWithValue("@dateTo", toDateTime.Value.ToOADate());

这很奇怪,因为虽然 DateTime 值在 DataGrid 中以一般的 DateTime 格式出现,但数据库必须这样读取它们:

一般日期时间格式:26/04/2013 9:47 AM

OLE 自动化日期:41390.4082198032

感谢您为我指出正确的方向 noobob!

于 2013-04-25T23:49:47.913 回答