2014-04-24 2 views
0

vb.net을 사용하여 MySQL 데이터베이스의 데이터를 조정하는 함수를 만들려고합니다.vb.net을 사용하여 데이터베이스 (MySQL) 데이터 조정

Private Sub alterDB_Click(sender As Object, e As EventArgs) Handles alterDB.Click 
    Dim user As String = globalValue.userN 
    Dim password As String = globalValue.passwN 
    If ComboBox1.Text <> "" Then 
     Dim newValue As Integer 
     newValue = Label3.Text - NumericUpDown1.Value 

     Dim conn As New sqlConnection 
     Dim answerN As DataTable   
     Dim sqlQnA As String = "INSERT INTO equiptment(inStorage) value '" & newValue & "' WHERE iName='" & ComboBox1.Text & "'" 

      conn.altering(user, password, sqlQnA) 
      answerN = conn.getData() 

    End If 

End Sub 

내가 오류 메시지가 거의 그냥 완전히, 기능 있어요 :)

오류 MSG를 제안한다 :

내 코드입니다 당신은 당신의 SQL 구문에 오류가 있습니다; "140"WHERE iNavn = 'SkateboardW15'근처의 라인 1에서 올바른 구문을 찾으려면 MySQL 서버 버전에 해당하는 설명서를 확인하십시오.

+1

INSERT는 WHERE 절을 사용하지 않습니다. INSERT는 행을 추가합니다. 아마 당신은 행을 "조정할"UPDATE를 원할 것입니다. '당신의 MySQL 서버 버전에 해당하는 매뉴얼을 확인하십시오. == 좋은 조언 – Plutonix

+0

아, 그냥 사소한 문제라고 생각했는데, 어떤 이유로 업데이트가 올바른 방법 일 때 INSERT에 고정되어있었습니다. 이제는 잘 작동합니다. 감사합니다. :) – Freshman

+0

그런 SQL 문자열에 데이터를 포함하지 마십시오. 그것은 귀하의 응용 프로그램을 미친 SQL 주입 공격에 취약한 단풍. 쿼리 매개 변수를 대신 사용하십시오. –

답변

1

UPDATE 대신의 INSERT를 사용해보십시오. 또한 SQL 주입 공격을 피하기 위해 매개 변수화 된 쿼리를 조사해야합니다. 당신이 가지고있는 것은 미친 짓입니다. 사용자 정의 유형을 사용하여 연결을 관리하고 쿼리를 실행하는 것처럼 보입니다. 좋습니다.하지만 쿼리 매개 변수에 해당 유형에 대한 지원을 추가해야합니다. 또한 ADO.Net은 연결 풀링 기능을 사용하므로 대부분의 쿼리에 대해 새 SqlConnection 개체를 사용할 때 가장 잘 작동합니다.

Private Sub alterDB_Click(sender As Object, e As EventArgs) Handles alterDB.Click 
    'Move password enforcement to your connection object or connection string 

    'use a guard clause that does not increase the nesting depth of your code 
    If string.IsNullOrWhiteSpace(ComboBox1.Text) Then Exit Sub 

    Dim newValue As Integer = CInt(Label3.Text) - NumericUpDown1.Value  
    'sql string should be a constant, or any changes should only be typed by the programmer 
    '         never the user 
    Dim sqlQnA As String = "UPDATE equipment SET inStorage = @newValue WHERE iName= @iName" 

    'Using block will guarantee the connection is closed properly, even if an exception is thrown 
    Using conn As New SqlConnection("connection string here"), _ 
      cmd As New SqlCommand(sqlQnA, conn) 

     'use query parameters that allow you to set the specific database type and length you need 
     cmd.Parameters.Add("@newValue", SqlDbType.Int).Value = newValue 
     cmd.Parameters.Add("@iName", SqlDbType.NVarChar, 40).Value = ComboBox1.Text 

     conn.Open() 
     cmd.ExecuteNonquery() 
    End Using 
End Sub 
+0

Thats 훨씬 더 우아한, 매력처럼 작동, 감사합니다 :)! – Freshman

0

이것은 아마도 당신이 찾고있는 것일 수도 있습니다 올바른 방향으로 당신을 가리 킵니다 :

Private Sub alterDB_Click(sender As Object, e As EventArgs) Handles alterDB.Click 
    If ComboBox1.Text <> "" Then 
     Dim con As New SqlConnection 
     Dim cmd As New SqlCommand 

     Try 
     ' Change Everything In The Next Line With CAPS To Your Configuration ' 
     con.ConnectionString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;User ID=" & globalValue.userN & ";Password=" & globalValue.passwN 

     con.Open() 

     cmd.Connection = con 
     cmd = New SqlCommand("INSERT INTO equiptment(inStorage) value '" & newValue & "' WHERE iName='" & ComboBox1.Text & "'", con) 
     cmd.ExecuteNonQuery() 

     Catch ex AS Exception 
     ' Do Something If Error Exist ' 
     Finally 
     con.close() 
     End Try 
    End If 
End Sub 
관련 문제