how to convert DateTime in short date and short time

O

Omer kamal

Dear all,

I want to save my data into MS Access database Table which consist of
some fields with short date and short time formats.
I try to save data in to the said table and I am getting error that my
SQL statment is not correct.

string strSQL = @"INSERT INTO Activities ( Task,Day,Start,Finish )
VALUES ( '"+id+"', '"+date+"', '"+start+"', '"+finish+"' )" ;

I think error is because i am not able to cast my variables correctly.

how can i convert my DateTime to short date and short Time formats???

regards,

Omer kamal
 
J

Jon Skeet [C# MVP]

Omer kamal said:
I want to save my data into MS Access database Table which consist of
some fields with short date and short time formats.
I try to save data in to the said table and I am getting error that my
SQL statment is not correct.

string strSQL = @"INSERT INTO Activities ( Task,Day,Start,Finish )
VALUES ( '"+id+"', '"+date+"', '"+start+"', '"+finish+"' )" ;

I think error is because i am not able to cast my variables correctly.

how can i convert my DateTime to short date and short Time formats???

The trick is not to embed the values directly in the SQL in the first
place. Instead, use aparameterised command. See
http://www.pobox.com/~skeet/csharp/faq/#db.parameters
 
P

Paul Sullivan

You should also be aware that date values are stored differnrtly in MS
Access, but the libray has methoids to convert both ways.

See:
DateTime.ToOADate Method
DateTime.FromOADate Method

Paul V. Sullivan
 

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