我有一个具有以下列名的sql表:c1,c2,c3,c4,c5,c6,c7,c8,c9,c10。我的Asp.Net代码(C#)中也有一个列表。
public static List listColumns = new List();
listColumns随机包含c1,c2 .. c10。例如,它可以包含c1,c5,c6,c9。我想用这个条件进行查询。所以,如果列表有这些值,我的查询应该是:
SELECT c1, c5, c6, c9 FROM Table
listColumns可能包含不同的值,并且具有不同的计数。我该如何定期进行此查询?
答案 0 :(得分:0)
尝试类似......
List<string> columnNames = new List<string> {"c1", "c2", "c4"};
string result = string.Join(", ", columnNames);
string sql = "SELECT " + result + " FROM Table";
答案 1 :(得分:0)
如果您只有十列,则更有意义的是将它们全部返回并在代码中选择您需要的列。
例如,如果您将类似GridView
的内容绑定到结果中,则可以通过编程方式创建绑定列(免责声明:这是来自内存):
var data = GetData(); // Get all 10 of your columns
var gridView = new GridView() { Id = "gridView1", AutoGenerateColumns = false };
foreach(var colName in listColumns)
{
gridView.Columns.Add(new BoundField() { DataField = colName });
}
gridView.DataSource = data;
gridView.DataBind();
答案 2 :(得分:-1)
尝试这样的事情:
sSQL = "SELECT [MIS.System.Beneficiaries].[ID] As ' الرقم التسلسلي '" _
+ FullName + NationalNumber + Gender + YearOfBirth + phone + Email + RelativePhoneNumber + Districts + City + Address + Date1 + Time + AuditName + VistNumber + SupervisorName + Benfishrieshere +
" FROM [MIS.System.Beneficiaries] INNER JOIN [MIS.Lookup.Districts] on [MIS.System.Beneficiaries].DistrictID=[MIS.Lookup.Districts].ID INNER JOIN [MIS.Lookup.Gender] on [MIS.System.Beneficiaries].GenderID=[MIS.Lookup.Gender].ID LEFT JOIN [dbo].[MIS.System.Beneficiaries.SpotCheck] on [MIS.System.Beneficiaries].ID =[MIS.System.Beneficiaries.SpotCheck].BenfiiciriesID WHERE [dbo].[MIS.System.Beneficiaries.SpotCheck].[Date] > Convert(datetime,'" + textbox6_1.Text + "') AND [dbo].[MIS.System.Beneficiaries.SpotCheck].[Date] < Convert(datetime,'" + textbox2.Text + "') AND [dbo].[MIS.System.Beneficiaries.SpotCheck].[CompanyID] ='" + DropDownList4.SelectedValue + "' "