Insert date value - Access database

G

Guest

Hi,

I want to update ACCESS database, the field Date (DateTime format). I used
the following code:

DateTime dt;
string strInsertShift = "INSERT INTO MyTable (Date) values (@dt)";

OleDbCommand cmdS = new OleDbCommand(strMyDb, conn);
cmdS.Parameters.Add(new OleDbParameter("@dt", OleDbType.DBDate));
cmdS.Parameters["@dt"].Value = new DateTime(2004, 09, 01);

conn.Open();
int rowsReturnedS = cmdS.ExecuteNonQuery();
conn.Close();


I get the exception message: Syntax error in INSERT INTO statement.

I guess the problem is with the DateTime field, as when I use similar code
for other field with string values, it works fine.

Any suggestion, what is wrong?

Thanks,

Lubomir
 
M

Miha Markic [MVP C#]

Hi Lubomir,

Try with
string strInsertShift = "INSERT INTO MyTable (Date) values (?)";
 
M

Miha Markic [MVP C#]

Put Date column (reserved word) in square brackets :)
INSERT INTO MyTable ([Date]) values (?)

And, be wise, do not use such words for column names :)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Lubomir said:
Hi Miha,

Thanks for help, however, it doesn't work either. I get the same exception
error "Syntax error in INSERT INTO statement".

Well, it is again the situation, when everything is right, just it doesn't
work :)

Lubomir


Miha Markic said:
Hi Lubomir,

Try with
string strInsertShift = "INSERT INTO MyTable (Date) values (?)";

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Lubomir said:
Hi,

I want to update ACCESS database, the field Date (DateTime format). I
used
the following code:

DateTime dt;
string strInsertShift = "INSERT INTO MyTable (Date) values (@dt)";

OleDbCommand cmdS = new OleDbCommand(strMyDb, conn);
cmdS.Parameters.Add(new OleDbParameter("@dt", OleDbType.DBDate));
cmdS.Parameters["@dt"].Value = new DateTime(2004, 09, 01);

conn.Open();
int rowsReturnedS = cmdS.ExecuteNonQuery();
conn.Close();


I get the exception message: Syntax error in INSERT INTO statement.

I guess the problem is with the DateTime field, as when I use similar
code
for other field with string values, it works fine.

Any suggestion, what is wrong?

Thanks,

Lubomir
 
G

Guest

Oh, God! :))

Thanks.



Miha Markic said:
Put Date column (reserved word) in square brackets :)
INSERT INTO MyTable ([Date]) values (?)

And, be wise, do not use such words for column names :)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Lubomir said:
Hi Miha,

Thanks for help, however, it doesn't work either. I get the same exception
error "Syntax error in INSERT INTO statement".

Well, it is again the situation, when everything is right, just it doesn't
work :)

Lubomir


Miha Markic said:
Hi Lubomir,

Try with
string strInsertShift = "INSERT INTO MyTable (Date) values (?)";

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Hi,

I want to update ACCESS database, the field Date (DateTime format). I
used
the following code:

DateTime dt;
string strInsertShift = "INSERT INTO MyTable (Date) values (@dt)";

OleDbCommand cmdS = new OleDbCommand(strMyDb, conn);
cmdS.Parameters.Add(new OleDbParameter("@dt", OleDbType.DBDate));
cmdS.Parameters["@dt"].Value = new DateTime(2004, 09, 01);

conn.Open();
int rowsReturnedS = cmdS.ExecuteNonQuery();
conn.Close();


I get the exception message: Syntax error in INSERT INTO statement.

I guess the problem is with the DateTime field, as when I use similar
code
for other field with string values, it works fine.

Any suggestion, what is wrong?

Thanks,

Lubomir
 

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