SQL强类型查询的实现

数据库 SQL Server
下面为您介绍的是一个直观的SQL强类型查询的实现方法,如果您在此方面遇到过问题,不妨一看,对您会有所帮助。

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 这个表达式

具体代码如下

  1.  class Program  
  2.   2    {  
  3.   3        static void Main(string[] args)  
  4.   4        {  
  5.   5  
  6.   6            Exp rs = new Exp("C1") == 25 & new Exp("C2") > 3 | new Exp("C3") < 5 ^ new Exp("C4") % "hehe";  
  7.   7            Console.WriteLine(rs.Sql);  
  8.   8            foreach (SqlParameter sp in rs.Sps)  
  9.   9            {  
  10.  10                Console.WriteLine(sp.ParameterName);  
  11.  11            }  
  12.  12            Console.Read();  
  13.  13        }  
  14.  14    }  
  15.  15  
  16.  16    class Exp  
  17.  17    {  
  18.  18        private string _Sql;  
  19.  19  
  20.  20        private List<SqlParameter> sps;  
  21.  21  
  22.  22        public List<SqlParameter> Sps  
  23.  23        {  
  24.  24            get { return sps; }  
  25.  25            set { sps = value; }  
  26.  26        }  
  27.  27  
  28.  28        private SqlParameter sp;  
  29.  29  
  30.  30        public string Sql  
  31.  31        {  
  32.  32            get { return _Sql; }  
  33.  33        }  
  34.  34  
  35.  35        private Exp()  
  36.  36        {  
  37.  37            sps = new List<SqlParameter>();  
  38.  38        }  
  39.  39  
  40.  40        public Exp(string CollumnName)  
  41.  41        {  
  42.  42            _Sql = CollumnName;  
  43.  43        }  
  44.  44  
  45.  45        public static Exp operator ==(Exp Left, Object Value)  
  46.  46        {  
  47.  47            Exp Next = new Exp();  
  48.  48            Next.sp = new SqlParameter(Left._Sql, Value);  
  49.  49            Next.sps.Add(Next.sp);  
  50.  50            Next._Sql = Left._Sql + " = @" + Left.Sql;  
  51.  51            return Next;  
  52.  52        }  
  53.  53        public static Exp operator !=(Exp Left, Object Value)  
  54.  54        {  
  55.  55            Exp Next = new Exp();  
  56.  56            Next.sp = new SqlParameter(Left._Sql, Value);  
  57.  57            Next.sps.Add(Next.sp);  
  58.  58            Next._Sql = Left._Sql + " <> @" + Left._Sql;  
  59.  59            return Next;  
  60.  60        }  
  61.  61  
  62.  62        public static Exp operator <(Exp Left, Object Value)  
  63.  63        {  
  64.  64            Exp Next = new Exp();  
  65.  65            Next.sp = new SqlParameter(Left._Sql, Value);  
  66.  66            Next.sps.Add(Next.sp);  
  67.  67            Next._Sql = Left._Sql + " < @" + Left._Sql;  
  68.  68            return Next;  
  69.  69        }  
  70.  70        public static Exp operator >(Exp Left, Object Value)  
  71.  71        {  
  72.  72            Exp Next = new Exp();  
  73.  73            Next.sp = new SqlParameter(Left._Sql, Value);  
  74.  74            Next.sps.Add(Next.sp);  
  75.  75            Next._Sql = Left._Sql + " > @" + Left._Sql;  
  76.  76            return Next;  
  77.  77        }  
  78.  78  
  79.  79        public static Exp operator %(Exp Left, Object Value)  
  80.  80        {  
  81.  81            Exp Next = new Exp();  
  82.  82            Next.sp = new SqlParameter(Left._Sql, Value);  
  83.  83            Next.sps.Add(Next.sp);  
  84.  84            Next._Sql = Left._Sql + " Like @" + Left._Sql;  
  85.  85            return Next;  
  86.  86        }  
  87.  87  
  88.  88        public static Exp operator &(Exp Left, Exp Right)  
  89.  89        {  
  90.  90            Exp Next = new Exp();  
  91.  91            foreach (SqlParameter sp in Left.sps)  
  92.  92            {  
  93.  93                Next.sps.Add(sp);  
  94.  94            }  
  95.  95            foreach (SqlParameter sp in Right.sps)  
  96.  96            {  
  97.  97                Next.sps.Add(sp);  
  98.  98            }  
  99.  99            Next._Sql = Left.Sql + " AND " + Right.Sql;  
  100. 100            return Next;  
  101. 101        }  
  102. 102  
  103. 103  
  104. 104        public static Exp operator |(Exp Left, Exp Right)  
  105. 105        {  
  106. 106            Exp Next = new Exp();  
  107. 107            foreach (SqlParameter sp in Left.sps)  
  108. 108            {  
  109. 109                Next.sps.Add(sp);  
  110. 110            }  
  111. 111            foreach (SqlParameter sp in Right.sps)  
  112. 112            {  
  113. 113                Next.sps.Add(sp);  
  114. 114            }  
  115. 115            Next._Sql = Left.Sql + " OR " + Right.Sql;  
  116. 116            return Next;  
  117. 117        }  
  118. 118  
  119. 119        public static Exp operator ^(Exp Left, Exp Right)  
  120. 120        {  
  121. 121            Exp Next = new Exp();  
  122. 122            foreach (SqlParameter sp in Left.sps)  
  123. 123            {  
  124. 124                Next.sps.Add(sp);  
  125. 125            }  
  126. 126            foreach (SqlParameter sp in Right.sps)  
  127. 127            {  
  128. 128                Next.sps.Add(sp);  
  129. 129            }  
  130. 130            Next._Sql = Left.Sql + " NOT " + Right.Sql;  
  131. 131            return Next;  
  132. 132        }  
  133. 133    }  
  134. 134    

 

 

【编辑推荐】

使用SQL语句实现查询连续号段

SQL查询效率的讨论

单表多条件查询sql语句写法

SQL动态查询的示例

使用SQL语句查询时间段

 

责任编辑:段燃 来源: 互联网
相关推荐

2009-09-08 14:56:55

强类型DataContLinq to SQL

2010-09-28 14:33:13

SQL语句

2010-11-09 09:43:20

SQL Server查

2010-11-09 15:18:37

SQL Server多

2010-10-21 14:43:46

SQL Server查

2010-10-21 10:28:13

SQL Server查

2010-09-26 17:09:05

SQL语句

2010-11-09 11:23:35

sql server查

2010-09-26 17:04:01

sql查询

2009-08-04 17:52:25

ActorLite强类型

2023-10-10 19:32:43

强静态类型制表符

2010-09-26 13:56:43

SQL远程查询

2010-09-28 11:53:28

SQL查询

2010-10-11 09:05:40

SQL Server

2010-11-08 17:13:21

SQL Server跨

2011-04-13 08:49:33

DataSet强类型化

2011-08-23 09:12:17

SQL Server多

2024-03-05 15:28:38

SQL窗口函数分页查询

2020-08-31 19:17:24

Python强类型语言弱类型语言

2010-09-25 16:37:33

SQL动态查询
点赞
收藏

51CTO技术栈公众号