CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2005
    Posts
    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

  2. #2
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    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.

  3. #3
    Join Date
    Aug 2003
    Posts
    102

    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
  •  





Click Here to Expand Forum to Full Width

Featured