I have a function in vb.net module that is meant to get the number of records in an access database.
Function CalculateRecords(ByVal column As String, ByVal table As String)
Dim count As Integer = 0
Using connect As New OleDbConnection(connectionString)
connect.Open()
Using com As New OleDbCommand("SELECT COUNT(@column) FROM @table", connect)
com.Parameters.Add(New OleDbParameter("@column", column))
com.Parameters.Add(New OleDbParameter("@table", table))
count = com.ExecuteScalar
End Using
End Using
Return count
End Function
Every time it's run an Incomplete sql clause
exception is thrown. Through trial and error I found that the problem lies with the @table
parameter. If rewritten this way:
Function CalculateRecords(ByVal column As String, ByVal table As String)
Dim count As Integer = 0
Using connect As New OleDbConnection(connectionString)
connect.Open()
Using com As New OleDbCommand("SELECT COUNT(@column) FROM " & table, connect)
com.Parameters.Add(New OleDbParameter("@column", column))
count = com.ExecuteScalar
End Using
End Using
Return count
End Function
The function works just fine. Where am I going wrong with my parameters?