简体   繁体   English

如何使用Oracle连接从asp.net C#Webform将日期插入Oracle表?

[英]How to insert date into Oracle table from asp.net C# webform using an Oracle connection?

I have a table which I had created using Toad. 我有一个使用Toad创建的表。 This has a field called created which is going to store the date of creation, so I need to insert the date of creation from the code behind using C# and an Oracle Connection. 这有一个称为created的字段,它将存储创建日期,因此我需要使用C#和Oracle Connection从后面的代码中插入创建日期。

But I am unable to insert the date. 但是我无法输入日期。 While doing so it's throwing the exception ORA-01843: not a valid month and when I try to use the to_date function it's showing that to_date couldn't be found in the current context in Microsoft Visual Studio. 这样做会to_date异常ORA-01843:不是有效月份 ,当我尝试使用to_date函数时,它表明在Microsoft Visual Studio的当前上下文中找不到to_date

I used the following code: 我使用以下代码:

DateTime dt = DateTime.Today;
.
.
.
cmd.CommandText = "insert into Employee (BADGE_ID, USER_ID, FNAME, LNAME,PLANNED_ALLOC, MANAGER, TEAM,CREATED,CREATED_BY,LAST_UPD,LAST_UPD_BY) values ( '" + bid + "', '" + uid + "', '" + fn + "', '" + ln + "', " + pa + ", '" + man + "', '" + team + "', '" + TO_DATE(dt.ToString(), "yyyy/mm/dd hh24:mi:ss") + "', '" + uid + "', '" + TO_DATE(dt.ToString(), 'yyyy/mm/dd hh24:mi:ss') + "', '" + uid + "')";

So, in this case, I would say let the database do the work. 因此,在这种情况下,我要说让数据库完成工作。 Use the GETDATE() function in your SQL statement and the server will format a full timestamp and stick it in there. 在SQL语句中使用GETDATE()函数,服务器将格式化完整的时间戳并将其粘贴在其中。

The "TO_DATE" bit is part of the PL/SQL (you've got it as part of your c# command), so it should be part of the "CommandText" string. “ TO_DATE”位是PL / SQL的一部分(您已将其作为c#命令的一部分),因此它应该是“ CommandText”字符串的一部分。

So you want something like this: 所以你想要这样的东西:

    cmd.CommandText = "insert into Employee (BADGE_ID, USER_ID, FNAME, LNAME,PLANNED_ALLOC, MANAGER, TEAM,CREATED,CREATED_BY,LAST_UPD,LAST_UPD_BY) values ( '" + bid + "', '" + uid + "', '" + fn + "', '" + ln + "', " + pa + ", '" + man + "', '" + team + "', '" + TO_DATE(dt.ToString(), "yyyy/mm/dd hh24:mi:ss") + "', '" + uid + "',  TO_DATE(dt.ToString(), 'yyyy/mm/dd hh24:mi:ss') + ', '" + uid + "')";

[Notice I've removed the " (speech marks) which takes the TO_DATE out of the actual command string]. [注意,我删除了将实际TO_DATE移出实际命令字符串的“(语音标记)]。

So it's got to be in the string that's "handed" to Oracle if you see what I mean. 因此,如果您明白我的意思,就必须放在“交给Oracle”的字符串中。

Kind regards, Mike 亲切的问候,迈克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM