|
-
September 15th, 2005, 01:06 AM
#1
OleDb Date Time insertion Problem
Hi,
I'm having problems inserting datetime into and access database as shown below
string insert = "insert into " + TableName + " (Name,Location,Date) ";
insert += " values('" + row["Name"] + "','" + row["Location"];
insert += "','#2/2/2004#')";
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand(insert,this.Connection);
cmd.ExecuteNonQuery();
or even with this
System.Data.OleDb.OleDbCommand cmd = database.CreateCommand();
cmd.CommandText = "insert into "+ TableName +" (Name,Location,Date)"+" values(?,?,?)";
cmd.Parameters.Add("Name",System.Data.OleDb.OleDbType.VarChar,40,"Name");
cmd.Parameters.Add("Location",System.Data.OleDb.OleDbType.VarChar,40,"Location");
cmd.Parameters.Add("Date",System.Data.OleDb.OleDbType.Date,0,"Date");
in both cases I get "syntax error in insert into statement"
The Date field is defined as "General Date", but I get the same thing with "Short Date"
If I take the parts to do with the date it works
Help Please
Last edited by RMirenzi; September 15th, 2005 at 01:17 AM.
Reason: change title
-
September 20th, 2005, 05:48 AM
#2
Re: OleDb Date Time insertion Problem
I usually stick to String version of datetime when i insert data and SQL manipulates the string data to date format...
If you think you CAN, you can, If you think you CAN'T, you are probably right.
Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.
-
September 22nd, 2005, 10:17 PM
#3
Re: OleDb Date Time insertion Problem
cmd.CommandText = "insert into "+ TableName +" (Name,Location,Date)"+" values(?,?,?)";
cmd.Parameters.Add( "Name",System.Data.OleDb.OleDbType.VarChar,40,"Name");
cmd.Parameters.Add( "Location",System.Data.OleDb.OleDbType.VarChar,40,"Location");
cmd.Parameters.Add( "Date",System.Data.OleDb.OleDbType.Date,0,"Date");
parameters are wrong........ should be
cmd.Parameters.Add("?", OleDbType.Char, 25, "Name")
cmd.Parameters.Add("?", OleDbType.Char, 25, "Location")
cmd.Parameters.Add("?", OleDbType.Date, 7, "thedate")
Each parameter has to be exactly the same order as it is in your "insert into statement and one more thing.............. though not sure.... dont use date as a column name in your database I think it is an SQL keyword......
use "?" when u are using Oledb..... you can specify column names, only when you are using SqlClient namespace
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|