开发者

Paste table name as parameter

开发者 https://www.devze.com 2022-12-21 08:21 出处:网络
I want to paste table name as function parameter and function need to return DataSet, this is the my code for that:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号