简体   繁体   English

插入表中会返回ORA-01843:无效月份

[英]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.

相关问题 如何解决SQL错误:ORA-01843:无效月份 - How to solve SQL error: ORA-01843: not a valid month ORA-01843:无效月份-在第二台PC上发行 - ORA-01843: not a valid month - issue on second pc 在SELECT中计算日期时得到“ ORA-01843:无效的月份” - Getting “ORA-01843: not a valid month” when calculating dates in SELECT Oracle.ManagedDataAccess.Client.OracleException:“ORA-01843:无效月份” - Oracle.ManagedDataAccess.Client.OracleException: "ORA-01843: not a valid month" ORA-01843:无效月份-在DB上工作,但在asp.net网页上进行相同操作时却无效 - ORA-01843: not a valid month — working on DB but not when doing the same on asp.net web pages 为什么将datetime转换为字符串会导致错误:ORA-01843:无效的月份 - Why converting datetime to string gets me the error : ORA-01843: not a valid month 如何将字符串类型查询的特定部分转换为最新日期,以避免出现“ ORA-01843:无效月份”错误? - How can I cast specific part of string type query to date to avoid “ORA-01843: not a valid month” error? 当我尝试向Oracle插入日期和时间时得到ORA-01843 - got ORA-01843 when I try to insert date & time to Oracle 远程服务器向 Oracle 提供无效月份错误 - Remote Server Gives Oracle Not a Valid Month Error 语句返回Oracle错误ORA-00984列,此处不允许 - statement gives back Oracle error ORA - 00984 column not allowed here
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM