0

Below is sample code I am using. For reference, let's say table = "manufacturers" and index = 5. When the code runs, I get an error saying there is a problem with my syntax near 'manufacturers' WHERE id = 5 at line 1.

The query is about as simple as it gets: SELECT id, locked FROM 'manufacturers' WHERE id = 5;

I am new to prepared statements so hopefully someone can shed some light on my failing code below (and thank you in advance for any help you may be able to offer).

Public Function IsRecordLocked(ByVal table As String, ByVal index As Int16)

Dim sqlQuery As String
Dim locked As String
sqlQuery = "SELECT id, locked FROM @tbl WHERE id = @key"
Using sqlConn As New MySqlConnection(gtxtMySQLConnectionString)
    Using sqlComm As New MySqlCommand()
        With sqlComm
            .Connection = sqlConn
            .CommandText = sqlQuery
            .CommandType = CommandType.Text
            .Parameters.Add("@tbl", MySqlDbType.VarChar, 15).Value = table
            .Parameters.Add("@key", MySqlDbType.Int16, 5).Value = index
        End With
        Try
            sqlConn.Open()
            sqlComm.Prepare()
            Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader()
            While sqlReader.Read()
                If String.IsNullOrEmpty(sqlReader("locked")) Then
                    locked = "False"
                Else
                    locked = sqlReader("locked").ToString()
                    MsgBox("This record is currently locked for editing by " + locked + ". You will not be able to make changes to this record until it is released.", MsgBoxStyle.OkOnly, "Record Locked")
                End If
            End While
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            sqlConn.Dispose()
        End Try
    End Using
End Using
Return locked

End Function

0 Answers0