Results 1 to 9 of 9

Thread: mysql insert command

  1. #1
    hawk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2008
    Posts
    8
    Reputation
    0

    Unhappy mysql insert command

    Hello everyone,

    I'm having great difficulty inserting information into my database.

    I'm hoping to insert into the table test and field alt the information in my textbox txtalt, but I just get the @alt inserted.

    Any Ideas?

    thanks

    Code:
            Dim myConnString = ";" & _
        "Database=;" & _
        "Uid=;" & _
        "Pwd=;" & _
        "Connect Timeout=30;"
    
    
            Dim myconnection As New MySqlConnection(myConnString)
            Dim command As New MySqlCommand("INSERT INTO test (alt) VALUES ('@alt')", myconnection)
    
            command.Parameters.AddWithValue("@alt", txtalt)
    
            myconnection.Open()
            command.ExecuteNonQuery()
            myconnection.Close()

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,588
    Reputation
    2737
    '@alt' is a string in query, @alt is a parameter in query, if you could understand the difference...

  3. #3
    hawk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2008
    Posts
    8
    Reputation
    0
    Hello John,

    I'm very new with vbnet.

    I think most of what I've acheived with my script has been fairly simple but this has stumped me. It took a day to get anything to insert into the database so I asked for help from those who know. I'm very keen to learn

  4. #4
    cjard's Avatar
    cjard is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2006
    Posts
    7,036
    Reputation
    1722
    i thought MySQL used parameters that looked like:

    ?name

    INSERT INTO table VALUES (?col1, ?col2)


    Note to hawk: notice that i didnt put string marks around my parameter name ' ' !! You define the parameter as being of string type, you dont need to / shouldnt try and tell mysql its a string, by putting the parameter name in a string. as an example here is some vb:

    Dim myNum as Integer = 7
    MessageBox.Show("myNum + 3")

    What does the messagebox show? Please don't say 10

  5. #5
    hawk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2008
    Posts
    8
    Reputation
    0
    ok what you say makes some sense ( please be gentle I'm new to this )

    so to insert the textbox txtalt I should use ?txtalt?

  6. #6
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    588
    Quote Originally Posted by hawk View Post
    ok what you say makes some sense ( please be gentle I'm new to this )

    so to insert the textbox txtalt I should use ?txtalt?
    Code:
            Dim command As New MySqlCommand("INSERT INTO test (alt) VALUES (?alt)", myconnection)
    
            command.Parameters.AddWithValue("?alt", txtalt.text)

  7. #7
    hawk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2008
    Posts
    8
    Reputation
    0
    Thank you for your help, that works perfect

    I'll be back

    thanks again, this is the most helpful resource out there for learners

  8. #8
    hawk is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Sep 2008
    Posts
    8
    Reputation
    0
    Hello guys,

    Been working on this and got some good results, again thanks for all the help provided.

    Now I want to update a record where a text field equals a database entry.

    ie update a field in a row

    I get this error

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE (taskref) = ('666')' at line 1

    any thoughts?

    Thanks Again

    Code:
            Dim myconnection As New MySqlConnection(myConnString)
    
            Dim command As New MySqlCommand("INSERT INTO test (end) VALUES (?end) WHERE (taskref) = (?taskref)", myconnection)
    
            command.Parameters.AddWithValue("?end", txtQuantity.Text)
            command.Parameters.AddWithValue("?taskref", txttaskref.Text)
    
            myconnection.Open()
            command.ExecuteNonQuery()
            myconnection.Close()

  9. #9
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,206
    Reputation
    588
    You're using an INSERT statement to do an UPDATE on the table.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking