【问题标题】:Filtering on two different things with DataSet使用 DataSet 过滤两个不同的事物
【发布时间】:2018-11-18 12:36:17
【问题描述】:

我现在可以过滤一件事(输入是textbox)。但我想过滤two textboxes。这个怎么做? 我正在使用DataSet,我添加了这个queryselect * from question where questioncat = @questioncat。 在FillBy 中,我使用的是query。 我也想filter 提问(where question = @question)

代码:

private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (txtBxSearch.Text == "") 
            {
                hR5DataSetVraagTableAdapter.Fill(hR5DataSet.question);
            }
            else
            {
                hR5DataSetVraagTableAdapter.FillBy(hR5DataSet.question, txtBxSearch.Text); 
            }
        }

【问题讨论】:

  • 为什么不直接使用参数来添加过滤条件?
  • 因为我需要在我的情况下添加另一个文本框,您可以在其中输入问题。另一个文本框用于问题猫。我想同时过滤一个问题和一个问题类别
  • 我写了一个答案希望对你有帮助

标签: c# filter


【解决方案1】:

如果您想动态设置过滤条件,可以尝试使用带参数的条件来实现。

在跳过任何空参数的代码中为 SQL 查询动态构建过滤语句

private void btnSearch_Click(object sender, RoutedEventArgs e)
{

    var sqlStr = new System.Text.StringBuilder();
    sqlStr.AppendLine("select * from question where 1 = 1 ");

    if(!string.IsNullOrEmpty(txtBxSearch.Text)){
        sqlStr.AppendLine(" AND questioncat = @questioncat ");
         Parameters.Add(new SqlParameter("@questioncat", txtCondition1.Text));
        //and add parameter for your command.
    }

    if(!string.IsNullOrEmpty(txtBxSearch1.Text)){
        sqlStr.AppendLine(" AND questioncat1 = @questioncat1 ");
        Parameters.Add(new SqlParameter("@questioncat1", txtCondition2.Text));
        //and add parameter for your command.
    }
    //.... do your SQL execute.

}

注意

WHERE 1 = 1 让您可以通过AND 操作设置您的条件。

【讨论】:

  • 因此,使用此解决方案,我还可以过滤两件事;在 txtCondition1.Text AND txtCondition2.Text 中输入?
  • @DutchFatBoys 是的,您只需在条件中添加然后
  • @DutchFatBoys 或者你可以参考这个stackoverflow.com/questions/19118245/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-21
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
相关资源
最近更新 更多