【发布时间】:2020-09-11 03:28:02
【问题描述】:
我有一个带有 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# 中编写此查询? 请帮帮我。
【问题讨论】:
我有一个带有 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# 中编写此查询? 请帮帮我。
【问题讨论】:
以下是在控制台应用程序中使用 C# 访问 Access 数据库的一些示例代码。如果需要,您可以将此代码改编为 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);
}
【讨论】:
问题不是编程语言,而是对 mdb 访问的查询。访问需要在输入日期之前输入单词DateValue:
string myQuery = "Select * FROM tableName WHERE date= DateValue ('03/02/2009')";
【讨论】: