我在vb.net模块中有一个函数,用于获取访问数据库中的记录数。
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
每次运行时都会抛出Incomplete sql clause
异常。通过反复试验,我发现问题出在@table
参数上。如果以这种方式改写:
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
这个功能很好用。我的参数在哪里错了?