问题描述:

I have different tables in my database. I want to write a single query to handle all tables like:

comm.CommandText = "Select * from table1 where UserId='" + 1 + "'";

comm.CommandText = "Select * from table2 where UserId='" + 1 + "'";

It is same query but different table name.

Is there any possible way to write both query with one query?

Thanks.

网友答案:

try using this:

string query = string.Format("Select * from {0} where UserId={1}", tableName, userID);
网友答案:

You can try with this code - string.Format

var input = "table 1";
var query = string.Format("Select * from {0} where UserId= 1 ",input);
comm.CommandText = query;
相关阅读:
Top