2014-10-23 2 views
0

VB에서 업데이트 명령을 만들려고합니다. 나는 그것을 만들 수 있었고 어떤 오류도 보이지 않았다.VB에서 mysql 데이터베이스를 업데이트 할 수 없습니다.

그러나 디버깅 중에 명령을 수행하려고하면 업데이트가 작동하지 않는 것 같습니다. 실행 중에 오류가 발생하지 않지만 데이터베이스가 변경되지 않았습니다. 여기

내 code..it가에 대한 비트 long..sorry입니다입니다 .. 여기

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

입니다
+0

그것은 발생하여 업데이트 문을 작성 당신이 업데이트 실패를 야기 할 열 이름을, 누락 될 수 있습니다 나타났습니다. – Donal

+0

@ doonal 내 코드로 어떻게 할 것인가? .thanks – mrkdenz

+0

문제는 ExecuteReader 대신 ExecuteNonQuery를 사용해야한다는 것입니다. 매개 변수가있는 쿼리는 모범 사례입니다. – Donal

답변

0

약간 뒤로 당신은 SQL 주입 공격을 방지하기 위해 매개 변수가있는 쿼리를 사용해야 우측 년

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 & "' " 
+0

죄송합니다. 실수로 게시물을 게시하는 도중 제거해야합니다.하지만 내 프로그램에 있습니다. 코드를 다시 작성하십시오. – mrkdenz

관련 문제