日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SQL強類型查詢的實現(xiàn)

SQL強類型查詢的許多實現(xiàn)方法都不夠直觀,下面的實現(xiàn)方法是一個直觀的SQL強類型查詢,供您參考,希望對您有所啟示。

魯?shù)榫W(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),魯?shù)榫W(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為魯?shù)閿?shù)千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個售后服務(wù)好的魯?shù)樽鼍W(wǎng)站的公司定做!

這里只是個驗證想法的代碼,所以沒有作任何容錯和擴展性處理.也不要提出OO不OO的看法,毫無疑義.

所設(shè)想的是一個查詢 Select [Columnlist] From [TableName] Where [Exp] Order By [PK] 一般來說是這個格式,我們最難表述的其實就是[Exp]這個部分。前面的都比較格式化,所以可以通過嵌套方法來實現(xiàn)還是比較合適的,但是[Exp]這個部分用諸如AND(Exp1,Exp2)這樣子的形式不能很直觀的看出表達式的意義,所以通過重載操作符的方式來實現(xiàn),這里我們假設(shè)在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 sps;
  21.  21
  22.  22        public List 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();
  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強類型查詢的實現(xiàn)
當前鏈接:http://www.dlmjj.cn/article/djochdo.html