简体   繁体   English

如何在c#中根据日期查询日期时间

[英]How to query datetime based on date in c#

I have an MS-Access database with a DateTime column.我有一个带有 DateTime 列的 MS-Access 数据库。
ex: 03/08/2009 12:00:00 AM .例如: 03/08/2009 12:00:00 AM

I want query based on date like:我想要基于日期的查询,如:

select * from tablename where date='03/08/2009'

I want display data as 03/08/2009 12:00:00 AM .我想将数据显示为03/08/2009 12:00:00 AM

How would I write this query in C#?我将如何在 C# 中编写此查询? Please help me.请帮我。

The question is not programming language, but the query to mdb access.问题不是编程语言,而是对 mdb 访问的查询。 Access requires the word DateValue before you enter a date:在输入日期之前,Access 需要单词DateValue

string myQuery = "Select * FROM tableName WHERE date= DateValue ('03/02/2009')"; 

Here's some sample code using C# in a console app to access an Access DB.下面是在控制台应用程序中使用 C# 访问 Access DB 的一些示例代码。 You can adapt this code to windows or ASP.NET if needed.如果需要,您可以将此代码调整为 Windows 或 ASP.NET。

/* Replace with the path to your Access database */
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;";

try
{
using(OleDbConnection conn = new OleDbConnection(connectionString)
{
   conn.Open();       
   string myQuery = "Select * FROM tableName WHERE date='03/02/2009'";       
   OleDbCommand cmd = new OleDbCommand(myQuery, conn);
   using(OleDbDataReader reader = cmd.ExecuteReader())
   {
      //iterate through the reader here
      while(reader.Read())
      {
         //or reader[columnName] for each column name
         Console.WriteLine("Fied1 =" + reader[0]); 
      }
   }
}

}
catch (Exception e)
{
   Console.WriteLine(e.Message);
}

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

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