无法使用VB更新mysql中的数据库

时间:2014-10-23 03:00:44

标签: mysql vb.net

我正在尝试在VB中创建更新命令。我能够创建它并且没有显示错误。

但是当我尝试在调试期间执行命令时,更新似乎无法正常工作。我在执行期间没有收到任何错误,但我的数据库没有任何更改。

这是我的代码......有点长......适合那个......



connection = New MySqlConnection
        connection.ConnectionString = "Database=ngp;data source=localhost; user id= root; password=********"
        Dim reader As MySqlDataReader

        If MsgBox("Are you sure about the CHANGES you made to " & Label33.Text & " ? " & vbNewLine & "May prob pa sa result ng yesno", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Try
                connection.Open()
                Dim edit As String
                edit = "update ngpmain set ID='" & TextBox21.Text & "',PENRO='" & ComboBox1.SelectedItem & "',CENRO='" & ComboBox2.SelectedItem & "',MUNICIPALITY_or_CITY='" & ComboBox3.SelectedItem & "',BARANGAY='" & TextBox1.Text & "',PROVINCE='" & TextBox2.Text & "',AREA='" & TextBox3.Text & "',SEEDS_PLANTED='" & TextBox4.Text & "',NAME_OF_ORG='" & TextBox5.Text & "',CONTACT_PERSON='" & TextBox6.Text & "',TYPE_OF_ORG='" & ComboBox4.SelectedItem & "',COMPONENT='" & ComboBox5.SelectedItem & "',COMMODITY='" & ComboBox10.SelectedItem & "',SPECIES='" & TextBox8.Text & "',YEAR='" & ComboBox6.SelectedItem & "',ZONE='" & ComboBox7.SelectedItem & "',TENURE='" & TextBox9.Text & "',NO_LOA='" & ComboBox8.SelectedItem & "',RIVER_BASIN='" & TextBox10.Text & "',WATERSHED='" & TextBox11.Text & "',REMARKS='" & TextBox12.Text & "',sid='" & TextBox13.Text & "',sid2='" & TextBox14.Text & "',YR_CD='" & TextBox15.Text & "', PSGC_CD='" & TextBox16.Text & "',SITE_ID='" & TextBox17.Text & "',AREA_CD='" & TextBox18.Text & "', MALE_PLANTER='" & TextBox19.Text & "',FEMALE_PLANTER='" & TextBox20.Text & "',TOTAL_PLANTERS='" & Label31.Text & "',UNIQUE_ID='" & Label33.Text & "',DISTRICT='" & ComboBox9.SelectedItem & "' where UNIQUE_ID='" & Label33.Text & "' "
                command = New MySqlCommand(edit, connection)
                reader = command.ExecuteReader
                
                MsgBox("Changes were successfully applied.", MsgBoxStyle.Information)
                Me.Show()
                
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                connection.Dispose()
            End Try

        Else
            MsgBox("No changes have been made.", MsgBoxStyle.Information)
            Me.Show()
        End If




这是我的新代码



Using connection As New MySqlConnection("Database=ngp;data source=localhost; user id= root; password=admin")
                Using edit As New MySqlCommand
                    With edit
                        .Connection = connection
                        'ID='" & TextBox21.Text & "', UNIQUE_ID='" & Label33.Text & "',sid='" & TextBox13.Text & "',sid2='" & TextBox14.Text & "',SITE_ID='" & TextBox17.Text & "',AREA_CD='" & TextBox18.Text & "',
                        .CommandText = "update ngpmain set PENRO='" & ComboBox1.SelectedItem & "',CENRO='" & ComboBox2.SelectedItem & "',MUNICIPALITY_or_CITY='" & ComboBox3.SelectedItem & "',BARANGAY='" & TextBox1.Text & "',PROVINCE='" & TextBox2.Text & "',AREA='" & TextBox3.Text & "',SEEDS_PLANTED='" & TextBox4.Text & "',NAME_OF_ORG='" & TextBox5.Text & "',CONTACT_PERSON='" & TextBox6.Text & "',TYPE_OF_ORG='" & ComboBox4.SelectedItem & "',COMPONENT='" & ComboBox5.SelectedItem & "',COMMODITY='" & ComboBox10.SelectedItem & "',SPECIES='" & TextBox8.Text & "',YEAR='" & ComboBox6.SelectedItem & "',ZONE='" & ComboBox7.SelectedItem & "',TENURE='" & TextBox9.Text & "',NO_LOA='" & ComboBox8.SelectedItem & "',RIVER_BASIN='" & TextBox10.Text & "',WATERSHED='" & TextBox11.Text & "',REMARKS='" & TextBox12.Text & "',YR_CD='" & TextBox15.Text & "',PSGC_CD='" & TextBox16.Text & "',MALE_PLANTER='" & TextBox19.Text & "',FEMALE_PLANTER='" & TextBox20.Text & "',TOTAL_PLANTERS='" & Label31.Text & "',DISTRICT='" & ComboBox9.SelectedItem & "'"
                        .CommandType = CommandType.Text
                        .Parameters.AddWithValue("UNIQUE_ID", Label33.Text)
                    End With
                    Try
                        connection.Open()
                        edit.ExecuteNonQuery()
                        MsgBox("Changes were successfully applied.", MsgBoxStyle.Information)
                        Me.Show()
                    Catch ex As Exception
                        MsgBox(ex.Message)

                    End Try
                End Using
            End Using




1 个答案:

答案 0 :(得分:0)

在略微重写更新语句后,我注意到您可能缺少列名,这会导致更新失败:

它发生在一年之后

edit = 
    "update ngpmain 
    set ID='" & TextBox21.Text & "'
    ,PENRO='" & ComboBox1.SelectedItem & "'
    ,CENRO='" & ComboBox2.SelectedItem & "'
    ,MUNICIPALITY_or_CITY='" & ComboBox3.SelectedItem & "'
    ,BARANGAY='" & TextBox1.Text & "'
    ,PROVINCE='" & TextBox2.Text & "'
    ,AREA='" & TextBox3.Text & "'
    ,SEEDS_PLANTED='" & TextBox4.Text & "'
    ,NAME_OF_ORG='" & TextBox5.Text & "'
    ,CONTACT_PERSON='" & TextBox6.Text & "'
    ,TYPE_OF_ORG='" & ComboBox4.SelectedItem & "'
    ,COMPONENT='" & ComboBox5.SelectedItem & "'
    ,COMMODITY='" & ComboBox10.SelectedItem & "'
    ,SPECIES='" & TextBox8.Text & "'
    ,YEAR='" & ComboBox6.SelectedItem & "'
    ,='" & ComboBox7.SelectedItem & "' <--- missing column name?!!
    ,TENURE='" & TextBox9.Text & "'
    ,NO_LOA='" & ComboBox8.SelectedItem & "'
    ,RIVER_BASIN='" & TextBox10.Text & "'
    ,WATERSHED='" & TextBox11.Text & "'
    ,REMARKS='" & TextBox12.Text & "'
    ,sid='" & TextBox13.Text & "'
    ,sid2='" & TextBox14.Text & "'
    ,YR_CD='" & TextBox15.Text & "'
    , PSGC_CD='" & TextBox16.Text & "'
    ,SITE_ID='" & TextBox17.Text & "'
    ,AREA_CD='" & TextBox18.Text & "'
    , MALE_PLANTER='" & TextBox19.Text & "'
    ,FEMALE_PLANTER='" & TextBox20.Text & "'
    ,TOTAL_PLANTERS='" & Label31.Text & "'
    ,UNIQUE_ID='" & Label33.Text & "'
    ,DISTRICT='" & ComboBox9.SelectedItem & "' 
    where UNIQUE_ID='" & Label33.Text & "' "