SQL强类型查询的实现

复制 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 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.
本文地址:http://www.bhae.cn/news/142e6499793.html
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。