当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - I have different tables in database - I want to select table dynamically with user clicking

问题描述:

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;
分享给朋友:
您可能感兴趣的文章:
随机阅读: