ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

c#-如何在一列中的两个文本框之间创建SqlSqlFilter

2019-12-11 06:09:06  阅读:236  来源: 互联网

标签:filter datagridview c


现在我使用此代码在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;
        }
    }

我想向其添加两个文本框以在同一列(年龄)中进行过滤,我想要的是在两个年龄之间进行过滤,例如20至30岁之间,如何将其添加到我的代码中
my form look like this

解决方法:

我认为很明显,您最终将需要以下SQL条件之一:

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

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

另外,我建议您从查询生成器中删除Control userInputControl参数,而改为接受字符串值.这将使您的方法独立于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;
     }
 }

标签:filter,datagridview,c
来源: https://codeday.me/bug/20191211/2106915.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有