Problem with datetimes in SQL CE.Need help.

J

jorgeo

Hello,
I'm having an annoying error. I try inserting
any datetime into the database but I always get the same
error. The code is quite simple:

// My ExecuteNonQuery function open the conection and
exucute the
//command.
ExecuteNonQuery(string.Format("insert into Tasks
(IDProject,IDTask,Date) "+
"values ({0},{1},'{2}')",
70,70,DateTime.Now));

The message error I have is :

There is an error in the datepart format.
[,,,Expresion,,,]

I have tried changing the value, converting to string,
converting to smalldatetime, and so on ... but I continue
having the same message.

I'd appreciate any help:

jorge ochoa.

I'm using:
VS 2003
Sql CE 2.0
The talbe task is like this in my database:
IDProject int
IDTask int
Date datetime
 
W

WSM

Hi,

A newbie to .NET CF, may be my comments shall help.

Instead of using "+" for string concatenation, use ampersand "&" in your
query string. I believe, it should work now.

WSM
 
J

jayderk

I think before you run a query like this you should see if the string is a
valid string to insert into the database.
string stringData = "insert into Tasks values(IDProject,IDTask,Date)
values(20,20,"'" + DateTime.Now.ToString + "'")";
ExecuteNonQuery(stringData);
 
C

chris-s

The method I use, which never fails is to pass the date as a string inside
single quotes formatted as "yyyy-M-d". If you want the time as well, use
"yyyy-M-d hh:mm:ss".


eg string sql = string.format("insert into mytableofdates values('{0}')",
mydatetime.ToString("yyyy-M-d") );


Chris


"insert into my_table
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top