C# INSERT statement using OleDb

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themoonisdown09
    New Member
    • Jul 2006
    • 11

    C# INSERT statement using OleDb

    I am using Visual Studio 2003, C# .NET

    I can't get this INSERT statement to work. I can get an UPDATE to work, but not this INSERT. Here is my code...

    *************** ***
    Code:
    //opens database
    string conString = "Provider=Microsoft.Jet.OLEDB.4.0;"
    			+ "Data Source=C:\\TimeClock\\TimeClock.mdb";
    
    OleDbConnection empConnection = new OleDbConnection(conString);
    			
    //add time clocked in/out in clock table
    string insertStatement = "INSERT INTO Clock "
    				     + "(Login, Time, Status) "
    				     + "VALUES (?, ?, ?)";
    
    OleDbCommand insertCommand = new OleDbCommand(insertStatement, empConnection);
    
    insertCommand.Parameters.Add("Login", OleDbType.Char).Value = strLogin;
    insertCommand.Parameters.Add("Time", OleDbType.Char).Value = strTime;
    insertCommand.Parameters.Add("Status", OleDbType.Char).Value = strStatus;
    
    empConnection.Open();
    			
    try
    {
    int count = insertCommand.ExecuteNonQuery();
    }
    catch (OleDbException ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    empConnection.Close();
    }
    *************** **
    Last edited by Frinavale; Nov 19 '09, 04:30 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • axas
    New Member
    • Jul 2006
    • 32

    #2
    You must write
    Code:
    string insertStatement = "INSERT INTO Clock "
    + "(Login, Time, Status) "
    + "VALUES (@Login, @Time, @Status)";
    
    insertCommand.Parameters.Add("@Login", OleDbType.Char).Value = strLogin;
    insertCommand.Parameters.Add("@Time", OleDbType.Char).Value = strTime;
    insertCommand.Parameters.Add("@Status", OleDbType.Char).Value = strStatus;
    I think its right
    Last edited by Frinavale; Nov 19 '09, 04:30 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

    Comment

    • themoonisdown09
      New Member
      • Jul 2006
      • 11

      #3
      It's still saying that there is a syntax error in the INSERT INTO statement. I don't understand. I have a book that shows me how to do it... and I tried AXAS code... and it still says that.

      Comment

      • Enyi
        New Member
        • Jul 2006
        • 38

        #4
        I tried making a program with your code, and i got the same error.

        Here is your problem:

        The word 'Time' is a keyword in MS Access 2003, and most likely other versions as well. Therefore, it cannot be used as a column name.

        I havn't tried with a different column name for time, but i'm sure it would work.

        Good luck!


        EDIT: Actually, I'm just thinking. How can update work in this situation? Now I'm questioning myself. I'll just try the program again.
        Last edited by Enyi; Jul 28 '06, 09:01 AM. Reason: Some addition

        Comment

        • Enyi
          New Member
          • Jul 2006
          • 38

          #5
          OK, I just tried changing the column name and it worked just fine with both an UPDATE and INSERT INTO command.

          However, when I tried with 'Time' as the column name in an UPDATE statement, it just said "Syntax error in UPDATE statement".

          Not sure how you got update to work, but I can't :P ( With a column named 'Time' )

          Of course, I'm using MS Access 2003, maybe your verison is different?

          Enyi

          Comment

          • themoonisdown09
            New Member
            • Jul 2006
            • 11

            #6
            I didn't use an UPDATE with this exact statement. It was with another part of my program. I'll try and change the column name when I get home... I'm at work now... ha. Thanks so much for your help... I'll let you know if it works for me too.

            By the way... I am using Microsoft Access 2003.

            Comment

            • Elena
              New Member
              • Jul 2006
              • 3

              #7
              try
              Code:
              string insertStatement = "INSERT INTO Clock "
              + "([Login], [Time], [Status]) "
              + "VALUES (@Login, @Time, @Status)";
              Last edited by Frinavale; Nov 19 '09, 04:31 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

              Comment

              • themoonisdown09
                New Member
                • Jul 2006
                • 11

                #8
                THANK YOU ELENA!!! I changed the column name and I also put the brackets around the names.... don't know which thing helped, but it works now.

                Comment

                • Enyi
                  New Member
                  • Jul 2006
                  • 38

                  #9
                  Very nice Elena.

                  I seen what it does it now. The square brackets explicitly refers to the column name, whereas without square brackets, it could be referring to a column or function or something else. Right?

                  I would also say in our cases, both helped :D

                  Comment

                  • dna5122
                    New Member
                    • Jul 2006
                    • 12

                    #10
                    You are correct Anyi.

                    Comment

                    • TracyM
                      New Member
                      • Jul 2006
                      • 3

                      #11
                      I came across this same problem with Access and Visual Studio. Visual Studio does not support read and writes in its Adapter to Oledb. Its not bi-directional. Just read only. I know its a major pain. Its the last thing I expected also. I suppose it forces you to use Sqlserver. There are other sources out there for MySql and some other databases but you may not have time.

                      You have a couple choices here. If available switch to SqlServer DB or try another method involving manipulation of Access:

                      Take a look at this link and see if it helps.

                      http://www.geekpedia.c om/tutorial158_Con nect-to-Access-Database-in-Visual-Studio-.NET.html

                      Best of Luck,
                      TracyM

                      Comment

                      • gts2277
                        New Member
                        • Apr 2007
                        • 1

                        #12
                        string insertStatement = "INSERT INTO Clock "
                        + "([Login], [Time], [Status]) "
                        + "VALUES (@Login, @Time, @Status)";

                        with the brack is WORK !!!!! and save your time use @ infront of VALUES. Like:

                        string insertStatement = "INSERT INTO Clock "
                        + "([Login], [Time], [Status]) "
                        + @"VALUES Login,Time,Stat us)";

                        Comment

                        Working...