【问题标题】:how to CreateSqlFilter between two textbox in one column如何在一列中的两个文本框之间创建SqlFilter
【发布时间】:2019-01-13 10:23:59
【问题描述】:

现在我使用此代码在 datagridview 中制作过滤器

private void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand selectCommand = new SqlCommand();

        var filterConditions = new[] {
    CreateSqlFilter("Name_Arabic", txtName_Arabic, selectCommand, false),
    CreateSqlFilter("gender", CBgender, selectCommand, false),
    CreateSqlFilter("CIVILIDD", txtCIVILIDD, selectCommand, true),
    CreateSqlFilter("status", comboBox1, selectCommand, false),
    CreateSqlFilter("username", txtusername, selectCommand, false),
    CreateSqlFilter("City", comboBoxCity, selectCommand, false),
    CreateSqlFilter("Governorate", comboBoxGovernorate, selectCommand, false),
    CreateSqlFilter("confirmation", comboBox2, selectCommand, false),
    CreateSqlFilter("NATIONALITY", CBNATIONALITY, selectCommand, false)
    // etc.
};

        string filterCondition = filterConditions.Any(a => a != null) ? filterConditions.Where(a => a != null).Aggregate((filter1, filter2) => String.Format("{0} AND {1}", filter1, filter2)) : (string)null;

        using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["my"].ConnectionString))
        {
            selectCommand.Connection = connection;
            selectCommand.CommandText = filterCondition == null ? "SELECT * FROM tabl2" : "SELECT * FROM tabl2 WHERE " + filterCondition;
            connection.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(selectCommand);
            DataTable dataSource = new DataTable();
            adapter.Fill(dataSource);
            dataGridView1.DataSource = dataSource;
        }
    }

    private string CreateSqlFilter(string fieldName, Control userInputControl, SqlCommand command, bool exactMatch)
    {
        string searchValue = null;
        if (userInputControl is TextBox) searchValue = ((TextBox)userInputControl).Text;
        if (userInputControl is ComboBox) searchValue = ((ComboBox)userInputControl).Text;
        if (String.IsNullOrWhiteSpace(searchValue)) return null;

        if (exactMatch)
        {
            command.Parameters.Add(new SqlParameter("@" + fieldName, searchValue));
            return fieldName + " = @" + fieldName;
        }
        else
        {
            command.Parameters.Add(new SqlParameter("@" + fieldName, "%" + searchValue + "%"));
            return fieldName + " LIKE @" + fieldName;
        }
    }

我想向它们添加两个文本框以在同一列(年龄)中进行过滤

【问题讨论】:

    标签: c# filter datagridview


    【解决方案1】:

    我认为您很清楚最终您需要以下 SQL 条件之一:

    age BETWEEN @ageFrom AND @ageTo
    // OR
    age >= @ageFrom AND age <= @ageTo
    

    但我不建议您扩展您的 CreateSqlFilter 方法,因为使所有内容通用并不是一个好习惯。您可以简单地创建另一个方法来构建您的过滤条件。

    另外,我建议您从查询构建器中删除 Control userInputControl 参数并改为接受 string value。这将使您的方法独立于 WinForms 命名空间。

    更新

    CreateRangeSqlFilter("age", tbAgeFrom.Text, tbAgeTo.Text, selectCommand),
    
    private string CreateRangeSqlFilter(string fieldName, string from, string to, SqlCommand command)
    {
        command.Parameters.Add(new SqlParameter("@" + fieldName + "From", from));
        command.Parameters.Add(new SqlParameter("@" + fieldName + "To", to));
        return $"{fieldName} BETWEEN @{fieldName}From AND @{fieldName}To";
    }
    

    更新 2

     private string CreateSqlFilter(string fieldName, Control userInputControl, SqlCommand command, bool exactMatch,
         bool isRange = false, Control userInputControl2 = null)
     {
         if (isRange)
         {
             string searchValue1 = null;
             if (userInputControl is TextBox) searchValue1 = ((TextBox)userInputControl).Text;
             if (userInputControl is ComboBox) searchValue1 = ((ComboBox)userInputControl).Text;
    
             string searchValue2 = null;
             if (userInputControl2 is TextBox) searchValue2 = ((TextBox)userInputControl2).Text;
             if (userInputControl2 is ComboBox) searchValue2 = ((ComboBox)userInputControl2).Text;
             if (String.IsNullOrWhiteSpace(searchValue1) && String.IsNullOrWhiteSpace(searchValue2)) return null;
    
             if (!String.IsNullOrWhiteSpace(searchValue1) && !String.IsNullOrWhiteSpace(searchValue2))
             {
                 command.Parameters.Add(new SqlParameter("@" + fieldName + "From", searchValue1));
                 command.Parameters.Add(new SqlParameter("@" + fieldName + "To", searchValue2));
                 return $"{fieldName} BETWEEN @{fieldName}From AND @{fieldName}To";
             }
    
             if (!String.IsNullOrWhiteSpace(searchValue1))
             {
                 command.Parameters.Add(new SqlParameter("@" + fieldName + "From", searchValue1));
                 return $"{fieldName} >= @{fieldName}From";
             }
    
             command.Parameters.Add(new SqlParameter("@" + fieldName + "To", searchValue2));
             return $"{fieldName} <= @{fieldName}To";
         }
    
         string searchValue = null;
         if (userInputControl is TextBox) searchValue = ((TextBox)userInputControl).Text;
         if (userInputControl is ComboBox) searchValue = ((ComboBox)userInputControl).Text;
         if (String.IsNullOrWhiteSpace(searchValue)) return null;
    
         if (exactMatch)
         {
             command.Parameters.Add(new SqlParameter("@" + fieldName, searchValue));
             return fieldName + " = @" + fieldName;
         }
         else
         {
             command.Parameters.Add(new SqlParameter("@" + fieldName, "%" + searchValue + "%"));
             return fieldName + " LIKE @" + fieldName;
         }
     }
    

    【讨论】:

    • 如果我想扩展它有什么方法添加它
    • 对不起,我反对。创建CreateSqlFilter 方法的副本并将其命名为CreateRangeSqlFilter。然后适当修改(接受ageFromageTo值并生成SQL条件)
    • 对不起,我还是不知道该怎么做
    • @AhmedAlKhteeb 请参阅我的帖子中的示例
    • @AhmedAlKhteeb 请参阅更新 2 部分。用法是CreateSqlFilter("age", tbAgeFrom, selectCommand, false, true, tbAgeTo)
    【解决方案2】:

    有两种不同的方法可以做到这一点,而无需每次都执行查询。

    亮点:

    过滤器:

    完整的源代码可在: https://github.com/KohrAhr/XTask/blob/master/Wpf.MainApp/Functions/Functions.DataGrid.cs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2021-08-21
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多