C# OleDbParameter 与 Access DateTime 查询

标签 c# ms-access datetime oledbparameter

我有以下查询,可以从 Access 内部或从 C# 作为 OleDbCommand 运行:

SELECT Table1.ProductType, Sum(Table1.ProductsSold)
FROM Table1
WHERE (Table1.DateTime Between #5/16/2013# And #5/17/2013#)
GROUP BY Table1.ProductType;

Table1.DateTime 是日期/时间数据类型。

现在我想将日期作为 OleDbParameters 传递。

SELECT Table1.ProductType, Sum(Table1.ProductsSold)
FROM Table1
WHERE (Table1.DateTime Between #@StartDate# And #@StopDate#)
GROUP BY Table1.ProductType;

cmd.Parameters.Add(new OleDbParameter("@StartDate", OleDbType.Date));
cmd.Parameters["@StartDate"].Value = dateTimePicker1.Value.ToShortDateString();
cmd.Parameters.Add(new OleDbParameter("@StopDate", OleDbType.Date));
cmd.Parameters["@StopDate"].Value = dateTimePicker2.Value.ToShortDateString();

我搜索并尝试了很多东西(VarChar 和字符串、单引号而不是主题标签、命令或参数中的主题标签等),但没有运气。我希望日期从午夜开始(因此是 ToShortDateString() 和 Date 类型。)

最佳答案

您需要删除查询文本中的井号 (#) 分隔符。 文字 SQL 查询需要分隔符,例如日期的 # 和字符串的 ',但在参数化中必须省略SQL 查询。作为引用,这是我的工作测试代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace oledbTest1
{
    class Program
    {
        static void Main(string[] args)
        {
            var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\__tmp\testData.accdb;");
            conn.Open();
            var cmd = new OleDbCommand(
                    "SELECT Table1.ProductType, SUM(Table1.ProductsSold) AS TotalSold " +
                    "FROM Table1 " +
                    "WHERE Table1.DateTime BETWEEN @StartDate AND @StopDate " +
                    "GROUP BY Table1.ProductType", 
                    conn);
            cmd.Parameters.AddWithValue("@StartDate", new DateTime(2013, 5, 16));
            cmd.Parameters.AddWithValue("@StopDate", new DateTime(2013, 5, 17));
            OleDbDataReader rdr = cmd.ExecuteReader();
            int rowCount = 0;
            while (rdr.Read())
            {
                rowCount++;
                Console.WriteLine("Row " + rowCount.ToString() + ":");
                for (int i = 0; i < rdr.FieldCount; i++)
                {
                    string colName = rdr.GetName(i);
                    Console.WriteLine("  " + colName + ": " + rdr[colName].ToString());
                }
            }
            rdr.Close();
            conn.Close();

            Console.WriteLine("Done.");
            Console.ReadKey();
        }
    }
}

请注意,我为参数添加了不同的名称(以便更接近您所做的操作),但请记住,对于 Access OLEDB,参数名称将被忽略,并且参数必须> 按照它们在命令文本中出现的顺序完全相同进行定义。

编辑

如果您只想提取 DateTimePicker 值的日期部分,请尝试如下操作:

DateTime justTheDate = dateTimePicker1.Value.Date;
MessageBox.Show(justTheDate.ToString());

当我运行时,MessageBox 总是显示类似 2013-05-01 00:00:00 的内容(而不是当前时间)。

关于C# OleDbParameter 与 Access DateTime 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16621695/

相关文章:

c# - 使用 Vector3.Distance 的性能问题

c# - 如何使用 PEM key 在 C# 中对 JWT 进行签名/加密?

sql - ADODB/Access : Update Multiple Fields from Record in Same Table

c++ - 如何从 Microsoft Access 调试 dll

C# 日期时间运算符 +=

c# - 将类型数组转换为通用数组?

c# - 获取正在运行的进程的维度

sql - 如何从几个可能空白的日期字段中查询最早的日期?

c# - 将日期时间转换为 yyyy/MM/dd

python - pyspark 的 "between"函数 : range search on timestamps is not inclusive