I want to paste table name as function parameter and function need to return DataSet, this is the my code for that:
Public Function GetTTabele(ByVal tableName As String) As DataSet
Dim DAT As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM tableName", nwindConn)
Dim DAT As DataSet = New DataSet()
DAT.MissingSchemaAction = MissingSchemaAction.AddWithKey
DAT.Fill(DAT, tableName)
GetTTabele = DAT
End Function
Now when i execute this code I'm开发者_运维知识库 getting next error: System.Data.SqlClient.SqlException: Invalid object name 'tableName'.
"SELECT * FROM tableName"
should be changed to "SELECT * FROM " & tableName
allowing the contents of your parameter tableName to be appended to the string "SELECT * FROM "
Table "tableName" does not exists on your database. Specify a existing table name.
Change the line of code
Dim DAT As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM tableName", nwindConn)
to read
Dim DAT As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM " & tableName, nwindConn)
You are trying to find a table called, literally, "tableName", rather than the table name stored in the variable tableName.
精彩评论