how can save DateTime c# language to oracle10g database

徘徊边缘 提交于 2019-12-13 08:27:53

问题


there is a table== create table newstudent(enroll int, name varchar2(20), DoJoin date); in c# when i type

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

回答1:


One of your problems may be the fact that your date is in the wrong format for Oracle.

Oracle accepts the yyyy/mm/dd date format which I can see you're not using.

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.

You can use the to_date function to convert your date into the correct format.

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




回答2:


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();
  }
}



回答3:


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:

command.Parameters.Add(":date", OracleDbType.DateTime).Value = ...;



回答4:


Did you check standard date format



来源:https://stackoverflow.com/questions/1218326/how-can-save-datetime-c-sharp-language-to-oracle10g-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!