2

嗨,我试图从两个日期之间的 excel 中选择数据。我有点卡住,因为 excel 中的数据只是文本类型。我将如何查询这个?

string Date = DateTime.Now.AddDays(7).ToString("dd/MM/yyyy HH:mm");

string Date2 = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

MyCommand = new System.Data.OleDb.OleDbDataAdapter("select [Req Start Date] from [CR$]", BETWEEN Date and Date2 ? MyConnection);
4

1 回答 1

1

您可以使用标准 SQL 语法在两个日期之间进行查询,但您需要将日期作为日期参数分配给 OleDbDataAdapter。否则,它将日期视为文本,您将无法获得正确的匹配。

DataSet ds = new DataSet();

using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(SQL, conn))
{
    // Format the dates as dd/mm/yyyy

    string date1 = DateTime.Now.AddDays(7).ToString("dd/MM/yyyy");
    string date2 = DateTime.Now.ToString("dd/MM/yyyy");

    // Set up the SQL with the two date paramters

    string SQL = "SELECT [Req Start Date] FROM [CR$] WHERE [Req Start Date] BETWEEN ? AND ?";

    // Assign the two dates

    dataAdapter.SelectCommand.Parameters.Add("@p1", OleDbType.Date).Value = date1;
    dataAdapter.SelectCommand.Parameters.Add("@p2", OleDbType.Date).Value = date2;

    // Run the query

    dataAdapter.Fill(ds);
}
于 2012-11-22T01:44:24.940 回答