查询datetime数据类型至今

时间:2019-10-31 08:56:59

标签: c# sql ms-access

我正在尝试使用CAST从访问数据库中加载带有查询的数据以将数据类型更改为c#中的日期,但是当我在文本框中键入要输入参数日期的参数时,它给我ierrorinfo.getdescription失败,出现e_fail

代码

 connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            string start = textBox1.Text;
            string end = textBox2.Text;
            string query = "SELECT dbo_RSWORKORDERCUR.workordercurID,dbo_RSWORKORDERCUR.workordercode,CAST(dbo_RSWORKORDERCUR.closeDtime AS DATE) AS [Close Date],CAST(dbo_RSWORKORDERCUR.RequiredbyDtime AS DATE) AS [Required By Date] " +
                "FROM dbo_RSWORKORDERCUR1 " +
                "WHERE (((CAST(dbo_RSWORKORDERCUR.closeDtime AS DATE))>=@start And (CAST(dbo_RSWORKORDERCUR.closeDtime AS DATE))<=@end));";
            command.Parameters.Add(new OleDbParameter("@start", start));
            command.Parameters.Add(new OleDbParameter("@end", end));
            command.CommandText = query;
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt5 = new DataTable();
            da.Fill(dt5);
            dataGridView1.DataSource = dt5;
            connection.Close();

1 个答案:

答案 0 :(得分:0)

我认为没有理由强制使用这些值。处理日期和时间时,请使用 DateTime

        DateTime start = DateTime.Parse(textBox1.Text);
        DateTime end = DateTime.Parse(textBox2.Text);
        string query = "SELECT dbo_RSWORKORDERCUR.workordercurID,dbo_RSWORKORDERCUR.workordercode,dbo_RSWORKORDERCUR.closeDtime, dbo_RSWORKORDERCUR.RequiredbyDtime " +
            "FROM dbo_RSWORKORDERCUR1 " +
            "WHERE dbo_RSWORKORDERCUR.closeDtime >= @start And dbo_RSWORKORDERCUR.closeDtime <= @end;";
        command.Parameters.Add(new OleDbParameter("@start", OleDbType.Date));
        command.Parameters.Add(new OleDbParameter("@end", OleDbType.Date));
        command.Parameters["@start"].Value = start;
        command.Parameters["@end"].Value = end;