我正在尝试使用 odp.net 对具有 TimeStamp(6) 列的 Oracle 数据库表执行查询
我使用下面的代码,它抛出此异常:“ORA-01840:输入值对于日期格式来说不够长”。
OracleConnection con = new OracleConnection(connStr");
using (con)
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandText = "SELECT * FROM Logs WHERE LOGDATE > :logDate";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("logDate", OracleDbType.TimeStamp, DateTime.Now.AddMonths(-1), ParameterDirection.Input));
using (command)
{
OracleDataReader rdr = command.ExecuteReader(); //ORA-01840 exception is thrown here
}
}
此查询及其参数有什么问题?我还尝试了 OracleDbType.Date,而不是 OracleDbType.TimeStamp,但出现了相同的错误。
最佳答案
我通过传递 OracleTimeStamp 对象而不是 DateTime 对象解决了该问题。下面的代码效果很好:
OracleConnection con = new OracleConnection(connStr");
using (con)
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandText = "SELECT * FROM Logs WHERE LOGDATE > :logDate";
command.CommandType = System.Data.CommandType.Text;
command.Parameters.Add(new OracleParameter("logDate", OracleDbType.TimeStamp, new OracleTimeStamp(DateTime.Now.AddMonths(-1)), ParameterDirection.Input));
using (command)
{
OracleDataReader rdr = command.ExecuteReader(); //ORA-01840 exception was thrown here
}
}
关于oracle - ORA-01840 : input value not long enough for date format when executing a query with a timestamp parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33777761/