[英]insert in to table gives back ORA-01843: not a valid month
I need to insert a row in my table in oracle from C# (Windows Forms) 我需要从C#(Windows Forms)在oracle的表中插入一行
conn.Open();
OracleCommand cmd = new OracleCommand("INSERT INTO RECIPES(id,name,time_cooking,time_prep,price,directions,user_name,submit_timestamp) VALUES (:id, :name, :time_cook, :time_prep, :price, :directions, :user_name, :sub_time)",conn);
cmd.Parameters.AddWithValue(":id",x);
cmd.Parameters.AddWithValue(":name",textBox10.Text);
cmd.Parameters.AddWithValue(":time_cook", textBox9.Text);
cmd.Parameters.AddWithValue(":time_prep",textBox8.Text);
cmd.Parameters.AddWithValue(":price", textBox6.Text);
cmd.Parameters.AddWithValue(":directions",richTextBox2.Text);
cmd.Parameters.AddWithValue(":user_name",this.username);
cmd.Parameters.AddWithValue(":sub_time",DateTime.Now.ToString("MM/dd/yyyy"));
try
{
cmd.ExecuteNonQuery();
}
catch (OracleException ex)
{
MessageBox.Show(ex.ToString());
}
conn.Close();
I get the error below. 我得到下面的错误。
ORA-01843: not a valid month
ORA-01843:无效月份
I checked in oracle : 我检查了oracle:
select *
from nls_session_parameters;
and returned NLS_DATE_FORMAT mm/dd/yyyy 并返回NLS_DATE_FORMAT mm / dd / yyyy
You shouldn't pass the date as a string; 您不应该将日期作为字符串传递; you should pass it as a date instead... just remove the
ToString
call: 您应该改为将其作为日期传递...只需删除
ToString
调用即可:
cmd.Parameters.AddWithValue(":sub_time", DateTime.Now);
If you pass a string, it will be parsed using the format specified by the nls_date_format
parameter in the database. 如果传递字符串,则将使用数据库中
nls_date_format
参数指定的格式来解析该字符串。 If the format you pass is not the one the database expects, the parsing will fail. 如果您传递的格式不是数据库期望的格式,则解析将失败。 By passing the date itself, you don't need to worry about the format.
通过传递日期本身,您无需担心格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.