简体   繁体   中英

Paste table name as parameter

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM