Ich habe Textfeld, das die Daten enthält. Wenn ich dann auf "Aktualisieren" klicke, wird das gesamte Textfeld aktiviert, damit ich sie bearbeiten kann. Wenn ich auf Aktualisieren wieder wird es zu retten, aber ich immer einen Fehler erhalten, dieFehler bei UPDATE-Anweisung beim Speichern
„Syntaxfehler in Update-Anweisung“, sagt
myConnection.Open()
Dim str As String = "UPDATE StudentDatabase set FName='" & FNameTextBox.Text & "',MName='" & MNameTextBox.Text & "',LName='" & LNameTextBox.Text & "' ,DOB='" & DOBDateTimePicker.Text & "',Gender='" & GenderTextBox.Text & "',Address='" & AddressTextBox.Text & "',Section='" & SectionTextBox.Text & "',FatherName='" & FatherNameTextBox.Text & "',FatherOccupation='" & FatherOccupationTextBox.Text & "',FatherContact='" & FatherContactTextBox.Text & "',MotherName='" & MotherNameTextBox.Text & "',MotherOccupation='" & MotherOccupationTextBox.Text & "',MotherContact='" & MotherContactTextBox.Text & "',Guardian='" & GuardianTextBox.Text & "',GuardianContact='" & GuardianContactTextBox.Text & "' where StudID='" & StudIDTextBox.Text & "' "
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
If (LNameTextBox.ReadOnly = True) Then
FNameTextBox.ReadOnly = False
LNameTextBox.ReadOnly = False
MNameTextBox.ReadOnly = False
GenderTextBox.ReadOnly = False
AddressTextBox.ReadOnly = False
SectionTextBox.ReadOnly = False
FatherNameTextBox.ReadOnly = False
FatherContactTextBox.ReadOnly = False
FatherOccupationTextBox.ReadOnly = False
MotherNameTextBox.ReadOnly = False
MotherContactTextBox.ReadOnly = False
MotherOccupationTextBox.ReadOnly = False
GuardianContactTextBox.ReadOnly = False
GuardianTextBox.ReadOnly = False
ElseIf (LNameTextBox.ReadOnly = False) Then
Try
cmd.ExecuteNonQuery()
FNameTextBox.ReadOnly = True
LNameTextBox.ReadOnly = True
MNameTextBox.ReadOnly = True
GenderTextBox.ReadOnly = True
AddressTextBox.ReadOnly = True
SectionTextBox.ReadOnly = True
FatherNameTextBox.ReadOnly = True
FatherContactTextBox.ReadOnly = True
FatherOccupationTextBox.ReadOnly = True
MotherNameTextBox.ReadOnly = True
MotherContactTextBox.ReadOnly = True
MotherOccupationTextBox.ReadOnly = True
GuardianContactTextBox.ReadOnly = True
GuardianTextBox.ReadOnly = True
myConnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Haben irgendwelche Ihrer Textfelder einen Apostroph oder ein Zitat darin, wenn sie diesen Aktualisierungscode laufen lassen? Die Art, wie dies eingerichtet wird, ist anfällig für SQL-Injection. –
Verwende Parameter ... Ich könnte 'StudentDatabase' mit '. '; DROP StudentDatabase' im Feld' StudIDTextBox' nur ein Vorschlag ... – Codexer
Verwenden Sie immer parametrisierte Abfragen. Das wird Ihre Syntaxfehler-Anweisung lösen und Sie vor SQL-Injection-Angriffen schützen. Der Code sollte wie folgt aussehen: 'Dim str As String =„UPDATE SET StudentDatabase FName = @fname, MNAME = @MName, ..... "' \t \t 'Cmd.Parameters.Add (" @ FName ", SqlDBType.VarChar) .Wert = FNameTextBox.Text' \t' Cmd.Parameters.Add ("@ MName, SqlDBType.VarChar) .Wert = MNameTextBox' \t' ... ' –