Use sql table like sql parameter in vb.net

467 views Asked by At

I have query with sql parameters. My parameter must be my table from database and I don't now how I can realize this.

I did it so:

myCommand = myConnection.CreateCommand()
myCommand.CommandType = CommandType.Text
myCommand.CommandText =
"SELECT Id, Bez, Param5, Info from Table" & "@idFixed"
myCommand.Parameters.Add("@idFixed", SqlDbType.VarChar).Value = strIdFixed

strIdFixed(transmitted parameter) must be something id, because I have many tables with names Table01, Table02, Table333 ....

1

There are 1 answers

0
Emma W. On BEST ANSWER

I have it

Dim tableName As String = "Test" + strIdFixed.ToString.Trim
Dim builder = New SqlCommandBuilder()
Dim escapedTableName As String = builder.QuoteIdentifier(tableName)

myCommand = myConnection.CreateCommand()
myCommand.CommandType = CommandType.Text
myCommand.CommandText =
"SELECT Id, Bez, Param5, Info from " + escapedTableName

Thank https://stackoverflow.com/a/17948039/6787667