views:

745

answers:

4

hi

I have A anb B in String format

A = 14/01/2007

B = 22:10:39

I try to insert date and time:

SQL = "insert into MyTbl(Tdate,Ttime) value ('" + Convert.ToDateTime(A) + "','" + Convert.ToDateTime(B) + "')";

i got ORA-01843 error, what I can do ?

thank's in advance

+4  A: 

Don't use raw SQL to insert values. Use a parameterized query instead. Parse your strings into .NET DateTime (or DateTimeOffset) and TimeSpan values in the normal way, and then use something like:

string sql = "insert into MyTbl(Tdate,Ttime) values (:date, :time)";
using (OracleCommand cmd = new OracleCommand(sql, connection))
{
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Add("date", OracleType.DateTime).Value = date;
    cmd.Parameters.Add("time", OracleType.IntervalDayToSecond).Value = time;
    cmd.ExecuteNonQuery();
}

(Obviously adjust for the types of your actual fields.)

Jon Skeet
+1 for bind variables! Also, it's silly to store date and time in two separate fields, as was mentioned elsewhere.
Rob
A: 

Convert the Strings within C# into a DateTime format and then use the ToString()-Method of that Object.

DateTime aDat = DateTime.ParseExact("14/01/2007", "dd/MM/yyyy", Null);
DateTime bDat = DateTime.ParseExact("22:10:39", "HH:mm:ss", Null);
SQL = "insert into MyTbl(Tdate,Ttime) value ('" + aDat.ToString("put the Orcale preferred DateFormat here") + "','" + aDat.ToString("put the Orcale preferred TimeFormat here") + "')";

The problem is that cyou're doing there an implicit conversion, were Orcale ends up thinking that 14 is the month.

Or use parameterized queries.

Bobby

Bobby
Embedding values directly is always risky compared with getting the driver to do the conversion for you. Parameterized queries are the way to go.
Jon Skeet
It's not necesserily risky...you've to check every conversion from Object to String yourself for the right format, and if you know how to do that, it's no problem.But I totally agree with you, parameterized queries, especially with reoccurring queries, should be preferred.
Bobby
You don't just have to check the conversion once - you've got to then worry about it changing if either your .NET machine *or* your Oracle server changes culture. It's just a bad idea, IMO... at least for something as easy to foul up as dates and times. Integers and strings are slightly less troublesome.
Jon Skeet
+1  A: 

Remember that Oracle doesn't have a time-only field.

You're trying to insert a time-only field into a datetime. My guess is that the CLR is turning B into 00/00/00 22:10:39, which isn't a valid oracle date. For example:

SQL> select to_date('00/00/00', 'MM/DD/YY') from dual;
select to_date('00/00/00', 'MM/DD/YY') from dual
               *
ERROR at line 1:
ORA-01843: not a valid month

Either way, Convert.ToDateTime(B) probably isn't returning the right thing.

Also, this:

"insert into MyTbl(Tdate,Ttime) value ("

should be this:

"insert into MyTbl(Tdate,Ttime) values ("

...but I'm guessing that's just a typo here.

Jason Baker
+2  A: 

The error is due to the month, try:

TO_DATE(A, 'DD/MM/YYYY')

Danny