How do I INSERT the values of variables into an Access table using SQL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Dransfield
    New Member
    • Jan 2011
    • 46

    How do I INSERT the values of variables into an Access table using SQL?

    I am trying to record certain incidents in my C# program, such as a user logging in or accessing the database.

    I am attempting to use something like

    Code:
    OleDbCommand writeLogs = new OleDbCommand("INSERT INTO [LOG] (username, firstName, lastName, logDetails) VALUES (user, fName, lName, details);", connection);
    but I get the error that "No value was given for one or more required parameters". The table that I am trying to insert the values into has Autonumber 'ID' followed by the username, firstName, lastName and logDetails fields.

    I apologise if this post is in the wrong category.
  • TheSmileyCoder
    Recognized Expert Moderator Top Contributor
    • Dec 2009
    • 2322

    #2
    I have not used anything outside access, to connect to access, but from what I can tell that SQL string is not valid for Access. (I will also admit I don't know about C and how it parses variables.)

    Anyways, my guess would be that you need to parse the string properly to make it function. The finished string would look something like this:
    Code:
    "INSERT INTO [LOG] (username, firstName, lastName, logDetails) 
                VALUES ('TSC', 'TheSmiley', 'Coder', 'Logging In');"

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      Essentially you currently have, in your SQL string, the names of the variables (I assume) that store the values you want entered. What you need to have is the values themselves within the string. Smiley has given examples of how it should look for string type values. Numeric values don't have any type of quotes but come just as they are. Does that make sense.

      Comment

      • Alex Dransfield
        New Member
        • Jan 2011
        • 46

        #4
        I've been trying to use the variable values, but not knowing the syntax for them is the problem I'm having unfortunately

        Comment

        • TheSmileyCoder
          Recognized Expert Moderator Top Contributor
          • Dec 2009
          • 2322

          #5
          You are programming in C#, are you saying that you don't know how to parse a string together in C#?

          I have given an example of how the finished string should look, to be in correct syntax (assuming that all your variables (user, fName, lName, details) are strings.

          Comment

          • Alex Dransfield
            New Member
            • Jan 2011
            • 46

            #6
            Ah, I understand what you mean now. I'm quite new to C# and programming in general, so I apologise!

            Comment

            • Alex Dransfield
              New Member
              • Jan 2011
              • 46

              #7
              How does this look?

              Code:
              string writeCommand = "INSERT INTO [LOG] (username, firstName, lastName, logDetails) VALUES ('" + user + "', '" + fName + "', " + lName + "', " + details + "');";
                          OleDbCommand write = new OleDbCommand(writeCommand, connection);
              I haven't been able to compile it yet as I have left my usb drive at home, but it seems like it should work.

              Thank you for your help by the way!

              Comment

              • TheSmileyCoder
                Recognized Expert Moderator Top Contributor
                • Dec 2009
                • 2322

                #8
                You forgot a single quote before lname and details, but from what I can see you have the correct syntax now.

                Whether the method work I don't know as I have never used C#.

                One thing to be aware of is that names with single quotes (Example: Mc'Grimy) should be properly escaped while parsing. OF the top of my head, I can't remember the escape charecter for access, but you can google that.

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  It's not an escape character as such. The character used as the quote character, if it occurs within the string (between the quotes), must be doubled up to ensure it is treated as a character rather than as indicating the end of the string.

                  EG. For a name such as O'Donnell :
                  Code:
                  'O''Donnell'

                  Comment

                  Working...