1

我有一个带有 DateTime 列的 MS-Access 数据库。
例如:03/08/2009 12:00:00 AM

我想要基于日期的查询,例如:

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

我想将数据显示为03/08/2009 12:00:00 AM.

我将如何在 C# 中编写此查询?请帮我。

4

2 回答 2

1

问题不是编程语言,而是对 mdb 访问的查询。Access 需要DateValue输入日期前的单词:

string myQuery = "Select * FROM tableName WHERE date= DateValue ('03/02/2009')"; 
于 2013-12-12T14:23:28.727 回答
1

这是在控制台应用程序中使用 C# 访问 Access DB 的一些示例代码。如果需要,您可以将此代码改编为 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);
}
于 2009-05-21T16:14:09.863 回答