前言: 博客一直空著不好看就把自己在博客園裏面寫的拉過了占個位子。是一篇代码,主要是如何加查询条件的類,估计这样的文章已经有了不少,我是繁體下面打的字,裏面代碼看上去有些走形,算是篇水帖。
要想更加实用就自己改改
二个重载方法:
search.AddCondition("Fieldname", "value"),
search.AddCondition("Fieldname", "value", SqlOperator.gLessThanOrEqual, false);
SearchCondition 类:
using System; using System.Collections.Generic; using System.Text; namespace DBHelp { public enum SqlOperator { aEqual = 1, cNotLike, bLike, dMoreThan, eLessThan, fMoreThanOrEqual, gLessThanOrEqual, hNotEqual, In, NotIn } public class SearchCondition { private StringBuilder l_strSQLCdtn = new StringBuilder(); private readonly SqlOperator sqlOperator; public SqlOperator SqlOperator { get { return sqlOperator; } } public StringBuilder L_strSQLCdtn { get { return l_strSQLCdtn; } set { l_strSQLCdtn = value; } } /// <summary> /// = string /// </summary> /// <param name="a_strFieldName">字段名稱</param> /// <param name="a_strValue">字段的值</param> public void AddCondition(string a_strFieldName,string a_strValue) { l_strSQLCdtn.Append(" and " + a_strFieldName+ " = '" + a_strValue +"'"); } /// <param name="a_strFieldName">字段名稱</param> /// <param name="a_strValue">字段的值 case 'In' a_strValue = "'asdf','asdf','sadf'"</param> /// <param name="a_enumOperator">看SqlOperator操作符的提示</param> /// <param name="a_bIsAnd"> if(a_bIsAnd) is " and " else is "or" </param> public void AddCondition(string a_strFieldName, string a_strValue, SqlOperator a_emOperator,bool a_bIsAnd) { string l_strConnSymbol = a_bIsAnd ? " and " : " or "; switch (a_emOperator.ToString()) { case "aEqual": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " = '" + a_strValue + "'"); break; case "bLike": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " like '%" + a_strValue + "%'"); break; case "cNotLike": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " not like '%" + a_strValue + "%'"); break; case "dMoreThan": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " > '" + a_strValue + "'"); break; case "eLessThan": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " < '" + a_strValue + "'"); break; case "fMoreThanOrEqual": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " >= '" + a_strValue + "'"); break; case "gLessThanOrEqual": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " <= '" + a_strValue + "'"); break; case "hNotEqual": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " != '" + a_strValue + "'"); break; case "In": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " in (" + a_strValue + ")"); break; case "NotIn": l_strSQLCdtn.Append(l_strConnSymbol + a_strFieldName + " in (" + a_strValue + ")"); break; default: break; } } } }
用法說明:
private void Form1_Load(object sender, EventArgs e) { SearchCondition search = new SearchCondition(); search.AddCondition("Field1", "123"); search.AddCondition("Field2", "'adsf','adf'", SqlOperator.In,true); search.AddCondition("Field3", "123",SqlOperator.fMoreThanOrEqual,true); search.AddCondition("Field3", "123", SqlOperator.gLessThanOrEqual, false); string l_strTest = search.L_strSQLCdtn.ToString(); }
运行结果:
and Field1 = '123' and Field2 in ('adsf','adf') and Field3 >= '123' or Field3 <= '123'