日期:2011-01-25 23:11:00 来源:本站整理
<b>SQL强范例查询的实现</b>[MSSQL防范]
本文“<b>SQL强范例查询的实现</b>[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
SQL强范例查询的很多实现办法都不够直观,下面的实现办法是一个直观的SQL强范例查询,供您参考,但愿对您有所启迪.
这里只是个考证设法的代码,所以没有作任何容错和扩大性处理.也不要提出OO不OO的见解,毫无疑义.
所假想的是一个查询 Select [Columnlist] From [TableName] Where [Exp] Order By [PK] 普通来说是这个格局,我们最难表述的其实就是[Exp]这个部份.前面的都对比格局化,所以可以通过嵌套办法来实现还是对比符合的,但是[Exp]这个部份用诸如AND(Exp1,Exp2)这模样的情势不能很直观的看出表达式的意义,所以通太重载操作符的方法来实现,这里我们假定在Entity的Class里有static成员来存储列的名称.那么
Exp Rs=new Exp(User.ID) == 2 & new Exp(User.State) > 0 ;
这模样的格局就可以表达Where背面的 ID=2 AND State>0 这个表达式
具体代码以下
<
- class Program
- 2 {
- 3 static void Main(string[] args)
- 4 {
- 5
- 6 Exp rs = new Exp("C1") == 25 & new Exp("C2") > 3 | new Exp("C3") < 5 ^ new Exp("C4") % "hehe";
- 7 Console.WriteLine(rs.Sql);
- 8 foreach (SqlParameter sp in rs.Sps)
- 9 {
- 10 Console.WriteLine(sp.ParameterName);
- 11 }
- 12 Console.Read();
- 13 }
- 14 }
- 15
- 16 class Exp
- 17 {
- 18 private string _Sql;
- 19
- 20 private List<SqlParameter> sps;
- 21
- 22 public List<SqlParameter> Sps
- 23 {
- 24 get { return sps; }
- 25 set { sps = value; }
- 26 }
- 27
- 28 private SqlParameter sp;
- 29
- 30 public string Sql
- 31 {
- 32 get { return _Sql; }
- 33 }
- 34
- 35 private Exp()
- 36 {
- 37 sps = new List<SqlParameter>();
- 38 }
- 39
- 40 public Exp(string CollumnName)
- 41 {
- 42 _Sql = CollumnName;
- 43 }
- 44
- 45 public static Exp operator ==(Exp Left, Object Value)
- 46 {
- 47 Exp Next = new Exp();
- 48 Next.sp = new SqlParameter(Left._Sql, Value);
- 49 Next.sps.Add(Next.sp);
- 50 Next._Sql = Left._Sql + " = @" + Left.Sql;
- 51 return Next;
- 52 }
- 53 public static Exp operator !=(Exp Left, Object Value)
- 54 {
- 55 Exp Next = new Exp();
- 56 Next.sp = new SqlParameter(Left._Sql, Value);
- 57 Next.sps.Add(Next.sp);
- 58 Next._Sql = Left._Sql + " <> @" + Left._Sql;
- 59 return Next;
- 60 }
- 61
- 62 public static Exp operator <(Exp Left, Object Value)
- 63 {
- 64 Exp Next = new Exp();
- 65 Next.sp = new SqlParameter(Left._Sql, Value);
- 66 Next.sps.Add(Next.sp);
- 67 Next._Sql = Left._Sql + " < @" + Left._Sql;
- 68 return Next;
- 69 }
- 70 public static Exp operator >(Exp Left, Object Value)
- 71 {
- 72 Exp Next = new Exp();
- 73 Next.sp = new SqlParameter(Left._Sql, Value);
- 74 Next.sps.Add(Next.sp);
- 75 Next._Sql = Left._Sql + " > @" + Left._Sql;
- 76 return Next;
- 77 }
- 78
- 79 public static Exp operator %(Exp Left, Object Value)
- 80 {
- 81 Exp Next = new Exp();
- 82 Next.sp = new SqlParameter(Left._Sql, Value);
- 83 Next.sps.Add(Next.sp);
- 84 Next._Sql = Left._Sql + " Like @" + Left._Sql;
- 85 return Next;
- 86 }
- 87
- 88 public static Exp operator &(Exp Left, Exp Right)
- 89 {
- 90 Exp Next = new Exp();
- 91 foreach (SqlParameter sp in Left.sps)
- 92 {
- 93 Next.sps.Add(sp);
- 94 }
- 95 foreach (SqlParameter sp in Right.sps)
- 96 {
- 97 Next.sps.Add(sp);
- 98 }
- 99 Next._Sql = Left.Sql + " AND " + Right.Sql;
- 100 return Next;
- 101 }
- 102
- 103
- 104 public static Exp operator |(Exp Left, Exp Right)
- 105 {
- 106 Exp Next = new Exp();
- 107 foreach (SqlParameter sp in Left.sps)
- 108 {
- 109 Next.sps.Add(sp);
- 110 }
- 111 foreach (SqlParameter sp in Right.sps)
- 112 {
- 113 Next.sps.Add(sp);
- 114 }
- 115 Next._Sql = Left.Sql + " OR " + Right.Sql;
- 116 return Next;
- 117 }
- 118
- 119 public static Exp operator ^(Exp Left, Exp Right)
- 120 {
- 121 Exp Next = new Exp();
- 122 foreach (SqlParameter sp in Left.sps)
- 123 {
- 124 Next.sps.Add(sp);
- 125 }
- 126 foreach (SqlParameter sp in Right.sps)
- 127 {
- 128 Next.sps.Add(sp);
- 129 }
- 130 Next._Sql = Left.Sql + " NOT " + Right.Sql;
- 131 return Next;
- 132 }
- 133 }
- 134
以上是“<b>SQL强范例查询的实现</b>[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论