<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_※自言自语※</title><subtitle type="text">dotnet 技术探讨</subtitle><id>http://feed.cnblogs.com/blog/u/4012/rss</id><updated>2012-04-19T02:47:43Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/4012/rss"/><entry><id>http://www.cnblogs.com/Koy/archive/2012/04/19/2456709.html</id><title type="text">SQL 數字欄位格式調整(輸出結果以0補至固定長度)</title><summary type="text">SELECT RIGHT('00000000'+ CONVERT(VARCHAR(8),bcd.Bill_CombineDetailsId),8) FROM adiaa.Bill_CombineDetails bcd</summary><published>2012-04-19T02:47:00Z</published><updated>2012-04-19T02:47:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2012/04/19/2456709.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2012/04/19/2456709.html"/><content type="html">&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div&gt;SELECT RIGHT('00000000'+ CONVERT(VARCHAR(8),bcd.Bill_CombineDetailsId),8) FROM adiaa.Bill_CombineDetails bcd&lt;/div&gt;&lt;p&gt; &lt;/p&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/2456709.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2012/04/19/2456709.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2012/04/08/2438035.html</id><title type="text">转载：数组Marshalling</title><summary type="text">转载自：http://blog.csdn.net/mountaintaiii/article/details/4230213在托管代码和本地代码之间传递数组，是interop marshaling中间比较复杂的一个问题。本文从数组的定义开始，介绍数组marshalling的三种方法，并对blittable类型等概念做进一步的讨论。当托管代码需要和本地代码互操作时，我们就进入了interop的领域。interop的场景形形色色，不变的是我们需要把数据从一个世界marshal到另一个世界。在讨论数组marshalling之前，请各位和我一起思考一个问题，什么是数组？之所以要讨论这个问题，原因在于不</summary><published>2012-04-08T13:35:00Z</published><updated>2012-04-08T13:35:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2012/04/08/2438035.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2012/04/08/2438035.html"/><content type="html">&lt;p&gt;&lt;/p&gt;&lt;div style="text-align: left;"&gt;&lt;font face="'Microsoft YaHei'"&gt;&lt;span style="font-size: 20px; line-height: 30px;"&gt;转载自：&lt;/span&gt;&lt;/font&gt;&lt;a href="http://blog.csdn.net/mountaintaiii/article/details/4230213"&gt;http://blog.csdn.net/mountaintaiii/article/details/4230213&lt;/a&gt;&lt;/div&gt;&lt;div id="article_content" style="margin-top: 20px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Arial; line-height: 26px; color: #333333; text-align: left; background-color: #ffffff; "&gt;&lt;p&gt;在托管代码和本地代码之间传递数组，是interop marshaling中间比较复杂的一个问题。本文从数组的定义开始，介绍数组marshalling的三种方法，并对blittable类型等概念做进一步的讨论。&lt;/p&gt;&lt;p&gt;&lt;br /&gt;当托管代码需要和本地代码互操作时，我们就进入了interop的领域。interop的场景形形色色，不变的是我们需要把数据从一个世界marshal到另一个世界。&lt;/p&gt;&lt;p&gt;&lt;br /&gt;在讨论数组marshalling之前，请各位和我一起思考一个问题，什么是数组？之所以要讨论这个问题，原因在于不同的术语在不同的语境中含有不同的意思。在使用c语言的时候，我认为数组就是一个指针。但是熟悉c#的朋友可能不同意我的观点，数组是System.Array或者Object[]。我认为，这两种回答都是出自语言领域的正确观点。那么如果有一个项目含有两个模块，一个用本地代码撰写，另一个用托管代码撰写，两者之间的接口要求传递一个数组，这个&amp;#8221;数组&amp;#8221;包含着怎样的语义呢？我觉得有两点是很重要的：&lt;/p&gt;&lt;p&gt;&lt;br /&gt;1. 如何访问数组元素。就好比c语言中的数组指针，c#中的数组引用，都是访问数组必不可少的线索。&amp;nbsp;&lt;br /&gt;2. 数组的大小。数组的大小不仅仅是System.Array.Length。它还可以包括诸如数组的维数，每个维上的启始边界和结束边界。&lt;/p&gt;&lt;p&gt;&lt;br /&gt;.NET在marshal数组的时候，很大程度上也是从以上两点出发，架起托管世界和本地代码之间的桥梁。根据操作的具体数据类型不同，数组marshal又可以分为以下两个大类，三个小类，我们分别介绍：&lt;/p&gt;&lt;p&gt;&lt;br /&gt;1. 数组作为参数传递&amp;nbsp;&lt;br /&gt;a) c/c++类型的数组&amp;nbsp;&lt;br /&gt;c类型的数组，也就是由指针指明存储空间首地址的数组，是一个自描述很低的数据结构。尽管有些编译器支持在固定偏移量上写入数组的长度，但是因为各个编译器处理的具体方法不同，没有一个标准让CLR来参考。所以我们在marshal一个c类型的数组的时候，不得不用其他方法考虑传递数组的大小，有以下两种：&lt;/p&gt;&lt;p&gt;1) 约定指针数组长度&amp;nbsp;&lt;br /&gt;这种方法需要调用者和被调用者之间有一个约定，给出一个数组长度的固定值。在托管端声明一个interop方法的时候，只需要用SizeConst这个属性，把这个约定告诉CLR。CLR在进行Marshal的时候，会根据这个值，在本地堆上分配相应的空间。&lt;/p&gt;&lt;p&gt;public static extern void Ex(&amp;nbsp;&lt;br /&gt;[In, Out][MarshalAs(UnmanagedType.LPArray,&amp;nbsp;&lt;span style="color: #ff0000; "&gt;SizeParamConst=3&lt;/span&gt;)]string[] a);&lt;/p&gt;&lt;p&gt;&lt;br /&gt;2）通过一个额外的参数指定数组长度&amp;nbsp;&lt;br /&gt;可能有的朋友觉得，约定一个静态的数组长度还不够灵活，希望能够动态的传递数组的长度，从而使得数组marshalling不必受制于固定数组长度的限制。我们先来看普通的函数调用是如何解决这个问题的：caller和callee通过约定一个额外的参数，来传递数组的长度，这可以被视作是一个调用者和被调用者的约定。由于marshalling需要clr的参与，那么就需要把这个约定用clr能够理解的方式，进行扩充，形成一个三方约定。CLR，用属性SizeParamIndex来描述此类约定。&lt;/p&gt;&lt;p&gt;public static extern void Ex2(&amp;nbsp;&lt;br /&gt;[In, Out][MarshalAs(UnmanagedType.LPArray,&amp;nbsp;&lt;span style="color: #ff0000; "&gt;SizeParamIndex=1&lt;/span&gt;)]string[] a,&amp;nbsp;&lt;br /&gt;int len);&lt;/p&gt;&lt;p&gt;&lt;br /&gt;b) SafeArray&amp;nbsp;&lt;br /&gt;SafeArray是COM引入的数据类型，是一个自描述度很高的数据结构。他可以很清楚的告诉用户，该数组的元素类型，数组包含了多少维，每一维的起始位置和终止位置。所以marshal这类safearray的时候，只需要通过设定属性，告诉CLR，当前array对应的本地代码是safearray即可。举例如下：&lt;/p&gt;&lt;p&gt;&lt;br /&gt;public void DumpSafeArrayStringIn(&lt;/p&gt;&lt;p&gt;[In][MarshalAs(&lt;span style="color: #ff0000; "&gt;UnmanagedType.SafeArray&lt;/span&gt;, SafeArraySubType=VarEnum.VT_BSTR)]Object[] array);&lt;/p&gt;&lt;p&gt;大家可以看到，SafeArraySubType可以用来指定数组元素的类型&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;2. 数组作为字段传递&amp;nbsp;&lt;br /&gt;很久以来，对于interop，一直有这样的评价，简单数据结构的marshalling其实并不复杂，但是一旦进入了struct或者class这种你中有我，我中有你的层叠数据结构之后，marshalling就成了bug的温床。所以在这里，我们也要提提数组作为struct/class的一个字段的方法。在这里首先要给这个stuct/class加一个限制，是byval。由于这个限制，大家可以想象的出，CLR在marshal的时候，做的事情是类似于浅copy的内存复制，所以对数组marshal的时候，也就只支持固定长度的数组marshal。&lt;/p&gt;&lt;p&gt;public class StructIntArray&amp;nbsp;&lt;br /&gt;{&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [MarshalAs(&lt;span style="color: #ff0000; "&gt;UnmanagedType.ByValArray&lt;/span&gt;, SizeConst=4)]&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int[] array;&amp;nbsp;&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;&lt;span style="color: #ff0000; "&gt;&lt;br /&gt;&lt;/span&gt;数组作为一种常用的数据结构，各种高级语言都提供了相应的支持，在这些高级语言之间交互操作的时候，数组也是传送集合类型数据的重要结构，希望今天的内容能对大家有所帮助。&lt;/p&gt;&lt;/div&gt;&lt;p&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/2438035.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2012/04/08/2438035.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2012/02/02/2336002.html</id><title type="text">ajax 維護 div 的 scrollbar</title><summary type="text">&lt;!-- Maintain Scroll Position after Asynchronous Postback --&gt;&lt;script type="text/javascript"&gt; var xPos, yPos; var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_beginRequest(BeginRequestHandler); prm.add_endRequest(EndRequestHandler); function BeginRequestHandler(s</summary><published>2012-02-02T08:59:00Z</published><updated>2012-02-02T08:59:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2012/02/02/2336002.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2012/02/02/2336002.html"/><content type="html">&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div&gt;&lt;div&gt;&amp;lt;!-- Maintain Scroll Position after Asynchronous Postback --&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;script type="text/javascript"&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; var xPos, yPos;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; var prm = Sys.WebForms.PageRequestManager.getInstance();&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; prm.add_beginRequest(BeginRequestHandler);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; prm.add_endRequest(EndRequestHandler);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; function BeginRequestHandler(sender, args) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; xPos = $('div[id$=pnlTreeContainer]').scrollLeft();&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; yPos = $('div[id$=pnlTreeContainer]').scrollTop();&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; function EndRequestHandler(sender, args) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $('div[id$=pnlTreeContainer]').scrollLeft(xPos);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; $('div[id$=pnlTreeContainer]').scrollTop(yPos);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;lt;/script&amp;gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/2336002.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2012/02/02/2336002.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2011/10/26/2225032.html</id><title type="text">每日构建的好工具</title><summary type="text">http://sourceforge.net/projects/r2build/</summary><published>2011-10-26T04:16:00Z</published><updated>2011-10-26T04:16:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2011/10/26/2225032.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2011/10/26/2225032.html"/><content type="html">&lt;div&gt;http://sourceforge.net/projects/r2build/&lt;/div&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/2225032.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2011/10/26/2225032.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2011/09/16/2178848.html</id><title type="text">修正了Flex Tree 控件在動態加載節點后 Scrollbar 沒有立即出現的問題</title><summary type="text">var _loadSubFolder:Function = function(children:Array):void{node.children = new ArrayCollection(children);for each(var glossaryfolder:Object in node.children){if(glossaryfolder.child_count &gt; 0){glossaryfolder.children = childrenMark; //replace the template}}//very important for showing tree scrol</summary><published>2011-09-16T07:49:00Z</published><updated>2011-09-16T07:49:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2011/09/16/2178848.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2011/09/16/2178848.html"/><content type="html">&amp;nbsp;&amp;nbsp;&amp;nbsp;var _loadSubFolder:Function = function(children:Array):void{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;node.children = new ArrayCollection(children);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for each(var glossaryfolder:Object in node.children){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(glossaryfolder.child_count &amp;gt; 0){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;glossaryfolder.children = childrenMark; //replace the template&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//very important for showing tree scrollbar.&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var arr:Array = grpTree.openItems as Array;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var openItems:ArrayCollection = new ArrayCollection(arr);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for(var i:int=0;i&amp;lt;openItems.length;i++){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(openItems[i] == node){&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;openItems.removeItemAt(i);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;openItems.refresh();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;grpTree.openItems = openItems.toArray();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;grpTree.expandItem(node, true);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;grpTree.invalidateList();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;grpTree.invalidateDisplayList();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;img src="http://www.cnblogs.com/Koy/aggbug/2178848.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2011/09/16/2178848.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2010/12/13/1904339.html</id><title type="text">根據基本目錄及文件的全路逕，創建相應的子目錄，爲保存文件作準備</title><summary type="text">public static string GenerateDirectoryByFileFullPath(string basePath, string fileFullPath){ DirectoryInfo dirInfo = new DirectoryInfo(basePath); string fileName = new DirectoryInfo(fileFullPath).Name; string subDir = fileFullPath.Replace(basePath, "").Replace(fileName, "").Trim('\\'); return dirInfo</summary><published>2010-12-13T04:00:00Z</published><updated>2010-12-13T04:00:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2010/12/13/1904339.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/12/13/1904339.html"/><content type="html">&lt;div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static string GenerateDirectoryByFileFullPath(string basePath, string fileFullPath){&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DirectoryInfo dirInfo = new DirectoryInfo(basePath);&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string fileName = new DirectoryInfo(fileFullPath).Name;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string subDir = fileFullPath.Replace(basePath, "").Replace(fileName, "").Trim('\\');&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dirInfo.CreateSubdirectory(subDir).FullName;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/div&gt;&lt;/div&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/1904339.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2010/12/13/1904339.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2010/08/16/1800626.html</id><title type="text">轉載：Sqlserver 2005 利用 with 創建臨時表進行遞歸查詢</title><summary type="text">轉載1自：http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html使用SQL2005 递归查询结合Row_Number()实现完全SQL端树排序在实际应用中，我们经常需要用到树型结构功能，数据库结构一般如下即用一个ParentID来标识该节点从属关系。为了最终生成一棵树，一般做法是把记录选出来，然后在程序里递归重新排好序后再呈现出...</summary><published>2010-08-16T04:56:00Z</published><updated>2010-08-16T04:56:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2010/08/16/1800626.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/08/16/1800626.html"/><content type="html">&lt;span style="line-height: 18px; font-family: verdana; font-size: 12px" class="Apple-style-span"&gt;&lt;div style="border-bottom: #676767 1px dashed; border-left: #676767 1px dashed; padding-bottom: 15px; line-height: 21px; background-color: white; padding-left: 15px; padding-right: 15px; font-family: verdana; word-wrap: break-word; background-position: 100% 0%; font-size: 14px; border-top: #676767 1px dashed; border-right: #676767 1px dashed; padding-top: 15px; background-origin: initial; background-clip: initial" class="post"&gt;&lt;div style="border-bottom: red 0px solid; border-left: red 0px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; font-family: verdana; word-wrap: break-word; font-size: 14px; border-top: red 0px solid; font-weight: bold; border-right: red 0px solid; padding-top: 10px" class="postTitle"&gt;&lt;h1 style="border-bottom: red 0px solid; border-left: red 0px solid; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; font-size: 14px; border-top: red 0px solid; font-weight: bold; border-right: red 0px solid; padding-top: 10px"&gt;轉載1自：&lt;a href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html"&gt;http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html&lt;/a&gt;&lt;a style="border-bottom: #efefef 0px dotted; padding-bottom: 10px; color: #78afd3; text-decoration: none" id="ctl04_TitleUrl" class="postTitle2" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html"&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;使用SQL2005 递归查询结合Row_Number()实现完全SQL端树排序&lt;/p&gt;&lt;/div&gt;&lt;p&gt;在实际应用中，我们经常需要用到树型结构功能，数据库结构一般如下&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kingimg/table.jpg" width="440" height="216" /&gt;&lt;/p&gt;&lt;p&gt;即用一个ParentID来标识该节点从属关系。为了最终生成一棵树，一般做法是把记录选出来，然后在程序里递归重新排好序后再呈现出来，但是如果有大量数据，就带来了性能开销问题。那么能不能直接在数据库利用SQL语句排好树后再输出呢？&lt;/p&gt;&lt;p&gt;SQL2005 有个递归查询功能也就是WITH .. AS 语句。对上面这样的表格使用递归查询，可以查询得到某树支下（包括根）的所有节点记录。类似语句如下：&lt;/p&gt;&lt;div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; overflow-x: auto; overflow-y: auto; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; font-family: 'Courier New'; word-wrap: break-word; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;with&amp;nbsp;RelClass&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;(&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;select&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;*&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;from&amp;nbsp;CMS_Site_Class&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;ClassID&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;=&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;1&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;union&amp;nbsp;all&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;select&amp;nbsp;csc.&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;*&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;from&amp;nbsp;CMS_Site_Class&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;csc&amp;nbsp;inner&amp;nbsp;join&amp;nbsp;RelClass&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;rc&amp;nbsp;on&amp;nbsp;csc.ClassID_Parent&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;=&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;rc.ClassID&amp;nbsp;)&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;*&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;from&amp;nbsp;RelClass&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;将得到ClassID为1的根节点下的所有记录：&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kingimg/r.jpg" width="307" height="187" /&gt;&lt;/p&gt;&lt;p&gt;但是这个记录集显然没有经过树排序，这时还需要程序里进一步处理才能输出到客户端。在这里我介绍一种WITH 结合 Row_Number() 实现SQL端排序的方法。&lt;/p&gt;&lt;p&gt;先来看看最终的代码：&lt;/p&gt;&lt;div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; overflow-x: auto; overflow-y: auto; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; font-family: 'Courier New'; word-wrap: break-word; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" id="Code_Closed_Image_110735" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" width="11" height="16" /&gt;&lt;span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; padding-bottom: 2px; background-color: #ffffff; padding-left: 2px; padding-right: 2px; display: inline; font-family: 'Courier New'; border-top: #808080 1px solid; border-right: #808080 1px solid; padding-top: 2px" id="Code_Closed_Text_110735" class="cnblogs_code_Collapse"&gt;Code&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 5px; overflow-x: auto; overflow-y: auto; background-color: #f5f5f5; padding-left: 5px; padding-right: 5px; font-family: 'Courier New'; word-wrap: break-word; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 5px" style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;--&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;=============================================&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;--&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;Author:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;kingimg&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;--&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;Create&amp;nbsp;date:&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;2009&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;-&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;2&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;-&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;5&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;--&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;Description:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;lt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;生成已排序的树&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;--&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;=============================================&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;Create&amp;nbsp;PROCEDURE&amp;nbsp;[dbo].[pCMS_Site_Class__GetList]&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;@ClassID&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;int&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;AS&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;BEGIN&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;with&amp;nbsp;RelClass&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;(&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;select&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;*&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;,&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;0&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;Level,cast(&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800000"&gt;'&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800000"&gt;0&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800000"&gt;'&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;nvarchar(max))&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;treepath&amp;nbsp;from&amp;nbsp;CMS_Site_Class&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;where&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;ClassID&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;=&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;@ClassID&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;union&amp;nbsp;all&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;select&amp;nbsp;csc.&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;*&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;,rc.[Level]&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;+&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;1&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;,rc.treepath&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;+&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;dbo.Lpad(Row_Number()&amp;nbsp;over&amp;nbsp;(order&amp;nbsp;by&amp;nbsp;csc.OrderID&amp;nbsp;desc),&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #800080"&gt;8&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;)&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;treepath&amp;nbsp;from&amp;nbsp;CMS_Site_Class&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;csc&amp;nbsp;inner&amp;nbsp;join&amp;nbsp;RelClass&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #0000ff"&gt;as&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;rc&amp;nbsp;on&amp;nbsp;csc.ClassID_Parent&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;=&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;rc.ClassID&amp;nbsp;)&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;*&lt;/span&gt;&lt;span style="font-family: 'Courier New'; color: #000000"&gt;&amp;nbsp;from&amp;nbsp;RelClass&amp;nbsp;order&amp;nbsp;by&amp;nbsp;treepath&amp;nbsp;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" align="top" src="http://www.cnblogs.com/Images/OutliningIndicators/None.gif" /&gt;END&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;执行以上存储过程，最后就输出结果：&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kingimg/sortedtree.jpg" width="529" height="187" /&gt;&lt;/p&gt;&lt;p&gt;这棵树已经从上到下按树结构排好序了！程序里只要原样输出即可！&lt;/p&gt;&lt;p&gt;Lpad函数将指定Int型数据左填0，按指定位数输出。关于为什么要用nvarchar(max)的问题，因为其它固定长度时，在递归查询里的rc.treepath&amp;nbsp;&lt;span style="color: #000000"&gt;+&lt;/span&gt;&lt;span style="color: #000000"&gt;&amp;nbsp;dbo.Lpad(..)时会改变长度，导致查询错误，使用max长度就避免了这个问题。当然，你也可以用固定长度，相加后再convert回来。&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #000000"&gt;好了，这样子我们就实现了完全SQL端生成已排序的树的目的了，完全脱离了程序处理，这个方法看起来效率还不错呢~&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color: #000000"&gt;我在此抛砖引玉了，各位如果有可以改进的地方，请贴出来~&lt;/span&gt;&lt;/p&gt;&lt;/div&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" alt="" src="http://www.cnblogs.com/kingimg/aggbug/1384410.html?type=1&amp;amp;webview=1" width="1" height="1" /&gt;&amp;nbsp;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="pagedcomment"&gt;&lt;/a&gt;&lt;br /&gt;&lt;div style="border-bottom: #cccccc 1px dotted; margin: 0px 0px 10px; font-family: verdana; word-wrap: break-word; font-size: 14px; font-weight: bold" class="feedback_area_title"&gt;发表评论&lt;/div&gt;&lt;div style="font-family: verdana; word-wrap: break-word" class="feedbackNoItems"&gt;&lt;/div&gt;&lt;div style="margin: 0px 0px 10px; font-family: verdana; word-wrap: break-word" class="feedbackItem"&gt;&lt;div style="border-bottom: #cccccc 1px dotted; font-family: verdana; word-wrap: break-word; margin-bottom: 10px; color: #4f4f4f; font-weight: bold" class="feedbackListSubtitle"&gt;&lt;div style="font-family: verdana; word-wrap: break-word; float: right; font-weight: normal" class="feedbackManage"&gt;　&lt;a style="color: #666666; text-decoration: none" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html#commentform"&gt;回复&lt;/a&gt;　&lt;a style="color: #666666; text-decoration: none" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html#commentform"&gt;引用&lt;/a&gt;　&lt;a style="color: #666666; text-decoration: none" title="查看该作者发表过的评论" href="http://www.cnblogs.com/CommentsByAuthor.aspx?author=%e7%8b%bcRobot" target="_blank"&gt;查看&lt;/a&gt;　&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;a style="color: #666666; text-decoration: none" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html#1443384"&gt;#1楼&lt;/a&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" id="comment_anchor_1443384" name="1443384"&gt;&lt;/a&gt;&amp;nbsp;2009-02-05 12:39 |&amp;nbsp;&lt;a style="color: #666666; text-decoration: none" id="Comments1_CommentList_ctl00_NameLink" class="feedbackmanagelink" href="http://www.cnblogs.com/robot/" target="_blank"&gt;狼Robot&lt;/a&gt;&amp;nbsp;&lt;a style="background-image: url(http://images.cnblogs.com/icoMsg.gif); background-position: 0% 0%; color: #666666; font-size: 12px; text-decoration: none; background-origin: initial; background-clip: initial" class="sendMsg2This" title="给此人发送站内短消息" href="http://space.cnblogs.com/msg/send/%e7%8b%bcRobot"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;&lt;div style="font-family: verdana; word-wrap: break-word; margin-bottom: 20px" class="feedbackCon"&gt;&lt;span id="comment_body_1443384" class="blog_comment_body"&gt;不错不错。以前在程序里递归，总觉得麻烦。&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px 0px 10px; font-family: verdana; word-wrap: break-word" class="feedbackItem"&gt;&lt;div style="border-bottom: #cccccc 1px dotted; font-family: verdana; word-wrap: break-word; margin-bottom: 10px; color: #4f4f4f; font-weight: bold" class="feedbackListSubtitle"&gt;&lt;div style="font-family: verdana; word-wrap: break-word; float: right; font-weight: normal" class="feedbackManage"&gt;　&lt;a style="color: #666666; text-decoration: none" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html#commentform"&gt;回复&lt;/a&gt;　&lt;a style="color: #666666; text-decoration: none" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html#commentform"&gt;引用&lt;/a&gt;　&lt;a style="color: #666666; text-decoration: none" title="查看该作者发表过的评论" href="http://www.cnblogs.com/CommentsByAuthor.aspx?author=nicye" target="_blank"&gt;查看&lt;/a&gt;　&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;a style="color: #666666; text-decoration: none" href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html#1443389"&gt;#2楼&lt;/a&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" id="comment_anchor_1443389" name="1443389"&gt;&lt;/a&gt;&amp;nbsp;2009-02-05 12:46 |&amp;nbsp;&lt;a style="color: #666666; text-decoration: none" id="Comments1_CommentList_ctl01_NameLink" class="feedbackmanagelink" href="http://www.cnblogs.com/kellynic/" target="_blank"&gt;nicye&lt;/a&gt;&amp;nbsp;&lt;a style="background-image: url(http://images.cnblogs.com/icoMsg.gif); background-position: 0% 0%; color: #666666; font-size: 12px; text-decoration: none; background-origin: initial; background-clip: initial" class="sendMsg2This" title="给此人发送站内短消息" href="http://space.cnblogs.com/msg/send/nicye"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/a&gt;&lt;/div&gt;&lt;div style="font-family: verdana; word-wrap: break-word; margin-bottom: 20px" class="feedbackCon"&gt;&lt;span id="comment_body_1443389" class="blog_comment_body"&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kellynic/001.jpg" width="346" height="372" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kellynic/002.jpg" width="490" height="456" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kellynic/003.jpg" width="381" height="270" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="" src="http://images.cnblogs.com/cnblogs_com/kellynic/004.jpg" width="395" height="167" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;做这样的功能，熟悉NicPetshop的话只需要1分钟，几乎都是生成的&lt;/p&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong style="color: #ff0000"&gt;再附上MSDN的解說：&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;span style="line-height: normal; font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 23px; font-weight: bold" class="Apple-style-span"&gt;使用公用表表达式的递归查询&lt;/span&gt;&lt;/p&gt;&lt;span style="line-height: normal; font-family: 'Segoe UI', Verdana, Arial; font-size: 13px" class="Apple-style-span"&gt;&lt;div style="padding-top: 10px" id="mainSection"&gt;&lt;div id="mainBody"&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;span id="changeHistory"&gt;&lt;/span&gt;&lt;p&gt;公用表表达式 (CTE) 具有一个重要的优点，那就是能够引用其自身，从而创建递归 CTE。递归 CTE 是一个重复执行初始 CTE 以返回数据子集直到获取完整结果集的公用表表达式。&lt;/p&gt;&lt;p&gt;在 SQL Server 2005 中，当某个查询引用递归 CTE 时，它即被称为&amp;#8220;递归查询&amp;#8221;。递归查询通常用于返回分层数据，例如：显示某个组织图中的雇员或物料清单方案（其中父级产品有一个或多个组件，而那些组件可能还有子组件，或者是其他父级产品的组件）中的数据。&lt;/p&gt;&lt;p&gt;递归 CTE 可以极大地简化在 SELECT、INSERT、UPDATE、DELETE 或 CREATE VIEW 语句中运行递归查询所需的代码。在 SQL Server 的早期版本中，递归查询通常需要使用临时表、游标和逻辑来控制递归步骤流。有关公用表表达式的详细信息，请参阅&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms190766(v=SQL.90).aspx"&gt;使用公用表表达式&lt;/a&gt;。&lt;/p&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div style="margin-top: 19px; margin-bottom: 19px" class="LW_CollapsibleArea_TitleDiv"&gt;&lt;span style="font-family: 'Segoe UI', Verdana, Arial; float: left; color: #3f529c; font-size: 1.53em; font-weight: bold" class="LW_CollapsibleArea_Title"&gt;递归 CTE 的结构&lt;/span&gt; &lt;div style="padding-top: 12px" class="LW_CollapsibleArea_HrDiv"&gt;&lt;hr style="border-bottom: #e5e5e5 1px solid; border-right-style: none; border-top-style: none; color: #e5e5e5; margin-left: 4px; border-left-style: none" class="LW_CollapsibleArea_Hr" /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a id="sectionToggle0" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/a&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Transact-SQL 中的递归 CTE 的结构与其他编程语言中的递归例程相似。尽管其他语言中的递归例程返回标量值，但递归 CTE 可以返回多行。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;递归 CTE 由下列三个元素组成：&lt;/p&gt;&lt;ol xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;li&gt;例程的调用。&lt;br /&gt;递归 CTE 的第一个调用包括一个或多个由 UNION ALL、UNION、EXCEPT 或 INTERSECT 运算符联接的&amp;nbsp;&lt;em&gt;CTE_query_definitions&lt;/em&gt;。由于这些查询定义形成了 CTE 结构的基准结果集，所以它们被称为&amp;#8220;定位点成员&amp;#8221;。&lt;br /&gt;&lt;em&gt;CTE_query_definitions&amp;nbsp;&lt;/em&gt;被视为定位点成员，除非它们引用了 CTE 本身。所有定位点成员查询定义必须放置在第一个递归成员定义之前，而且必须使用 UNION ALL 运算符联接最后一个定位点成员和第一个递归成员。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;例程的递归调用。&lt;br /&gt;递归调用包括一个或多个由引用 CTE 本身的 UNION ALL 运算符联接的&amp;nbsp;&lt;em&gt;CTE_query_definitions&lt;/em&gt;。这些查询定义被称为&amp;#8220;递归成员&amp;#8221;。&amp;nbsp;&lt;br /&gt;&lt;/li&gt;&lt;li&gt;终止检查。&amp;nbsp;&lt;br /&gt;终止检查是隐式的；当上一个调用中未返回行时，递归将停止。&amp;nbsp;&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;div style="border-bottom: #bbbbbb 1px solid; border-left: #bbbbbb 1px solid; background-color: #fcfec5; margin-bottom: 10px; border-top: #bbbbbb 1px solid; border-right: #bbbbbb 1px solid" class="alert" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;table style="border-bottom: #bbbbbb 0px solid; border-left: #bbbbbb 0px solid; width: 790px; border-collapse: collapse; border-top: #bbbbbb 0px solid; border-right: #bbbbbb 0px solid"&gt;&lt;tbody&gt;&lt;tr style="vertical-align: top"&gt;&lt;th style="border-bottom: #bbbbbb 0px hidden; text-align: left; border-left: #bbbbbb 0px hidden; padding-bottom: 1px; background-color: #fcfec5; font-style: normal; padding-left: 11px; padding-right: 11px; border-collapse: collapse; font-family: 'Segoe UI', Verdana, Arial; height: 21px; color: #000000; font-size: 1.07em; border-top: #bbbbbb 0px hidden; font-weight: bold; border-right: #bbbbbb 0px hidden; padding-top: 10px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px"&gt;&lt;img style="background-image: url(http://i3.msdn.microsoft.com/Hash/bda288328f5eabd19cfdf9486efda99d.png); border-right-width: 0px; overflow-x: hidden; overflow-y: hidden; width: 10px; background-attachment: scroll; padding-right: 5px; background-position: -1825px -3px; border-top-width: 0px; border-bottom-width: 0px; height: 10px; border-left-width: 0px; background-origin: initial; background-clip: initial" id="note" class="cl_IC101471" alt="ms186243.note(zh-cn,SQL.90).gif" src="http://i.msdn.microsoft.com/Hash/030c41d9079671d09a62d8e2c1db6973.gif" xmlns="" /&gt;注意：&lt;/th&gt;&lt;/tr&gt;&lt;tr style="vertical-align: top"&gt;&lt;td style="border-bottom: #bbbbbb 0px solid; border-left: #bbbbbb 0px solid; padding-bottom: 10px; line-height: 18px; background-color: #fcfec5; margin: 1px; padding-left: 11px; padding-right: 11px; border-top: #bbbbbb 0px solid; border-right: #bbbbbb 0px solid; padding-top: 1px"&gt;如果递归 CTE 组合不正确，可能会导致无限循环。例如，如果递归成员查询定义对父列和子列返回相同的值，则会造成无限循环。在测试递归查询的结果时，可以通过在 INSERT、UPDATE、DELETE 或 SELECT 语句的 OPTION 子句中使用 MAXRECURSION 提示和 0 到 32,767 之间的值，来限制特定语句允许的递归级数。有关详细信息，请参阅&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms181714(v=SQL.90).aspx"&gt;查询提示 (Transact-SQL)&lt;/a&gt;&amp;nbsp;和&amp;nbsp;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms175972(v=SQL.90).aspx"&gt;WITH common_table_expression (Transact-SQL)&lt;/a&gt;。 &lt;p&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;&lt;h3 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1.07em; font-weight: bold" class="subHeading" xmlns="http://www.w3.org/1999/xhtml"&gt;伪代码和语义&lt;/strong&gt;&lt;/p&gt;&lt;div class="subSection" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;p&gt;递归 CTE 结构必须至少包含一个定位点成员和一个递归成员。以下伪代码显示了包含一个定位点成员和一个递归成员的简单递归 CTE 的组件。&lt;/p&gt;&lt;p&gt;&lt;code&gt;WITH cte_name ( column_name [,...n] )&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;AS&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;(&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;CTE_query_definition &amp;#8211;- Anchor member is defined.&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;UNION ALL&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;CTE_query_definition &amp;#8211;- Recursive member is defined referencing cte_name.&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;)&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;-- Statement using the CTE&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;SELECT *&lt;/code&gt;&lt;/p&gt;&lt;p&gt;&lt;code&gt;FROM cte_name&lt;/code&gt;&lt;/p&gt;&lt;p&gt;递归执行的语义如下：&lt;/p&gt;&lt;ol&gt;&lt;li&gt;将 CTE 表达式拆分为定位点成员和递归成员。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;运行定位点成员，创建第一个调用或基准结果集 (T&lt;span class="sub"&gt;0&lt;/span&gt;)。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;运行递归成员，将 T&lt;span class="sub"&gt;i&lt;/span&gt;&amp;nbsp;作为输入，将 T&lt;span class="sub"&gt;i+1&lt;/span&gt;&amp;nbsp;作为输出。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;重复步骤 3，直到返回空集。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;返回结果集。这是对 T&lt;span class="sub"&gt;0&lt;/span&gt;&amp;nbsp;到 T&lt;span class="sub"&gt;n&lt;/span&gt;&amp;nbsp;执行 UNION ALL 的结果。&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div style="margin-top: 19px; margin-bottom: 19px" class="LW_CollapsibleArea_TitleDiv"&gt;&lt;span style="font-family: 'Segoe UI', Verdana, Arial; float: left; color: #3f529c; font-size: 1.53em; font-weight: bold" class="LW_CollapsibleArea_Title"&gt;示例&lt;/span&gt; &lt;div style="padding-top: 12px" class="LW_CollapsibleArea_HrDiv"&gt;&lt;hr style="border-bottom: #e5e5e5 1px solid; border-right-style: none; border-top-style: none; color: #e5e5e5; margin-left: 4px; border-left-style: none" class="LW_CollapsibleArea_Hr" /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a id="sectionToggle1" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/a&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;以下示例通过返回 Adventure Works Cycles 公司的雇员的分层列表（从最高级雇员开始）显示递归 CTE 结构的语义。执行 CTE 的语句将结果集限制到研发组中的雇员。示例后面是代码执行的演练。&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="CodeSpippet0"&gt;&lt;/a&gt;&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" class="LW_CodeSnippetContainerCodeCollection"&gt;&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" class="LW_CodeSnippetToolBar"&gt;&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" class="LW_CodeSnippetToolBarText"&gt;&lt;a style="background-color: white; padding-left: 4px; padding-right: 4px; font-family: 'Segoe UI', Verdana, Arial; color: #1364c4; margin-left: 5px; font-size: 10pt; margin-right: 5px; text-decoration: none" title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/ms186243(SQL.90).aspx"&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode0" class="LW_CodeSnippetContainerCode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;USE AdventureWorks;&lt;br/&gt;GO&lt;br/&gt;WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level)&lt;br/&gt;AS&lt;br/&gt;(&lt;br/&gt;-- Anchor member definition&lt;br/&gt;SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID,&lt;br/&gt;0 AS Level&lt;br/&gt;FROM HumanResources.Employee AS e&lt;br/&gt;INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh&lt;br/&gt;ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL&lt;br/&gt;WHERE ManagerID IS NULL&lt;br/&gt;UNION ALL&lt;br/&gt;-- Recursive member definition&lt;br/&gt;SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID,&lt;br/&gt;Level + 1&lt;br/&gt;FROM HumanResources.Employee AS e&lt;br/&gt;INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh&lt;br/&gt;ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL&lt;br/&gt;INNER JOIN DirectReports AS d&lt;br/&gt;ON e.ManagerID = d.EmployeeID&lt;br/&gt;)&lt;br/&gt;-- Statement that executes the CTE&lt;br/&gt;SELECT ManagerID, EmployeeID, Title, Level&lt;br/&gt;FROM DirectReports&lt;br/&gt;INNER JOIN HumanResources.Department AS dp&lt;br/&gt;ON DirectReports.DeptID = dp.DepartmentID&lt;br/&gt;WHERE dp.GroupName = N'Research and Development' OR Level = 0;&lt;br/&gt;GO&lt;br/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;h3 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1.07em; font-weight: bold" class="subHeading" xmlns="http://www.w3.org/1999/xhtml"&gt;示例代码演练&lt;/strong&gt;&lt;/p&gt;&lt;div class="subSection" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;ol&gt;&lt;li&gt;递归 CTE&amp;nbsp;&lt;code&gt;DirectReports&lt;/code&gt;&amp;nbsp;定义了一个定位点成员和一个递归成员。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;定位点成员返回基准结果集 T&lt;span class="sub"&gt;0&lt;/span&gt;。这就是公司中的最高级雇员，即不向经理报告的雇员。&amp;nbsp;&lt;br /&gt;以下是定位点成员返回的结果集：&amp;nbsp;&lt;br /&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="CodeSpippet1"&gt;&lt;/a&gt;&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" class="LW_CodeSnippetContainerCodeCollection"&gt;&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" class="LW_CodeSnippetToolBar"&gt;&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" class="LW_CodeSnippetToolBarText"&gt;&lt;a style="background-color: white; padding-left: 4px; padding-right: 4px; font-family: 'Segoe UI', Verdana, Arial; color: #1364c4; margin-left: 5px; font-size: 10pt; margin-right: 5px; text-decoration: none" title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/ms186243(SQL.90).aspx"&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode1" class="LW_CodeSnippetContainerCode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;ManagerID EmployeeID Title                                   Level&lt;br/&gt;    --------- ---------- --------------------------------------- ------&lt;br/&gt;    NULL      109        Chief Executive Officer                 0&lt;br/&gt;    &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;递归成员返回定位点成员结果集中的雇员的直接下属。这是通过在&amp;nbsp;&lt;code&gt;Employee&lt;/code&gt;&amp;nbsp;表和&amp;nbsp;&lt;code&gt;DirectReports&lt;/code&gt;&amp;nbsp;CTE 之间执行联接操作获得的。正是此次对 CTE 自身的引用建立了递归调用。利用 CTE&amp;nbsp;&lt;code&gt;DirectReports&amp;nbsp;&lt;/code&gt;中的雇员作为输入 (T&lt;span class="sub"&gt;i&lt;/span&gt;)，联接&amp;nbsp;&lt;code&gt;(Employee.ManagerID = DirectReports.EmployeeID&lt;/code&gt;) 返回经理为 (T&lt;span class="sub"&gt;i&lt;/span&gt;) 的雇员作为输出 (T&lt;span class="sub"&gt;i+1&lt;/span&gt;)。这样，递归成员的第一次迭代返回了以下结果集：&amp;nbsp;&lt;br /&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="CodeSpippet2"&gt;&lt;/a&gt;&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" class="LW_CodeSnippetContainerCodeCollection"&gt;&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" class="LW_CodeSnippetToolBar"&gt;&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" class="LW_CodeSnippetToolBarText"&gt;&lt;a style="background-color: white; padding-left: 4px; padding-right: 4px; font-family: 'Segoe UI', Verdana, Arial; color: #1364c4; margin-left: 5px; font-size: 10pt; margin-right: 5px; text-decoration: none" title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/ms186243(SQL.90).aspx"&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode2" class="LW_CodeSnippetContainerCode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;ManagerID EmployeeID Title                                   Level&lt;br/&gt;    --------- ---------- --------------------------------------- ------&lt;br/&gt;    109       12         Vice President of Engineering           1&lt;br/&gt;    &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;重复激活递归成员。递归成员的第二次迭代使用步骤 3 中的单行结果集（包含&amp;nbsp;&lt;code&gt;EmployeeID&lt;/code&gt;&lt;code&gt;12&lt;/code&gt;）作为输入值，并返回以下结果集：&amp;nbsp;&lt;br /&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="CodeSpippet3"&gt;&lt;/a&gt;&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" class="LW_CodeSnippetContainerCodeCollection"&gt;&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" class="LW_CodeSnippetToolBar"&gt;&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" class="LW_CodeSnippetToolBarText"&gt;&lt;a style="background-color: white; padding-left: 4px; padding-right: 4px; font-family: 'Segoe UI', Verdana, Arial; color: #1364c4; margin-left: 5px; font-size: 10pt; margin-right: 5px; text-decoration: none" title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/ms186243(SQL.90).aspx"&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode3" class="LW_CodeSnippetContainerCode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;ManagerID EmployeeID Title                                   Level&lt;br/&gt;    --------- ---------- --------------------------------------- ------&lt;br/&gt;    12        3          Engineering Manager                     2&lt;br/&gt;    &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;递归成员的第三次迭代使用上面的单行结果集（包含&amp;nbsp;&lt;code&gt;EmployeeID&lt;/code&gt;&lt;code&gt;3)&lt;/code&gt;）作为输入值，并返回以下结果集：&amp;nbsp;&lt;br /&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="CodeSpippet4"&gt;&lt;/a&gt;&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" class="LW_CodeSnippetContainerCodeCollection"&gt;&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" class="LW_CodeSnippetToolBar"&gt;&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" class="LW_CodeSnippetToolBarText"&gt;&lt;a style="background-color: white; padding-left: 4px; padding-right: 4px; font-family: 'Segoe UI', Verdana, Arial; color: #1364c4; margin-left: 5px; font-size: 10pt; margin-right: 5px; text-decoration: none" title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/ms186243(SQL.90).aspx"&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode4" class="LW_CodeSnippetContainerCode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;ManagerID EmployeeID Title                                   Level&lt;br/&gt;    --------- ---------- --------------------------------------- ------&lt;br/&gt;    3         4          Senior Tool Designer                    3&lt;br/&gt;    3         9          Design Engineer                         3&lt;br/&gt;    3         11         Design Engineer                         3&lt;br/&gt;    3         158        Research and Development Manager        3&lt;br/&gt;    3         263        Senior Tool Designer                    3&lt;br/&gt;    3         267        Senior Design Engineer                  3&lt;br/&gt;    3         270        Design Engineer                         3&lt;br/&gt;    &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;递归成员的第四次迭代使用&amp;nbsp;&lt;code&gt;EmployeeID&lt;/code&gt;&amp;nbsp;值&amp;nbsp;&lt;code&gt;4&lt;/code&gt;、&lt;code&gt;9&lt;/code&gt;、&lt;code&gt;11&lt;/code&gt;、&lt;code&gt;158&lt;/code&gt;、&lt;code&gt;263&lt;/code&gt;、&lt;code&gt;267&lt;/code&gt;&amp;nbsp;和&amp;nbsp;&lt;code&gt;270&lt;/code&gt;&amp;nbsp;的上一个行集作为输入值。&amp;nbsp;&lt;br /&gt;重复此过程，直到递归成员返回一个空结果集。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;正在运行的查询返回的最终结果集是定位点成员和递归成员生成的所有结果集的并集。&amp;nbsp;&lt;br /&gt;以下是示例返回的完整结果集：&lt;br /&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a style="background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); text-indent: 20px; width: 20px; height: 20px" name="CodeSpippet5"&gt;&lt;/a&gt;&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" class="LW_CodeSnippetContainerCodeCollection"&gt;&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" class="LW_CodeSnippetToolBar"&gt;&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" class="LW_CodeSnippetToolBarText"&gt;&lt;a style="background-color: white; padding-left: 4px; padding-right: 4px; font-family: 'Segoe UI', Verdana, Arial; color: #1364c4; margin-left: 5px; font-size: 10pt; margin-right: 5px; text-decoration: none" title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/ms186243(SQL.90).aspx"&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode5" class="LW_CodeSnippetContainerCode"&gt;&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;ManagerID EmployeeID Title                                   Level&lt;br/&gt;    --------- ---------- --------------------------------------- ------&lt;br/&gt;    NULL      109        Chief Executive Officer                 0&lt;br/&gt;    109       12         Vice President of Engineering           1&lt;br/&gt;    12        3          Engineering Manager                     2&lt;br/&gt;    3         4          Senior Tool Designer                    3&lt;br/&gt;    3         9          Design Engineer                         3&lt;br/&gt;    3         11         Design Engineer                         3&lt;br/&gt;    3         158        Research and Development Manager        3&lt;br/&gt;    3         263        Senior Tool Designer                    3&lt;br/&gt;    3         267        Senior Design Engineer                  3&lt;br/&gt;    3         270        Design Engineer                         3&lt;br/&gt;    263       5          Tool Designer                           4&lt;br/&gt;    263       265        Tool Designer                           4&lt;br/&gt;    158       79         Research and Development Engineer       4&lt;br/&gt;    158       114        Research and Development Engineer       4&lt;br/&gt;    158       217        Research and Development Manager        4&lt;br/&gt;    (15 row(s) affected)&lt;br/&gt;    &lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div style="margin-top: 19px; margin-bottom: 19px" class="LW_CollapsibleArea_TitleDiv"&gt;&lt;span style="font-family: 'Segoe UI', Verdana, Arial; float: left; color: #3f529c; font-size: 1.53em; font-weight: bold" class="LW_CollapsibleArea_Title"&gt;请参阅&lt;/span&gt; &lt;div style="padding-top: 12px" class="LW_CollapsibleArea_HrDiv"&gt;&lt;hr style="border-bottom: #e5e5e5 1px solid; border-right-style: none; border-top-style: none; color: #e5e5e5; margin-left: 4px; border-left-style: none" class="LW_CollapsibleArea_Hr" /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a id="seeAlsoToggle" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/a&gt;&lt;h4 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1em; font-weight: bold" class="subHeading" xmlns="http://www.w3.org/1999/xhtml"&gt;概念&lt;/strong&gt;&lt;/p&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms190766(v=SQL.90).aspx"&gt;使用公用表表达式&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;h4 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1em; font-weight: bold" class="subHeading" xmlns="http://www.w3.org/1999/xhtml"&gt;其他资源&lt;/strong&gt;&lt;/p&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms175972(v=SQL.90).aspx"&gt;WITH common_table_expression (Transact-SQL)&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms181714(v=SQL.90).aspx"&gt;查询提示 (Transact-SQL)&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms174335(v=SQL.90).aspx"&gt;INSERT (Transact-SQL)&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms177523(v=SQL.90).aspx"&gt;UPDATE (Transact-SQL)&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms189835(v=SQL.90).aspx"&gt;DELETE (Transact-SQL)&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;a style="color: #1364c4; text-decoration: none" href="http://msdn.microsoft.com/zh-cn/library/ms188055(v=SQL.90).aspx"&gt;EXCEPT 和 INTERSECT (Transact-SQL)&lt;/a&gt;&lt;br xmlns="http://www.w3.org/1999/xhtml" /&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/1800626.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2010/08/16/1800626.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2010/08/11/1797508.html</id><title type="text">計算某天屬於當月的第幾週的簡單方法(最終版)</title><summary type="text">using System;using System.Collections.Generic;using System.Text;namespace PMSLib.Utils { public sealed class SpecialDateTime { private int _year, _month, _day; private int _week; private int _maxDays;...</summary><published>2010-08-11T09:35:00Z</published><updated>2010-08-11T09:35:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2010/08/11/1797508.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/08/11/1797508.html"/><content type="html">&lt;div&gt;&lt;p&gt;&lt;p&gt;using System;&lt;/p&gt;&lt;p&gt;using System.Collections.Generic;&lt;/p&gt;&lt;p&gt;using System.Text;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;namespace PMSLib.Utils {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;public sealed class SpecialDateTime {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private int _year, _month, _day;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private int _week;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;private int _maxDays;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public SpecialDateTime(int year, int month, int day) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_year = year;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_month = month;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_day = day;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_maxDays = DateTime.DaysInMonth(year, month);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public SpecialDateTime(int year, int month) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_year = year;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_month = month;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_day = 1;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;_maxDays = DateTime.DaysInMonth(year, month);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public SpecialDateTime() { }&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int Removal {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int removal = 7 - (int)(new DateTime(_year, _month, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int Markup {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (6 - (int)(new DateTime(_year, _month, _maxDays).DayOfWeek));&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int WeeksInMonth {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (_maxDays + Markup - Removal) / 7;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public int DaysInMonth {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return (EndDateOfMonth - StartDateOfMonth).Days + 1; }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public DateTime StartDateOfMonth {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return new DateTime(_year, _month, 1).AddDays(Removal); }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public DateTime EndDateOfMonth {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;get { return new DateTime(_year, _month, _maxDays).AddDays(Markup); }&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void GetWeekInMonthBy(int year, int month, int day, out int realMonth, out int week) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int removal = 7 - (int)(new DateTime(year, month, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;removal = removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TimeSpan ts = new DateTime(year, month, day) - new DateTime(year, month, 1).AddDays(removal);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(ts.Days &amp;lt; 0) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;realMonth = month - 1;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;removal = 7 - (int)(new DateTime(year, realMonth, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;removal = removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;TimeSpan ts1 = new DateTime(year, month, day) - new DateTime(year, realMonth, 1).AddDays(removal);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;week = (int)Math.Ceiling(ts1.Days == 0 ? 1 : (ts1.Days) * 1.0 / 7);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;realMonth = month;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;week = (int)Math.Ceiling(ts.Days == 0 ? 1 : ts.Days * 1.0 / 7);&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(ts.Days != 0 &amp;amp;&amp;amp; ts.Days % 7 == 0) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;week = week + 1;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public DateTime GetDateBy(int year, int month, int weekInMonth, DayOfWeek dayOfWeek) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(month &amp;gt; 12) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;year += month / 12;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;month = month % 12;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int removal = 7 - (int)(new DateTime(year, month, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;removal = removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateTime startDate = new DateTime(year, month, 1).AddDays(removal);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return startDate.AddDays((weekInMonth - 1) * 7 + (int)dayOfWeek);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public void GetDateRangeInWeek(int year, int month, int week, out DateTime startDate, out DateTime endDate) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;startDate = (new DateTime(year, month, 1).AddDays(Removal).AddDays((week - 1) * 7));&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;endDate = (new DateTime(year, month, 1).AddDays(Removal).AddDays((week - 1) * 7 + 6));&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IList&amp;lt;DateTime&amp;gt; GetDatesBy(int minWeekInMonth, DayOfWeek dow, int interval) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IList&amp;lt;DateTime&amp;gt; dates = new List&amp;lt;DateTime&amp;gt;();&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while(GetDateBy(_year, _month, minWeekInMonth, dow) &amp;lt;= EndDateOfMonth) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dates.Add(GetDateBy(_year, _month, minWeekInMonth, dow));&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;minWeekInMonth += interval;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dates;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public IList&amp;lt;DateTime&amp;gt; GetDatesBy(DateTime date, int interval, string unit, int realMonth) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;IList&amp;lt;DateTime&amp;gt; dates = new List&amp;lt;DateTime&amp;gt;();&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SpecialDateTime sdt = new SpecialDateTime(date.Year, realMonth);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(unit.Equals("W")) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;do {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateTime d = date.AddDays(7 * interval);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(sdt.StartDateOfMonth &amp;gt; d || d &amp;gt; sdt.EndDateOfMonth) break;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dates.Add(d);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;date = d;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} while(sdt.StartDateOfMonth &amp;lt;= date &amp;amp;&amp;amp; date &amp;lt;= sdt.EndDateOfMonth);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int week, month;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sdt.GetWeekInMonthBy(date.Year, date.Month, date.Day, out month, out week);&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DateTime d = sdt.GetDateBy(date.Year, month + interval, week, date.DayOfWeek);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(realMonth == month + interval) {&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dates.Add(d);&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return dates;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;/p&gt;&lt;/div&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/1797508.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2010/08/11/1797508.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2010/08/11/1797151.html</id><title type="text">轉載：Case具有两种格式。简单Case函数和Case搜索函数。</title><summary type="text">轉載自：http://topic.csdn.net/u/20090304/15/e827ea69-3d56-4557-abae-d5701ee24330.htmlCase具有两种格式。简单Case函数和Case搜索函数。 --简单Case函数CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女'ELSE '其他' END--Case搜索函数CASE WHEN se...</summary><published>2010-08-11T03:05:00Z</published><updated>2010-08-11T03:05:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2010/08/11/1797151.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/08/11/1797151.html"/><content type="html">&lt;span class="Apple-style-span" style="font-family: simsun; line-height: 23px; font-size: 12px; "&gt;&lt;div&gt;&lt;font class="Apple-style-span" color="#008080"&gt;&lt;span class="Apple-style-span" style="line-height: 18px;"&gt;轉載自：&lt;span class="Apple-style-span" style="color: #000000; line-height: 23px; "&gt;&lt;a href="http://topic.csdn.net/u/20090304/15/e827ea69-3d56-4557-abae-d5701ee24330.html"&gt;http://topic.csdn.net/u/20090304/15/e827ea69-3d56-4557-abae-d5701ee24330.html&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/div&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;span class="Apple-style-span" style="line-height: 23px; "&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;Case具有两种格式。简单Case函数和Case搜索函数。 &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;--&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;简单Case函数&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; sex&lt;br/&gt;         &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;男&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;         &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;女&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;其他&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;--&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;Case搜索函数&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; sex &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;男&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;         &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; sex &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;女&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;其他&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;br/&gt;这两种方式，可以实现相同的功能。简单Case函数的写法相对比较简洁，但是和Case搜索函数相比，功能方面会有些限制，比如写判断式。 &lt;br/&gt;还有一个需要注意的问题，Case函数只返回第一个符合条件的值，剩下的Case部分将会被自动忽略。 &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;--&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;比如说，下面这段SQL，你永远无法得到&amp;#8220;第二类&amp;#8221;这个结果&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; col_1 &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;IN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; ( &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;a&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;, &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;b&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;) &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;第一类&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;         &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; col_1 &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;IN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; (&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;a&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;)       &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;第二类&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;其他&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;br/&gt;下面我们来看一下，使用Case函数都能做些什么事情。 &lt;br/&gt;&lt;br/&gt;一，已知数据按照另外一种方式进行分组，分析。 &lt;br/&gt;&lt;br/&gt;有如下数据:(为了看得更清楚，我并没有使用国家代码，而是直接用国家名作为Primary &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;Key&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;) &lt;br/&gt;国家（country）    人口（population）&lt;br/&gt;中国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;600&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;美国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;100&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;加拿大    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;100&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;英国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;200&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;法国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;300&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;日本    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;250&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;德国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;200&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;墨西哥    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;50&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;印度    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;250&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;br/&gt;根据这个国家人口数据，统计亚洲和北美洲的人口数量。应该得到下面这个结果。 &lt;br/&gt;洲    人口&lt;br/&gt;亚洲    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1100&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;北美洲    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;250&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;其他    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;700&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;br/&gt;想要解决这个问题，你会怎么做？生成一个带有洲Code的View，是一个解决方法，但是这样很难动态的改变统计的方式。 &lt;br/&gt;如果使用Case函数，SQL代码如下: &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;SELECT&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;SUM&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;(population),&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; country&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;中国&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;亚洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;印度&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;亚洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;日本&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;亚洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;美国&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;北美洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;加拿大&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;北美洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;墨西哥&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;北美洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;其他&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;FROM&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    Table_A&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;GROUP&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;BY&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; country&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;中国&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;亚洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;印度&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;亚洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;日本&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;亚洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;美国&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;     &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;北美洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;加拿大&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;北美洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;墨西哥&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;北美洲&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;其他&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;;&lt;br/&gt;&lt;br/&gt;同样的，我们也可以用这个方法来判断工资的等级，并统计每一等级的人数。SQL代码如下； &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;SELECT&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;500&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;             &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;500&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;600&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;             &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;600&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;800&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;3&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;             &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;800&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1000&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;4&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;NULL&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary_class,&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;COUNT&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;(&lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;*&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;)&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;FROM&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    Table_A&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;GROUP&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;BY&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;500&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;             &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;500&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;600&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;             &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;600&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;800&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;3&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;             &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;800&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;lt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1000&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;4&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;        &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;NULL&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;;&lt;br/&gt;&lt;br/&gt;二，用一个SQL语句完成不同条件的分组。 &lt;br/&gt;&lt;br/&gt;有如下数据 &lt;br/&gt;国家（country）    性别（sex）    人口（population）&lt;br/&gt;中国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;340&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;中国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;260&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;美国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;45&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;美国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;55&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;加拿大    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;51&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;加拿大    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;49&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;英国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;40&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;英国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;60&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;br/&gt;按照国家和性别进行分组，得出结果如下 &lt;br/&gt;国家    男    女&lt;br/&gt;中国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;340&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;260&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;美国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;45&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;55&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;加拿大    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;51&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;49&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;英国    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;40&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;    &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;60&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;&lt;br/&gt;普通情况下，用UNION也可以实现用一条语句进行查询。但是那样增加消耗(两个Select部分)，而且SQL语句会比较长。 &lt;br/&gt;下面是一个是用Case函数来完成这个功能的例子 &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;SELECT&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; country,&lt;br/&gt;       &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;SUM&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;( &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; sex &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;br/&gt;                      population &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;0&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;),  &lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;--&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;男性人口&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;       &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;SUM&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;( &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; sex &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;br/&gt;                      population &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;0&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;)   &lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;--&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;女性人口&lt;/span&gt;&lt;span style="line-height: 18px; color: #008080; "&gt;&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;FROM&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;  Table_A&lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;GROUP&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;BY&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; country;&lt;br/&gt;&lt;br/&gt;这样我们使用Select，完成对二维表的输出形式，充分显示了Case函数的强大。 &lt;br/&gt;&lt;br/&gt;三，在Check中使用Case函数。 &lt;br/&gt;&lt;br/&gt;在Check中使用Case函数在很多情况下都是非常不错的解决方法。可能有很多人根本就不用Check，那么我建议你在看过下面的例子之后也尝试一下在SQL中使用Check。 &lt;br/&gt;下面我们来举个例子 &lt;br/&gt;公司A，这个公司有个规定，女职员的工资必须高于1000块。如果用Check和Case来表现的话，如下所示 &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;CONSTRAINT&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; check_salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;CHECK&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;           ( &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; sex &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff00ff; "&gt;CASE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;WHEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1000&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                        &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;THEN&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;0&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;                  &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;ELSE&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;END&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; )&lt;br/&gt;&lt;br/&gt;如果单纯使用Check，如下所示 &lt;br/&gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;CONSTRAINT&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; check_salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #0000ff; "&gt;CHECK&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;br/&gt;           ( sex &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;=&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;2&lt;/span&gt;&lt;span style="line-height: 18px; color: #ff0000; "&gt;'&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;AND&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; salary &lt;/span&gt;&lt;span style="line-height: 18px; color: #808080; "&gt;&amp;gt;&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; &lt;/span&gt;&lt;span style="line-height: 18px; color: #800000; font-weight: bold; "&gt;1000&lt;/span&gt;&lt;span style="line-height: 18px; color: #000000; "&gt; )&lt;br/&gt;&lt;br/&gt;女职员的条件倒是符合了，男职员就无法输入了。 &lt;br/&gt;&lt;/span&gt;&lt;/span&gt;&lt;br/&gt;&lt;br/&gt;&lt;/span&gt;&lt;/p&gt;&lt;/span&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/1797151.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2010/08/11/1797151.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2010/08/09/1795904.html</id><title type="text">使用 Using 語句釋放有限的資源</title><summary type="text">遇到一個老問題，開始很清楚解決辦法，後來變得不清楚了（因爲我解釋不了該辦法爲什麽是正確的）。相信很多人都遇到使用 SqlDataReader 的時候，出現連接池不足的問題，其根本原因就是打開數據庫連接而沒有關閉，即是 SqlConnection 對象連接的關閉。有很多人不放心使用 using 語句來關閉而選擇顯式的使用 SqlConnection.Close() 方法來關閉。很有可能是因爲對 us...</summary><published>2010-08-09T09:11:00Z</published><updated>2010-08-09T09:11:00Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/Koy/archive/2010/08/09/1795904.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/08/09/1795904.html"/><content type="html">&lt;p&gt;遇到一個老問題，開始很清楚解決辦法，後來變得不清楚了（因爲我解釋不了該辦法爲什麽是正確的）。相信很多人都遇到使用 SqlDataReader 的時候，出現連接池不足的問題，其根本原因就是打開數據庫連接而沒有關閉，即是 SqlConnection 對象連接的關閉。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;有很多人不放心使用 using 語句來關閉而選擇顯式的使用 SqlConnection.Close() 方法來關閉。很有可能是因爲對 using 的不了解，現在帖出MSDN的一些解釋，如下：&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;第一。SqlConnection 的使用：&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;原文：&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx"&gt;http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx&lt;/a&gt;&amp;nbsp;&lt;/p&gt;&lt;span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div class="LW_CollapsibleArea_TitleDiv" style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span class="LW_CollapsibleArea_Title" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;备注&lt;/span&gt;&lt;div class="LW_CollapsibleArea_HrDiv" style="padding-top: 12px; "&gt;&lt;hr class="LW_CollapsibleArea_Hr" style="border-bottom-color: #e5e5e5; border-bottom-width: 1px; border-bottom-style: solid; border-left-style: none; border-left-width: initial; border-left-color: initial; border-top-style: none; border-top-width: initial; border-top-color: initial; border-right-style: none; border-right-width: initial; border-right-color: initial; color: #e5e5e5; margin-left: 4px; " /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a name="remarksToggle" xmlns="http://www.w3.org/1999/xhtml" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;对象表示与 SQL Server 数据源的一个唯一的会话。对于客户端/服务器数据库系统，它等效于到服务器的网络连接。&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;与&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldataadapter(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;SqlDataAdapter&lt;/a&gt;和&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;SqlCommand&lt;/a&gt;&amp;nbsp;一起使用，可以在连接 Microsoft SQL Server 数据库时提高性能。对于所有第三方 SQL 服务器产品以及其他支持 OLE DB 的数据源，请使用&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.oledb.oledbconnection(v=VS.80).aspx" style="color: #960bb4; text-decoration: none; "&gt;OleDbConnection&lt;/a&gt;。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;当创建&amp;nbsp;&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;的实例时，所有属性都设置为它们的初始值。有关这些值的列表，请参见&amp;nbsp;&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;构造函数。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;如果&amp;nbsp;&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;超出范围，则该连接将保持打开状态。因此，必须通过调用&amp;nbsp;&lt;strong&gt;Close&lt;/strong&gt;&amp;nbsp;或&amp;nbsp;&lt;strong&gt;Dispose&lt;/strong&gt;&amp;nbsp;显式关闭该连接。&lt;strong&gt;Close&lt;/strong&gt;&amp;nbsp;和&amp;nbsp;&lt;strong&gt;Dispose&lt;/strong&gt;&amp;nbsp;在功能上等效。如果连接池值&lt;strong&gt;Pooling&lt;/strong&gt;&amp;nbsp;设置为&amp;nbsp;&lt;strong&gt;true&lt;/strong&gt;&amp;nbsp;或&amp;nbsp;&lt;strong&gt;yes&lt;/strong&gt;，则基础连接将返回到连接池。另一方面，如果&amp;nbsp;&lt;strong&gt;Pooling&lt;/strong&gt;&amp;nbsp;设置为&amp;nbsp;&lt;strong&gt;false&lt;/strong&gt;&amp;nbsp;或&amp;nbsp;&lt;strong&gt;no&lt;/strong&gt;，则实际上会关闭到服务器的基础连接。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;若要确保连接始终关闭，请在&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;块内部打开连接，如下面的代码段所示。这样可确保在代码退出代码块时自动关闭连接。&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;/div&gt;&lt;div class="alert" xmlns="http://www.w3.org/1999/xhtml" style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; background-color: #fcfec5; margin-bottom: 10px; "&gt;&amp;nbsp;&lt;/div&gt;&lt;p&gt;&lt;table width="100%" style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; border-collapse: collapse; width: 955px; "&gt;&lt;tbody&gt;&lt;tr style="vertical-align: top; "&gt;&lt;th align="left" style="border-top-style: hidden; border-right-style: hidden; border-bottom-style: hidden; border-left-style: hidden; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; background-color: #fcfec5; padding-right: 11px; padding-left: 11px; padding-bottom: 1px; padding-top: 10px; text-align: left; height: 21px; font-family: 'Segoe UI', Verdana, Arial; font-size: 1.077em; color: #000000; font-style: normal; font-weight: bold; border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; "&gt;&lt;img class="note cl_IC101471" alt="Note" src="http://i.msdn.microsoft.com/Hash/030c41d9079671d09a62d8e2c1db6973.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; background-image: url(http://i3.msdn.microsoft.com/Hash/bda288328f5eabd19cfdf9486efda99d.png); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: initial; width: 10px; height: 10px; overflow-x: hidden; overflow-y: hidden; padding-right: 5px; background-position: -1825px -3px; background-repeat: no-repeat no-repeat; " /&gt;注意&lt;/th&gt;&lt;/tr&gt;&lt;tr style="vertical-align: top; "&gt;&lt;td style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; background-color: #fcfec5; line-height: 18px; padding-right: 11px; padding-left: 11px; padding-bottom: 10px; margin-top: 1px; margin-right: 1px; margin-bottom: 1px; margin-left: 1px; padding-top: 1px; "&gt;&lt;p&gt;若要部署高性能应用程序，则必须使用连接池。在使用用于 SQL Server 的 .NET Framework 数据提供程序时，不必启用连接池，因为提供程序会自动对此进行管理，不过您可以修改某些设置。有关更多信息，请参见&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/8xx3tyca(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;使用连接池&lt;/a&gt;。&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;如果执行&amp;nbsp;&lt;strong&gt;SqlCommand&lt;/strong&gt;&amp;nbsp;的方法生成&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlexception(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;SqlException&lt;/a&gt;，那么当严重级别小于等于 19 时，&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;将仍保持打开状态。当严重级别大于等于 20 时，服务器通常会关闭&amp;nbsp;&lt;strong&gt;SqlConnection&lt;/strong&gt;。但是，用户可以重新打开连接并继续操作。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;用于创建&amp;nbsp;&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;对象实例的应用程序可通过设置声明性或强制性安全要求，要求所有直接和间接的调用方对��码都具有足够的权限。&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;使用&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlclientpermission(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;SqlClientPermission&lt;/a&gt;&amp;nbsp;对象设置安全要求。用户可以通过使用&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlclientpermissionattribute(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;SqlClientPermissionAttribute&lt;/a&gt;&amp;nbsp;对象来验证他们的代码是否具有足够的权限。用户和管理员还可以使用&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/cb6t8dtz(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;代码访问安全策略工具 (Caspol.exe)&lt;/a&gt;&amp;nbsp;来修改计算机、用户和企业级别的安全策略。有关更多信息，请参见&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/fkytk30f(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;.NET Framework 中的安全性&lt;/a&gt;。有关演示如何使用安全请求的示例，请参见&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/0x4t63kb(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;代码访问安全性和 ADO.NET&lt;/a&gt;。&lt;/p&gt;&lt;div class="alert" xmlns="http://www.w3.org/1999/xhtml" style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; background-color: #fcfec5; margin-bottom: 10px; "&gt;&lt;table width="100%" style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; border-collapse: collapse; width: 955px; "&gt;&lt;tbody&gt;&lt;tr style="vertical-align: top; "&gt;&lt;th align="left" style="border-top-style: hidden; border-right-style: hidden; border-bottom-style: hidden; border-left-style: hidden; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; background-color: #fcfec5; padding-right: 11px; padding-left: 11px; padding-bottom: 1px; padding-top: 10px; text-align: left; height: 21px; font-family: 'Segoe UI', Verdana, Arial; font-size: 1.077em; color: #000000; font-style: normal; font-weight: bold; border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; "&gt;&lt;img class="note cl_IC101471" alt="Note" src="http://i.msdn.microsoft.com/Hash/030c41d9079671d09a62d8e2c1db6973.gif" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; background-image: url(http://i3.msdn.microsoft.com/Hash/bda288328f5eabd19cfdf9486efda99d.png); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: initial; width: 10px; height: 10px; overflow-x: hidden; overflow-y: hidden; padding-right: 5px; background-position: -1825px -3px; background-repeat: no-repeat no-repeat; " /&gt;注意&lt;/th&gt;&lt;/tr&gt;&lt;tr style="vertical-align: top; "&gt;&lt;td style="border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-top-color: #bbbbbb; border-right-color: #bbbbbb; border-bottom-color: #bbbbbb; border-left-color: #bbbbbb; background-color: #fcfec5; line-height: 18px; padding-right: 11px; padding-left: 11px; padding-bottom: 10px; margin-top: 1px; margin-right: 1px; margin-bottom: 1px; margin-left: 1px; padding-top: 1px; "&gt;&lt;p&gt;如果您正在使用 Microsoft .NET Framework 1.0 版，则在使用&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection.open(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;Open&lt;/a&gt;&amp;nbsp;连接到 SQL Server 时必须使用&amp;nbsp;&lt;strong&gt;FullTrust&lt;/strong&gt;&amp;nbsp;命名权限集。如果使用的是 .NET Framework 1.1 版或更高版本，则此要求不适用。有关更多信息，请参见&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/yd267cce(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;请求权限&lt;/a&gt;&amp;nbsp;和&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/4652tyx7(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;命名的权限集&lt;/a&gt;。&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;有关处理来自服务器的警告和信息性消息的更多信息，请参见&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/a0hee08w(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;使用连接事件&lt;/a&gt;。&lt;/p&gt;&lt;div class="HxLinkTable" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;span class="Apple-style-span" style="font-size: 20px; color: #3f529c; font-weight: bold; "&gt;示例&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div class="LW_CollapsibleArea_TitleDiv" style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;div class="LW_CollapsibleArea_HrDiv" style="padding-top: 12px; "&gt;&lt;hr class="LW_CollapsibleArea_Hr" style="border-bottom-color: #e5e5e5; border-bottom-width: 1px; border-bottom-style: solid; border-left-style: none; border-left-width: initial; border-left-color: initial; border-top-style: none; border-top-width: initial; border-top-color: initial; border-right-style: none; border-right-width: initial; border-right-color: initial; color: #e5e5e5; margin-left: 4px; " /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a name="codeExampleToggle" xmlns="http://www.w3.org/1999/xhtml" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;下面的示例创建一个&amp;nbsp;&lt;strong&gt;SqlCommand&lt;/strong&gt;&amp;nbsp;和一个&amp;nbsp;&lt;strong&gt;SqlConnection&lt;/strong&gt;。&lt;strong&gt;SqlConnection&lt;/strong&gt;&amp;nbsp;打开，并设置为&amp;nbsp;&lt;strong&gt;SqlCommand&lt;/strong&gt;&amp;nbsp;的&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.connection(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;Connection&lt;/a&gt;。然后，该示例调用&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlcommand.executenonquery(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;ExecuteNonQuery&lt;/a&gt;&amp;nbsp;并关闭该连接。为了完成此任务，将为&amp;nbsp;&lt;strong&gt;ExecuteNonQuery&lt;/strong&gt;&amp;nbsp;传递一个连接字符串和一个查询字符串，后者是一个 Transact-SQL INSERT 语句。&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a name="CodeSpippet3" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerTabs" style="vertical-align: middle; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt !important; height: 22px; position: relative; z-index: 1; "&gt;&lt;div class="LW_CodeSnippetContainerTabLeft cl_lw_codesnippet_lt_tab" style="background-image: url(http://i3.msdn.microsoft.com/Hash/bda288328f5eabd19cfdf9486efda99d.png); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: initial; width: 6px; height: 20px; overflow-x: hidden; overflow-y: hidden; float: left; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #d0d2d2; background-position: -375px -3px; background-repeat: no-repeat no-repeat; "&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerTabFirst" style="height: 19px; float: left; width: auto; border-top-style: solid; border-top-width: 1px; border-top-color: #bbbbbb; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #d0d2d2; padding-top: 0px; padding-right: 8px; padding-bottom: 0px; padding-left: 6px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eff5ff; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx" class="LW_CodeSnippetContainerTabLinkBold" style="font-weight: bold !important; color: #1364c4; text-decoration: none; "&gt;VB&lt;/a&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerTabActive" style="height: 19px; float: left; width: auto; border-top-style: solid; border-top-width: 1px; border-top-color: #bbbbbb; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #ffffff; padding-top: 0px; padding-right: 8px; padding-bottom: 0px; padding-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; border-left-style: solid; border-left-width: 1px; border-left-color: #929292; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;a class="LW_CodeSnippetContainerTabLinkBold" style="font-weight: bold !important; color: #e66a38; text-decoration: none; "&gt;C#&lt;/a&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerTab" style="height: 19px; float: left; width: auto; border-top-style: solid; border-top-width: 1px; border-top-color: #bbbbbb; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #d0d2d2; padding-top: 0px; padding-right: 8px; padding-bottom: 0px; padding-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eff5ff; border-left-style: solid; border-left-width: 1px; border-left-color: #929292; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx" class="LW_CodeSnippetContainerTabLinkNormal" style="font-weight: normal !important; color: #1364c4; text-decoration: none; "&gt;C++&lt;/a&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerTab" style="height: 19px; float: left; width: auto; border-top-style: solid; border-top-width: 1px; border-top-color: #bbbbbb; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #d0d2d2; padding-top: 0px; padding-right: 8px; padding-bottom: 0px; padding-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eff5ff; border-left-style: solid; border-left-width: 1px; border-left-color: #929292; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx" class="LW_CodeSnippetContainerTabLinkNormal" style="font-weight: normal !important; color: #1364c4; text-decoration: none; "&gt;F#&lt;/a&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerTabLast" style="height: 19px; float: left; width: auto; border-top-style: solid; border-top-width: 1px; border-top-color: #bbbbbb; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #d0d2d2; padding-top: 0px; padding-right: 6px; padding-bottom: 0px; padding-left: 8px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eff5ff; border-left-style: solid; border-left-width: 1px; border-left-color: #929292; background-position: initial initial; background-repeat: initial initial; "&gt;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx" class="LW_CodeSnippetContainerTabLinkNormal" style="font-weight: normal !important; color: #1364c4; text-decoration: none; "&gt;JScript&lt;/a&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerTabRight cl_lw_codesnippet_rt_tab" style="background-image: url(http://i3.msdn.microsoft.com/Hash/bda288328f5eabd19cfdf9486efda99d.png); background-attachment: scroll; background-origin: initial; background-clip: initial; background-color: initial; width: 6px; height: 20px; overflow-x: hidden; overflow-y: hidden; float: left; border-bottom-style: solid; border-bottom-width: 2px; border-bottom-color: #d0d2d2; background-position: -387px -3px; background-repeat: no-repeat no-repeat; "&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #e5e5e5; border-left-style: solid; border-left-width: 1px; border-left-color: #bbbbbb; border-right-style: solid; border-right-width: 3px; border-right-color: #e5e5e5; "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: #ffffff; width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqlconnection(VS.80).aspx" style="color: #1364c4; text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode3" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;&lt;span style="color: blue; "&gt;private&lt;/span&gt; &lt;span style="color: blue; "&gt;static&lt;/span&gt; &lt;span style="color: blue; "&gt;void&lt;/span&gt; OpenSqlConnection()&lt;br/&gt;{&lt;br/&gt;    &lt;span style="color: blue; "&gt;string&lt;/span&gt; connectionString = GetConnectionString();&lt;br/&gt;    &lt;span style="color: blue; "&gt;using&lt;/span&gt; (SqlConnection connection = &lt;span style="color: blue; "&gt;new&lt;/span&gt; SqlConnection(connectionString))&lt;br/&gt;    {&lt;br/&gt;        connection.Open();&lt;br/&gt;        Console.WriteLine(&lt;span style="color: #a31515; "&gt;"ServerVersion: {0}"&lt;/span&gt;, connection.ServerVersion);&lt;br/&gt;        Console.WriteLine(&lt;span style="color: #a31515; "&gt;"State: {0}"&lt;/span&gt;, connection.State);&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;&lt;span style="color: blue; "&gt;static&lt;/span&gt; &lt;span style="color: blue; "&gt;private&lt;/span&gt; &lt;span style="color: blue; "&gt;string&lt;/span&gt; GetConnectionString()&lt;br/&gt;{&lt;br/&gt;    &lt;span style="color: green; "&gt;// To avoid storing the connection string in your code, &lt;/span&gt;&lt;br/&gt;    &lt;span style="color: green; "&gt;// you can retrieve it from a configuration file, using the &lt;/span&gt;&lt;br/&gt;    &lt;span style="color: green; "&gt;// System.Configuration.ConfigurationSettings.AppSettings property &lt;/span&gt;&lt;br/&gt;    &lt;span style="color: blue; "&gt;return&lt;/span&gt; &lt;span style="color: #a31515; "&gt;"Data Source=(local);Initial Catalog=AdventureWorks;"&lt;/span&gt;&lt;br/&gt;        + &lt;span style="color: #a31515; "&gt;"Integrated Security=SSPI;"&lt;/span&gt;;&lt;br/&gt;}&lt;br/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;第二。using 語句的使用&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;原文：&lt;a href="http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx#1"&gt;http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx#1&lt;/a&gt;&lt;/p&gt;&lt;span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;div class="title" xmlns:asp="http://msdn2.microsoft.com/asp" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.769em; color: #3f529c; font-weight: bold; "&gt;using 语句（C# 参考）&lt;/div&gt;&lt;div id="mainSection" style="padding-top: 10px; "&gt;&lt;div id="mainBody"&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;定义一个范围，将在此范围之外释放一个或多个对象。&lt;/p&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div class="LW_CollapsibleArea_TitleDiv" style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span class="LW_CollapsibleArea_Title" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;语法&lt;/span&gt;&lt;div class="LW_CollapsibleArea_HrDiv" style="padding-top: 12px; "&gt;&lt;hr class="LW_CollapsibleArea_Hr" style="border-bottom-color: #e5e5e5; border-bottom-width: 1px; border-bottom-style: solid; border-left-style: none; border-left-width: initial; border-left-color: initial; border-top-style: none; border-top-width: initial; border-top-color: initial; border-right-style: none; border-right-width: initial; border-right-color: initial; color: #e5e5e5; margin-left: 4px; " /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a name="sectionToggle0" xmlns="http://www.w3.org/1999/xhtml" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a name="CodeSpippet0" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #e5e5e5; border-left-style: solid; border-left-width: 1px; border-left-color: #bbbbbb; border-right-style: solid; border-right-width: 3px; border-right-color: #e5e5e5; "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: #ffffff; width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode0" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;        using (Font font1 = new Font("Arial", 10.0f))&lt;br/&gt;{&lt;br/&gt;}&lt;br/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div class="LW_CollapsibleArea_TitleDiv" style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span class="LW_CollapsibleArea_Title" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;备注&lt;/span&gt;&lt;div class="LW_CollapsibleArea_HrDiv" style="padding-top: 12px; "&gt;&lt;hr class="LW_CollapsibleArea_Hr" style="border-bottom-color: #e5e5e5; border-bottom-width: 1px; border-bottom-style: solid; border-left-style: none; border-left-width: initial; border-left-color: initial; border-top-style: none; border-top-width: initial; border-top-color: initial; border-right-style: none; border-right-width: initial; border-right-color: initial; color: #e5e5e5; margin-left: 4px; " /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a name="remarksToggle" xmlns="http://www.w3.org/1999/xhtml" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;C# 通过 .NET Framework 公共语言运行库 (CLR) 自动释放用于存储不再需要的对象的内存。内存的释放具有不确定性；一旦 CLR 决定执行垃圾回收，就会释放内存。但是，通常最好尽快释放诸如文件句柄和网络连接这样的有限资源。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句允许程序员指定使用资源的对象应当何时释放资源。为&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句提供的对象必须实现&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.idisposable(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;IDisposable&lt;/a&gt;&amp;nbsp;接口。此接口提供了&amp;nbsp;&lt;a href="http://msdn.microsoft.com/zh-cn/library/system.idisposable.dispose(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; "&gt;Dispose&lt;/a&gt;&amp;nbsp;方法，该方法将释放此对象的资源。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;可以在到达&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句的末尾时，或者在该语句结束之前引发了异常并且控制权离开语句块时，退出&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句。&lt;/p&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;可以在&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句中声明对象（如上所示），或者在 using 语句之前声明对象，如下所示：&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a name="CodeSpippet1" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #e5e5e5; border-left-style: solid; border-left-width: 1px; border-left-color: #bbbbbb; border-right-style: solid; border-right-width: 3px; border-right-color: #e5e5e5; "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: #ffffff; width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode1" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;Font font2 = new Font("Arial", 10.0f);&lt;br/&gt;using (font2)&lt;br/&gt;{&lt;br/&gt;    // use font2&lt;br/&gt;}&lt;br/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;可以有多个对象与&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句一起使用，但是必须在&amp;nbsp;&lt;strong&gt;using&lt;/strong&gt;&amp;nbsp;语句内部声明这些对象，如下所示：&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a name="CodeSpippet2" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #e5e5e5; border-left-style: solid; border-left-width: 1px; border-left-color: #bbbbbb; border-right-style: solid; border-right-width: 3px; border-right-color: #e5e5e5; "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: #ffffff; width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode2" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;        using (Font font3 = new Font("Arial", 10.0f), &lt;br/&gt;           font4 = new Font("Arial", 10.0f))&lt;br/&gt;{&lt;br/&gt;    // Use font3 and font4.&lt;br/&gt;}&lt;br/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div class="LW_CollapsibleArea_TitleDiv" style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span class="LW_CollapsibleArea_Title" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;示例&lt;/span&gt;&lt;div class="LW_CollapsibleArea_HrDiv" style="padding-top: 12px; "&gt;&lt;hr class="LW_CollapsibleArea_Hr" style="border-bottom-color: #e5e5e5; border-bottom-width: 1px; border-bottom-style: solid; border-left-style: none; border-left-width: initial; border-left-color: initial; border-top-style: none; border-top-width: initial; border-top-color: initial; border-right-style: none; border-right-width: initial; border-right-color: initial; color: #e5e5e5; margin-left: 4px; " /&gt;&lt;/div&gt;&lt;/div&gt;&lt;a name="codeExampleToggle" xmlns="http://www.w3.org/1999/xhtml" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;下面的示例显示用户定义类可以如何实现它自己的 Dispose 行为。注意类型必须从&amp;nbsp;&lt;strong&gt;IDisposable&lt;/strong&gt;&amp;nbsp;继承。&lt;/p&gt;&lt;div class="LW_CodeSnippetContainer" xmlns=""&gt;&lt;a name="CodeSpippet3" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cnblogs.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "&gt;&lt;/a&gt;&lt;div class="LW_CodeSnippetContainerCodeCollection" style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div class="LW_CodeSnippetToolBar" style="width: auto; height: auto; border-top-style: solid; border-top-width: 3px; border-top-color: #e5e5e5; border-left-style: solid; border-left-width: 1px; border-left-color: #bbbbbb; border-right-style: solid; border-right-width: 3px; border-right-color: #e5e5e5; "&gt;&lt;div class="LW_CodeSnippetToolBarText" style="float: right; top: -10px; position: relative; background-color: #ffffff; width: auto; padding-left: 4px; padding-right: 4px; height: 0px; "&gt;&lt;a title="复制到剪贴板。" href="http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx" style="color: #1364c4; text-decoration: none; margin-left: 5px; margin-right: 5px; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt; background-color: white; padding-left: 4px; padding-right: 4px; "&gt;复制&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="CodeSnippetContainerCode3" class="LW_CodeSnippetContainerCode" style="width: auto; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-right: 21px; padding-left: 21px; "&gt;&lt;div style="padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; "&gt;using System;&lt;br/&gt;&lt;br/&gt;class C : IDisposable&lt;br/&gt;{&lt;br/&gt;    public void UseLimitedResource()&lt;br/&gt;    {&lt;br/&gt;        Console.WriteLine("Using limited resource...");&lt;br/&gt;    }&lt;br/&gt;&lt;br/&gt;    void IDisposable.Dispose()&lt;br/&gt;    {&lt;br/&gt;        Console.WriteLine("Disposing limited resource.");&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;br/&gt;class Program&lt;br/&gt;{&lt;br/&gt;    static void Main()&lt;br/&gt;    {&lt;br/&gt;        using (C c = new C())&lt;br/&gt;        {&lt;br/&gt;            c.UseLimitedResource();&lt;br/&gt;        }&lt;br/&gt;        Console.WriteLine("Now outside using statement.");&lt;br/&gt;        Console.ReadLine();&lt;br/&gt;    }&lt;br/&gt;}&lt;br/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="LW_CollapsibleArea_Container" xmlns=""&gt;&lt;div class="LW_CollapsibleArea_TitleDiv" style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;第三。總結：&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span class="Apple-tab-span" style="white-space:pre"&gt;&lt;/span&gt;由於using 語句是：&lt;span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;Defines a scope, outside of which an object or objects will be disposed. &amp;nbsp;因此SqlConnection資源被使用後是會調用 disposed 來釋放資源的，當然也會關閉連接，所以以後&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "&gt;可以放心使用 using 語句了，呵呵！&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;另外，在MSDN有人（&lt;span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;a href="http://msdn.microsoft.com/zh-cn/library/community/user/93808.aspx" style="color: #1364c4; text-decoration: none; "&gt;shiny zhu&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: verdana, 'courier new'; font-size: 14px; line-height: 21px; "&gt;）做過using語句生成IL代碼的研究 ，如下：&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;div class="AnnotationTitle h3" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.077em; color: #3f529c; font-weight: bold; padding-left: 13px; padding-top: 13px; padding-bottom: 13px; padding-right: 13px; "&gt;&lt;span&gt;using语句的本质&lt;/span&gt;&lt;/div&gt;&lt;div class="AnnotationBody" style="word-wrap: break-word; padding-left: 13px; "&gt;&lt;p&gt;使用using语句实际上生成的IL代码中是一个try, finally代码块，在finally代码块里释放资源。&lt;/p&gt;&lt;p&gt;比如这样一段代码：&lt;/p&gt;            using (SqlConnection conn = new SqlConnection())&lt;br /&gt;            {&lt;br /&gt;                conn.Open();&lt;br /&gt;                throw new Exception("Exception!!");//抛出异常之后能回收SqlConnection对象的资源吗？&lt;br /&gt;            }&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;IL代码可为：&lt;/p&gt;  // Code size       42 (0x2a)&lt;br /&gt;  .maxstack  2&lt;br /&gt;  .locals init ([0] class [System.Data]System.Data.SqlClient.SqlConnection conn,&lt;br /&gt;           [1] bool CS$4$0000)&lt;br /&gt;  IL_0000:  nop&lt;br /&gt;  IL_0001:  newobj     instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor()&lt;br /&gt;  IL_0006:  stloc.0&lt;br /&gt;  .try&lt;br /&gt;  {&lt;br /&gt;    IL_0007:  nop&lt;br /&gt;    IL_0008:  ldloc.0&lt;br /&gt;    IL_0009:  callvirt   instance void [System.Data]System.Data.Common.DbConnection::Open()&lt;br /&gt;    IL_000e:  nop&lt;br /&gt;    IL_000f:  ldstr      "Exception!!"&lt;br /&gt;    IL_0014:  newobj     instance void [mscorlib]System.Exception::.ctor(string)&lt;br /&gt;    IL_0019:  throw&lt;br /&gt;  }  // end .try&lt;br /&gt;  finally&lt;br /&gt;  {&lt;br /&gt;    IL_001a:  ldloc.0&lt;br /&gt;    IL_001b:  ldnull&lt;br /&gt;    IL_001c:  ceq&lt;br /&gt;    IL_001e:  stloc.1&lt;br /&gt;    IL_001f:  ldloc.1&lt;br /&gt;    IL_0020:  brtrue.s   IL_0029&lt;br /&gt;    IL_0022:  ldloc.0&lt;br /&gt;    IL_0023:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()&lt;br /&gt;    IL_0028:  nop&lt;br /&gt;    IL_0029:  endfinally&lt;br /&gt;  }  // end handler &lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;可以看到调用了SqlConnection的Dispose方法释放了资源。&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/1795904.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2010/08/09/1795904.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
