现在我使用此代码在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,CreatesqlFilter("CIVILIDD",txtCIVILIDD,true),CreatesqlFilter("status",comboBox1,CreatesqlFilter("username",txtusername,CreatesqlFilter("City",comboBoxCity,CreatesqlFilter("Governorate",comboBoxGovernorate,CreatesqlFilter("confirmation",comboBox2,CreatesqlFilter("NATIONAliTY",CBNATIONAliTY,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岁之间,如何将其添加到我的代码中
age BETWEEN @ageFrom AND @ageto// ORage >= @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,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; } }
总结 以上是内存溢出为你收集整理的c#-如何在一列中的两个文本框之间创建SqlSqlFilter 全部内容,希望文章能够帮你解决c#-如何在一列中的两个文本框之间创建SqlSqlFilter 所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)