简体   繁体   English

在SQL列中循环

[英]Cycle Through Sql Columns

I am working with a gridview in C# and I am wondering if there is an effective way to use one data source instead of three. 我正在C#中使用gridview,我想知道是否存在使用一个数据源而不是三个数据源的有效方法。 Right now I have an if statement that selects the Data Source based off the value in a dropdownlist, ddlType. 现在,我有一个if语句,它根据下拉列表ddlType中的值选择数据源。

if (ddlType.Text == "Confirmation")
    gvMailMergeExport.DataSourceID = "SqlDSConfirmation";
else if (ddlType.Text == "Cancellation")
    gvMailMergeExport.DataSourceID = "SqlDSCancellation";
else
    gvMailMergeExport.DataSourceID = "SqlDSPreArrival";

Because each Database needs to look at a different column in the same table to decide which data to show. 因为每个数据库都需要查看同一表中的不同列来决定显示哪些数据。 The three columns being used are ConfirmationEmail, CancellationEmail, and PreArrivalEmail. 使用的三列是ConfirmationEmail,CancellationEmail和PreArrivalEmail。 Each of these three columns is a bit value and I only display the rows where the correct column has a '0' for it's value. 这三列中的每一列都是一个位值,我只显示正确的列的值为“ 0”的行。 So question: is there anything like @ColumnName = 0 that would work for this? 那么问题来了:是否有像@ColumnName = 0这样的东西可以工作呢? Thank you. 谢谢。

I've never used SQLDataSources, but if you want to go the more custom route, you could build out your query (or use LINQ to SQL or LINQ to Entity Framework ) with the custom where clause based on the user selection. 我从来没有使用过SQLDataSources,但是如果您想走更多的自定义路线,则可以根据用户选择使用自定义where子句来构建查询(或使用LINQ to SQLLINQ to Entity Framework )。

Which technology are you more familiar with? 您更熟悉哪种技术? LINQ would be better, but I can give an answer in ADO.NET as well ( SqlConnection, SqlCommand , etc). LINQ会更好,但是我也可以在ADO.NET给出答案( SqlConnection, SqlCommand等)。

So the LINQ would be relatively simple. 因此,LINQ将相对简单。 After setting up your LINQ to Entities (EDMX) or LINQ to SQL (DBML) (I'd do the EDMX, because L2S is no longer supported in forward maintenance by MSFT). 在设置LINQ to Entities(EDMX)或LINQ to SQL(DBML)之后(我会做EDMX,因为MSFT的正向维护不再支持L2S)。 With an existing DB, it's very easy, drag and drop. 使用现有的数据库,拖放非常简单。

The code would look like this: 代码如下所示:

using(var context = new LinqDbContext())
{
    var results = context.Tablename;
    if(limitByConfEmail)
    {
        results = results.Where(data => data.ConfirmationEmail == true);
    }
    // do your elses here

    gridview.DataSource = results;
    gridview.DataBind();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM