Inserting data into a MySQL table using VB.NET

前端 未结 6 2040
别跟我提以往
别跟我提以往 2020-12-10 20:46

I have a problem inserting data into a table on a MySQL database using a VB.NET application. I have a simple form where when I set some data to the textboxes and I press a

6条回答
  •  抹茶落季
    2020-12-10 21:37

    • First, You missed this one: sqlCommand.CommandType = CommandType.Text
    • Second, Your MySQL Parameter Declaration is wrong. It should be @ and not ?

    try this:

    Public Function InsertCar() As Boolean
    
        Dim iReturn as boolean
        Using SQLConnection As New MySqlConnection(connectionString)
            Using sqlCommand As New MySqlCommand()
                With sqlCommand
                    .CommandText = "INSERT INTO members_car (`car_id`, `member_id`, `model`, `color`, `chassis_id`, `plate_number`, `code`) values (@xid,@m_id,@imodel,@icolor,@ch_id,@pt_num,@icode)"
                    .Connection = SQLConnection
                    .CommandType = CommandType.Text // You missed this line
                    .Parameters.AddWithValue("@xid", TextBox20.Text)
                    .Parameters.AddWithValue("@m_id", TextBox20.Text)
                    .Parameters.AddWithValue("@imodel", TextBox23.Text)
                    .Parameters.AddWithValue("@icolor", TextBox24.Text)
                    .Parameters.AddWithValue("@ch_id", TextBox22.Text)
                    .Parameters.AddWithValue("@pt_num", TextBox21.Text)
                    .Parameters.AddWithValue("@icode", ComboBox1.SelectedItem)
                End With
                Try
                    SQLConnection.Open()
                    sqlCommand.ExecuteNonQuery()
                    iReturn = TRUE
                Catch ex As MySqlException
                    MsgBox ex.Message.ToString
                    iReturn = False
                Finally
                    SQLConnection.Close()
                End Try
            End Using
        End Using
    
       Return iReturn
    
    End Function
    

提交回复
热议问题