DELETE语句中的SQL语法错误

时间:2021-12-18 20:27:35

When executing a DELETE SQL statement i get an error message

执行DELETE SQL语句时,我收到一条错误消息

SYNTAX ERROR NEAR '*'

SYNTAX ERROR NEAR'*'

What could be the error in the following code ?

以下代码中的错误可能是什么?

 Public Sub dele()

    Try
        Dim con As New SqlConnection("Data Source=192.168.10.3;Initial Catalog=IT_INV;user id=sa;password=1n@r1dev")

        con.Open()

        Dim inventtable As New DataTable("pc")
        Dim rs As New SqlCommand("DELETE * FROM pc WHERE [pcname]= '" & Label5.Text & "' ", con)

        rs.ExecuteNonQuery()
        MessageBox.Show("Data Deleted")

        con.Close()
        Display_Data()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

1 个解决方案

#1


Remove the *, it doesn't belong to a DELETE but to a SELECT:

删除*,它不属于DELETE而属于SELECT:

DELETE FROM pc ...

Also, always use sql-parameters to prevent sql injection and the Using statement.

另外,始终使用sql-parameters来阻止sql注入和Using语句。

Using con As New SqlConnection("Connectino string")
    Using rs As New SqlCommand("DELETE FROM pc WHERE pcname=@pcname", con)
        rs.Parameters.Add("@pcname", SqlDbType.VarChar).Value = Label5.Text.Trim()
        con.Open()
        Dim deleted As Int32 = rs.ExecuteNonQuery()
        If deleted > 0 Then
            MessageBox.Show("Data Deleted")
        End If
    End Using '  connection doesn't need to be closed due to the Using
End Using

#1


Remove the *, it doesn't belong to a DELETE but to a SELECT:

删除*,它不属于DELETE而属于SELECT:

DELETE FROM pc ...

Also, always use sql-parameters to prevent sql injection and the Using statement.

另外,始终使用sql-parameters来阻止sql注入和Using语句。

Using con As New SqlConnection("Connectino string")
    Using rs As New SqlCommand("DELETE FROM pc WHERE pcname=@pcname", con)
        rs.Parameters.Add("@pcname", SqlDbType.VarChar).Value = Label5.Text.Trim()
        con.Open()
        Dim deleted As Int32 = rs.ExecuteNonQuery()
        If deleted > 0 Then
            MessageBox.Show("Data Deleted")
        End If
    End Using '  connection doesn't need to be closed due to the Using
End Using