简体   繁体   中英

how to CreateSqlFilter between two textbox in one column

now i use this code to make the filter in 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;
        }
    }

i want to add two textbox to them to filter in the same column ( age ) what i looking for is filter between two age like between 20 and 30 year old how to add them to my code 我的表格看起来像这样

there are two different way to do this without executing query each time.

Highlight:

在此处输入图片说明

Filter:

在此处输入图片说明

Full source code available at: https://github.com/KohrAhr/XTask/blob/master/Wpf.MainApp/Functions/Functions.DataGrid.cs

I think it's clear for you that eventually you need one of these SQL conditions:

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

But I don't recommend you to extend your CreateSqlFilter method because making everything universal is not a good practice. You can simply create another method that will build your filter condition.

Also I suggest you to remove Control userInputControl parameter from your query builder and accept string value instead. This will make your method independent of WinForms namespace.

UPDATE

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";
}

UPDATE 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;
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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