oracle 如何将DateTime c#语言保存到oracle10g数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1218326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can save DateTime c# language to oracle10g database
提问by Ahmed Said
there is a table== create table newstudent(enroll int, name varchar2(20), DoJoin date); in c# when i type
有一个表== 创建表newstudent(enroll int, name varchar2(20), DoJoin date); 在 c# 中,当我输入时
oraclecommand(1,'amit purohit', DateTime.Now());
//Error found 'nvalid month'
oraclecommand(1,'amit purohit', String.Format("{0:dd-MMM-yyyy}"));
//problme that save only date into database but i need time also. like '11-AUG-2009 11:22:32.0'
oraclecommand(1,'amit purohit', String.Format("{0:dd-MMM-yyyy hh:mm:ss}")
//also error get like string picture format in date time values
回答by mlevit
One of your problems may be the fact that your date is in the wrong format for Oracle.
您的问题之一可能是您的日期对于 Oracle 的格式错误。
Oracle accepts the yyyy/mm/dddate format which I can see you're not using.
Oracle 接受yyyy/mm/dd日期格式,我可以看到您没有使用。
I'm with Jon Skeet on this one for the oraclecommand but you maybe want to try creating a normal SQL Query inside a string and then running the query.
我和 Jon Skeet 一起讨论了 oraclecommand,但您可能想尝试在字符串中创建一个普通的 SQL 查询,然后运行该查询。
You can use the to_date function to convert your date into the correct format.
您可以使用 to_date 函数将日期转换为正确的格式。
insert into table_name
(date_field)
values
(to_date('2003/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'));
(Via Standard Date and Time Format Strings)
(通过标准日期和时间格式字符串)
Or on the other hand you may try converting the date to a proper Oracle accepted format using some C# functions: Oracle/PLSQL: Insert a date/time value into an Oracle table
或者另一方面,您可以尝试使用一些 C# 函数将日期转换为适当的 Oracle 接受的格式:Oracle/PLSQL: Insert a date/time value into an Oracle table
回答by Jon Skeet
You should use a parameterized statement, and add a parameter with the relevant data in, specifying the appropriate parameter type. The exact format of that will depend on which Oracle driver you're using, but it's likely to be something like:
您应该使用参数化语句,并添加带有相关数据的参数,指定适当的参数类型。其确切格式将取决于您使用的 Oracle 驱动程序,但它可能类似于:
command.Parameters.Add(":date", OracleDbType.DateTime).Value = ...;
回答by pero
Try something like this (non-tested code):
尝试这样的事情(未经测试的代码):
public void InsertDateTime(DateTime aoDateTime) {
using (var oracleConnection = new OracleConnection("<my connection string>"))
{
var oracleCommand = new OracleCommand(
"INSERT INTO MY_TABLE (MY_DATE_FIELD) VALUES (:pMY_DATE_VALUE)",
oracleConnection);
oracleCommand.Parameters.Add(
new OracleParameter(":pMY_DATE_VALUE", aoDateTime));
oracleConnection.Open();
oracleCommand.ExecuteNonQuery();
}
}
回答by Ahmed Said
Did you check standard date format
您是否检查了标准日期格式