-1

I have Date Var in Oracle, and I try to insert Data from my C# program

sql = "insert into Table(MyDate) values (" + convert.todatetime(txt) + ")";

I get an Error, what can i do ?

2
  • 2
    Please also post the text of the actual error, else it is quite hard to help you. Commented Mar 30, 2009 at 18:08
  • 1. What error do you get? 2. What is the value of txt. 3. Why are you converting the string txt to a DateTime only to convert it back to a string again? 4. The default format for a DateTime is probably not the correct format for Oracle to parse, you may need to use .ToString() on the DateTime to get it into the correct format. Commented Mar 30, 2009 at 18:10

7 Answers 7

13
cmd.CommandText = "INSERT INTO Table (myDate)VALUES(:dateParam)";

cmd.Parameters.Add(new OracleParameter("dateParam", OracleDbType.Date))
    .Value = DateTime.Now;

cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

2 Comments

I think OracleType is meant to be OracleDbType, and I think .DateTime is meant to be .Date. I do not have OracleType.DateTime in my intellisense using the Oracle 12c DLL ODP for .NET, client assembly Oracle.ManagedDataAccess.dll version 4.121.2.
Also, you don't put the : on the name of the OracleParameter, when you add it. The OP was also trying to use an exact date (txt), not DateTime.Now. This doesn't answer that question.
8

Use parameters. It's going to solve your problem and prevent injection.

2 Comments

Rather than just saying this, should show an example using the OP's code.
I don't understand why this answer got that many upvotes. This should be a comment, because it neither shows code nor does it explain a possible approach.
6

Oracle expects it to be an actual date value, not just a string that looks like a date. You have to use the TO_DATE() function to explain how your string is formatted, something like this:

INSERT INTO Table (myDate)
VALUES(TO_DATE('2009-03-30 12:30:00', 'YYYY-MM-DD HH:mi:ss'));

1 Comment

That's not strictly true. If you insert the date in the format DD-MMM-YYYY, Oracle (at least 10g) accepts it as is.
1

Try using DateTime.TryParse(text) or DateTime.Parse(text)

2 Comments

This, alone, will not work - at least for me. I did DateTime myDate = DateTime.Parse("1/1/1900"); and sent that into an OracleParameter: OraCommand.Parameters.Add("@"+fieldName, OracleDbType.Date).Value = myDate; - received ORA-01840: input value not long enough for date format\nORA-06512: at line 1. Something more is needed than just this.
Actually, no, I was wrong. DateTime.Parse("1/1/1900"); was fine, and so was my OracleParameter. I would delete my previous comment, but this might actually help someone: my issue was actually because I had my parameters in my stored procedure out of order from what I was sending in, in my C#, so what I thought was being set to my date field really was being recorded into a different input parameter in my stored procedure, and set to a different field. My date field was getting just an empty string from another input parameter's value, instead of the parsed date. Moral: check order.
1

I know this was a poorly asked question, but I saw some poor answers when I had the same question and ran into this. This is how I solved it, and I'll answer using the OP's context:

Parse the date in to a DateTime variable:

DateTime myDate = DateTime.Parse(txt);

Then parameterize your query:

sql = "insert into Table(MyDate) values (:myDate)";

Set up an OracleParameter:

OracleParameter param = new OracleParameter();
param.ParameterName = "myDate";
param.OracleDbType = OracleDbType.Date;
param.Value = myDate;

Assuming you already have an OracleConnection as connection, set up your command and add your parameter:

OracleCommand cmd = new OracleCommand(sql, connection);
cmd.Parameters.Add(param);

Execute:

cmd.ExecuteNonQuery();

Do NOT waste your time on any of the TO_DATE nonsense. This is for when you are adding something using SQL*Plus or Oracle SQL Developer directly, or MAYBE where you want to send in a STRING variable's value (not a DateTime variable) in the EXACT format that TO_DATE expects and that you assign within the TO_DATE construct within your query or a stored procedure (i.e. to_date('2013-05-13 12:13:14', 'YYYY-MM-DD HH24:MI:SS'). Using a DateTime variable and assigning that to an OracleParameter with an OracleDbType of OracleDbType.Date, assuming you have a DATE field in your table and can parse txt into a DateTime variable, however, is best and easiest.

Comments

1

Easiest way possible:

DateTime inputDate = Convert.ToDateTime("01/01/2019"); //<---Input Sample Date in format

string queryParameters = String.Format("SELECT * FROM TABLE WHERE DATE = '{0}')", inputDate.ToString("dd-MMM-yyyy")); //<-- Converts System.DateTime into Oracle DateTime

//Forget looking anywhere else for an answer, copy and paste and reform this very code 
//and see the results

Comments

0

Please bind your variables (like ocdecio tells) ! Not only does it prevent sql injection it is also much faster. Especially in a multi concurrency situation. Read for example here: http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28844/building_odp.htm#CEGCGDAB .

"Bind variables are placeholders inside a SQL statement. When a database receives a SQL statement, it determines if the statement has already been executed and stored in memory. If the statement does exist in memory, Oracle Database can reuse it and skip the task of parsing and optimizing the statement. Using bind variables makes the statement reusable with different input values. Using bind variables also improves query performance in the database, eliminates the need for special handling of literal quotation marks in the input, and protects against SQL injection attacks."

1 Comment

the title was about "How to insert date"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.