Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The program I am writing is designed to be used on different databases at different times with different table names in the database. How can I write the Select statement to use a table name that is chosen at runtime? I tried putting the table name into a string variable "strTableName" and then writing the Select statement as follows:

Using da As New OleDbDataAdapter("Select * From strTableName", cnnSource)

But this will not work. If I assign a specific table name in the database, such as "MyTestData", and then write the Select statement as follows, it works:

Using da As New OleDbDataAdapter("Select * From MyTestData", cnnSource)

But I do not want to have to use that table name in every database.

Any Suggestions?
Posted
Comments
[no name] 10-Apr-14 11:18am    
"this will not work", of course not. Learn about string concatenation or use string.Format.

1 solution

You could do it:
VB
Using da As New OleDbDataAdapter("Select * From " & strTableName, cnnSource)
- but you will have to be very, very careful where you get the table name from - concatenating strings to form an SQL command is seriously dangerous, and very bad practice! It leaves you wide open to SQL Injection if you don't pay attention and that can destroy your database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900