我正在尝试使用timeptamp(6)列在Oracle数据库表上执行查询,使用odp.net
我正在使用下面的代码,它会抛出此异常:“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,但我得到了同样的错误。
答案 0 :(得分:2)
我通过传递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
}
}