<?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-02-02T09:00:17Z</updated><author><name>Koy</name><uri>http://www.cnblogs.com/Koy/</uri></author><generator>CNBlogs BlogServer</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/02/02/2336002.html</id><title type="text">ajax 維護 div 的 scrollbar</title><summary type="text">&amp;lt;!-- Maintain Scroll Position after Asynchronous Postback --&amp;gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;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;&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;&#xD;
&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 &amp;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" &gt;&#xD;
&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" &gt;&#xD;
&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" &gt;&#xD;
&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"  href="http://www.cnblogs.com/kingimg/archive/2009/02/05/1384410.html"&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&#xD;
&lt;p&gt;使用SQL2005 递归查询结合Row_Number()实现完全SQL端树排序&lt;/p&gt;&lt;/div&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;在实际应用中，我们经常需要用到树型结构功能，数据库结构一般如下&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&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;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;即用一个ParentID来标识该节点从属关系。为了最终生成一棵树，一般做法是把记录选出来，然后在程序里递归重新排好序后再呈现出来，但是如果有大量数据，就带来了性能开销问题。那么能不能直接在数据库利用SQL语句排好树后再输出呢？&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;SQL2005 有个递归查询功能也就是WITH .. AS 语句。对上面这样的表格使用递归查询，可以查询得到某树支下（包括根）的所有节点记录。类似语句如下：&lt;/p&gt;&#xD;
&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-left:5px;padding-right:5px;"&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;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;将得到ClassID为1的根节点下的所有记录：&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&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;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;但是这个记录集显然没有经过树排序，这时还需要程序里进一步处理才能输出到客户端。在这里我介绍一种WITH 结合 Row_Number() 实现SQL端排序的方法。&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;先来看看最终的代码：&lt;/p&gt;&#xD;
&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-left:5px;padding-right:5px;"&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" &gt;Code&lt;/span&gt;&lt;/div&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;&lt;/p&gt;&#xD;
&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-left:5px;padding-right:5px;"&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;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&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;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;这棵树已经从上到下按树结构排好序了！程序里只要原样输出即可！&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&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;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&gt;&lt;span style="color: #000000"&gt;好了，这样子我们就实现了完全SQL端生成已排序的树的目的了，完全脱离了程序处理，这个方法看起来效率还不错呢~&lt;/span&gt;&lt;/p&gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&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;&#xD;
&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" &gt;发表评论&lt;/div&gt;&#xD;
&lt;div style="font-family: verdana; word-wrap: break-word" &gt;&lt;/div&gt;&#xD;
&lt;div style="margin: 0px 0px 10px; font-family: verdana; word-wrap: break-word" &gt;&#xD;
&lt;div style="border-bottom: #cccccc 1px dotted; font-family: verdana; word-wrap: break-word; margin-bottom: 10px; color: #4f4f4f; font-weight: bold" &gt;&#xD;
&lt;div style="font-family: verdana; word-wrap: break-word; float: right; font-weight: normal" &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"  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"  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;&#xD;
&lt;div style="font-family: verdana; word-wrap: break-word; margin-bottom: 20px" &gt;&lt;span id="comment_body_1443384" &gt;不错不错。以前在程序里递归，总觉得麻烦。&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&#xD;
&lt;div style="margin: 0px 0px 10px; font-family: verdana; word-wrap: break-word" &gt;&#xD;
&lt;div style="border-bottom: #cccccc 1px dotted; font-family: verdana; word-wrap: break-word; margin-bottom: 10px; color: #4f4f4f; font-weight: bold" &gt;&#xD;
&lt;div style="font-family: verdana; word-wrap: break-word; float: right; font-weight: normal" &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"  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"  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;&#xD;
&lt;div style="font-family: verdana; word-wrap: break-word; margin-bottom: 20px" &gt;&lt;span id="comment_body_1443389" &gt;&#xD;
&lt;p style="text-indent: 0px; margin: 5px auto"&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;&#xD;
&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&#xD;
&lt;p&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;strong style="color: #ff0000"&gt;再附上MSDN的解說：&lt;/strong&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;span style="line-height: normal; font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 23px; font-weight: bold" &gt;使用公用表表达式的递归查询&lt;/span&gt;&lt;/p&gt;&lt;span style="line-height: normal; font-family: 'Segoe UI', Verdana, Arial; font-size: 13px" &gt;&#xD;
&lt;div style="padding-top: 10px" id="mainSection"&gt;&#xD;
&lt;div id="mainBody"&gt;&#xD;
&lt;p&gt;&lt;/p&gt;&lt;span id="changeHistory"&gt;&lt;/span&gt;&#xD;
&lt;p&gt;公用表表达式 (CTE) 具有一个重要的优点，那就是能够引用其自身，从而创建递归 CTE。递归 CTE 是一个重复执行初始 CTE 以返回数据子集直到获取完整结果集的公用表表达式。&lt;/p&gt;&#xD;
&lt;p&gt;在 SQL Server 2005 中，当某个查询引用递归 CTE 时，它即被称为&amp;#8220;递归查询&amp;#8221;。递归查询通常用于返回分层数据，例如：显示某个组织图中的雇员或物料清单方案（其中父级产品有一个或多个组件，而那些组件可能还有子组件，或者是其他父级产品的组件）中的数据。&lt;/p&gt;&#xD;
&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;&#xD;
&lt;div  xmlns=""&gt;&#xD;
&lt;div style="margin-top: 19px; margin-bottom: 19px" &gt;&lt;span style="font-family: 'Segoe UI', Verdana, Arial; float: left; color: #3f529c; font-size: 1.53em; font-weight: bold" &gt;递归 CTE 的结构&lt;/span&gt; &#xD;
&lt;div style="padding-top: 12px" &gt;&#xD;
&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"  /&gt;&#xD;
&lt;/div&gt;&lt;/div&gt;&lt;a id="sectionToggle0" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/a&gt;&#xD;
&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Transact-SQL 中的递归 CTE 的结构与其他编程语言中的递归例程相似。尽管其他语言中的递归例程返回标量值，但递归 CTE 可以返回多行。&lt;/p&gt;&#xD;
&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;递归 CTE 由下列三个元素组成：&lt;/p&gt;&#xD;
&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;&#xD;
&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"  xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
&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;&#xD;
&lt;tbody&gt;&#xD;
&lt;tr style="vertical-align: top"&gt;&#xD;
&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"  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;&#xD;
&lt;tr style="vertical-align: top"&gt;&#xD;
&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;。 &#xD;
&lt;p style="margin: 0px"&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&#xD;
&lt;p style="margin: 0px"&gt;&lt;/p&gt;&lt;/div&gt;&#xD;
&lt;h3 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1.07em; font-weight: bold"  xmlns="http://www.w3.org/1999/xhtml"&gt;伪代码和语义&lt;/strong&gt;&lt;/p&gt;&#xD;
&lt;div  xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
&lt;p&gt;递归 CTE 结构必须至少包含一个定位点成员和一个递归成员。以下伪代码显示了包含一个定位点成员和一个递归成员的简单递归 CTE 的组件。&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;WITH cte_name ( column_name [,...n] )&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;AS&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;(&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;CTE_query_definition &amp;#8211;- Anchor member is defined.&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;UNION ALL&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;CTE_query_definition &amp;#8211;- Recursive member is defined referencing cte_name.&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;)&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;-- Statement using the CTE&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;SELECT *&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;code&gt;FROM cte_name&lt;/code&gt;&lt;/p&gt;&#xD;
&lt;p&gt;递归执行的语义如下：&lt;/p&gt;&#xD;
&lt;ol&gt;&lt;li&gt;将 CTE 表达式拆分为定位点成员和递归成员。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;运行定位点成员，创建第一个调用或基准结果集 (T&lt;span &gt;0&lt;/span&gt;)。&lt;br /&gt;&lt;/li&gt;&lt;li&gt;运行递归成员，将 T&lt;span &gt;i&lt;/span&gt;&amp;nbsp;作为输入，将 T&lt;span &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 &gt;0&lt;/span&gt;&amp;nbsp;到 T&lt;span &gt;n&lt;/span&gt;&amp;nbsp;执行 UNION ALL 的结果。&lt;br /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;/div&gt;&#xD;
&lt;div  xmlns=""&gt;&#xD;
&lt;div style="margin-top: 19px; margin-bottom: 19px" &gt;&lt;span style="font-family: 'Segoe UI', Verdana, Arial; float: left; color: #3f529c; font-size: 1.53em; font-weight: bold" &gt;示例&lt;/span&gt; &#xD;
&lt;div style="padding-top: 12px" &gt;&#xD;
&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"  /&gt;&#xD;
&lt;/div&gt;&lt;/div&gt;&lt;a id="sectionToggle1" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/a&gt;&#xD;
&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;以下示例通过返回 Adventure Works Cycles 公司的雇员的分层列表（从最高级雇员开始）显示递归 CTE 结构的语义。执行 CTE 的语句将结果集限制到研发组中的雇员。示例后面是代码执行的演练。&lt;/p&gt;&#xD;
&lt;div  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;&#xD;
&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" &gt;&#xD;
&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" &gt;&#xD;
&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" &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;&#xD;
&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode0" &gt;&#xD;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;&lt;pre style="padding-bottom: 5px; font-style: normal; margin: 0px; padding-left: 5px; padding-right: 5px; font-family: Consolas, Courier, monospace; word-wrap: break-word; word-break: break-all; font-weight: normal; padding-top: 5px"&gt;USE AdventureWorks;&#xD;
GO&#xD;
WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level)&#xD;
AS&#xD;
(&#xD;
-- Anchor member definition&#xD;
SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID,&#xD;
0 AS Level&#xD;
FROM HumanResources.Employee AS e&#xD;
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh&#xD;
ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL&#xD;
WHERE ManagerID IS NULL&#xD;
UNION ALL&#xD;
-- Recursive member definition&#xD;
SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID,&#xD;
Level + 1&#xD;
FROM HumanResources.Employee AS e&#xD;
INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh&#xD;
ON e.EmployeeID = edh.EmployeeID AND edh.EndDate IS NULL&#xD;
INNER JOIN DirectReports AS d&#xD;
ON e.ManagerID = d.EmployeeID&#xD;
)&#xD;
-- Statement that executes the CTE&#xD;
SELECT ManagerID, EmployeeID, Title, Level&#xD;
FROM DirectReports&#xD;
INNER JOIN HumanResources.Department AS dp&#xD;
ON DirectReports.DeptID = dp.DepartmentID&#xD;
WHERE dp.GroupName = N'Research and Development' OR Level = 0;&#xD;
GO&#xD;
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&#xD;
&lt;h3 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1.07em; font-weight: bold"  xmlns="http://www.w3.org/1999/xhtml"&gt;示例代码演练&lt;/strong&gt;&lt;/p&gt;&#xD;
&lt;div  xmlns="http://www.w3.org/1999/xhtml"&gt;&#xD;
&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 &gt;0&lt;/span&gt;。这就是公司中的最高级雇员，即不向经理报告的雇员。&amp;nbsp;&lt;br /&gt;以下是定位点成员返回的结果集：&amp;nbsp;&lt;br /&gt;&#xD;
&lt;div  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;&#xD;
&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" &gt;&#xD;
&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" &gt;&#xD;
&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" &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;&#xD;
&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode1" &gt;&#xD;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;&lt;pre style="padding-bottom: 5px; font-style: normal; margin: 0px; padding-left: 5px; padding-right: 5px; font-family: Consolas, Courier, monospace; word-wrap: break-word; word-break: break-all; font-weight: normal; padding-top: 5px"&gt;ManagerID EmployeeID Title                                   Level&#xD;
    --------- ---------- --------------------------------------- ------&#xD;
    NULL      109        Chief Executive Officer                 0&#xD;
    &lt;/pre&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 &gt;i&lt;/span&gt;)，联接&amp;nbsp;&lt;code&gt;(Employee.ManagerID = DirectReports.EmployeeID&lt;/code&gt;) 返回经理为 (T&lt;span &gt;i&lt;/span&gt;) 的雇员作为输出 (T&lt;span &gt;i+1&lt;/span&gt;)。这样，递归成员的第一次迭代返回了以下结果集：&amp;nbsp;&lt;br /&gt;&#xD;
&lt;div  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;&#xD;
&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" &gt;&#xD;
&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" &gt;&#xD;
&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" &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;&#xD;
&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode2" &gt;&#xD;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;&lt;pre style="padding-bottom: 5px; font-style: normal; margin: 0px; padding-left: 5px; padding-right: 5px; font-family: Consolas, Courier, monospace; word-wrap: break-word; word-break: break-all; font-weight: normal; padding-top: 5px"&gt;ManagerID EmployeeID Title                                   Level&#xD;
    --------- ---------- --------------------------------------- ------&#xD;
    109       12         Vice President of Engineering           1&#xD;
    &lt;/pre&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;&#xD;
&lt;div  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;&#xD;
&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" &gt;&#xD;
&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" &gt;&#xD;
&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" &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;&#xD;
&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode3" &gt;&#xD;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;&lt;pre style="padding-bottom: 5px; font-style: normal; margin: 0px; padding-left: 5px; padding-right: 5px; font-family: Consolas, Courier, monospace; word-wrap: break-word; word-break: break-all; font-weight: normal; padding-top: 5px"&gt;ManagerID EmployeeID Title                                   Level&#xD;
    --------- ---------- --------------------------------------- ------&#xD;
    12        3          Engineering Manager                     2&#xD;
    &lt;/pre&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;&#xD;
&lt;div  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;&#xD;
&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" &gt;&#xD;
&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" &gt;&#xD;
&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" &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;&#xD;
&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode4" &gt;&#xD;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;&lt;pre style="padding-bottom: 5px; font-style: normal; margin: 0px; padding-left: 5px; padding-right: 5px; font-family: Consolas, Courier, monospace; word-wrap: break-word; word-break: break-all; font-weight: normal; padding-top: 5px"&gt;ManagerID EmployeeID Title                                   Level&#xD;
    --------- ---------- --------------------------------------- ------&#xD;
    3         4          Senior Tool Designer                    3&#xD;
    3         9          Design Engineer                         3&#xD;
    3         11         Design Engineer                         3&#xD;
    3         158        Research and Development Manager        3&#xD;
    3         263        Senior Tool Designer                    3&#xD;
    3         267        Senior Design Engineer                  3&#xD;
    3         270        Design Engineer                         3&#xD;
    &lt;/pre&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;&#xD;
&lt;div  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;&#xD;
&lt;div style="position: relative; margin-bottom: 12px; clear: both; top: -3px" &gt;&#xD;
&lt;div style="border-left: #bbbbbb 1px solid; width: auto; height: auto; border-top: #e5e5e5 3px solid; border-right: #e5e5e5 3px solid" &gt;&#xD;
&lt;div style="position: relative; background-color: #ffffff; padding-left: 4px; width: auto; padding-right: 4px; float: right; height: 0px; top: -10px" &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;&#xD;
&lt;div style="margin: 0px; padding-left: 21px; width: auto; padding-right: 21px" id="CodeSnippetContainerCode5" &gt;&#xD;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; color: black; padding-top: 0px"&gt;&lt;pre style="padding-bottom: 5px; font-style: normal; margin: 0px; padding-left: 5px; padding-right: 5px; font-family: Consolas, Courier, monospace; word-wrap: break-word; word-break: break-all; font-weight: normal; padding-top: 5px"&gt;ManagerID EmployeeID Title                                   Level&#xD;
    --------- ---------- --------------------------------------- ------&#xD;
    NULL      109        Chief Executive Officer                 0&#xD;
    109       12         Vice President of Engineering           1&#xD;
    12        3          Engineering Manager                     2&#xD;
    3         4          Senior Tool Designer                    3&#xD;
    3         9          Design Engineer                         3&#xD;
    3         11         Design Engineer                         3&#xD;
    3         158        Research and Development Manager        3&#xD;
    3         263        Senior Tool Designer                    3&#xD;
    3         267        Senior Design Engineer                  3&#xD;
    3         270        Design Engineer                         3&#xD;
    263       5          Tool Designer                           4&#xD;
    263       265        Tool Designer                           4&#xD;
    158       79         Research and Development Engineer       4&#xD;
    158       114        Research and Development Engineer       4&#xD;
    158       217        Research and Development Manager        4&#xD;
    (15 row(s) affected)&#xD;
    &lt;/pre&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;&#xD;
&lt;div  xmlns=""&gt;&#xD;
&lt;div style="margin-top: 19px; margin-bottom: 19px" &gt;&lt;span style="font-family: 'Segoe UI', Verdana, Arial; float: left; color: #3f529c; font-size: 1.53em; font-weight: bold" &gt;请参阅&lt;/span&gt; &#xD;
&lt;div style="padding-top: 12px" &gt;&#xD;
&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"  /&gt;&#xD;
&lt;/div&gt;&lt;/div&gt;&lt;a id="seeAlsoToggle" xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;/a&gt;&#xD;
&lt;h4 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1em; font-weight: bold"  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;&#xD;
&lt;h4 style="font-family: 'Segoe UI', Verdana, Arial; color: #3f529c; font-size: 1em; font-weight: bold"  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;&#xD;
&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;public sealed class SpecialDateTime {&lt;/p&gt;&lt;p&gt;private int _year, _month, _day;&lt;/p&gt;&lt;p&gt;private int _week;&lt;/p&gt;&lt;p&gt;private int _maxDays;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;public SpecialDateTime(int year, int month, int day) {&lt;/p&gt;&lt;p&gt;_year = year;&lt;/p&gt;&lt;p&gt;_month = month;&lt;/p&gt;&lt;p&gt;_day = day;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;_maxDays = DateTime.DaysInMonth(year, month);&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public SpecialDateTime(int year, int month) {&lt;/p&gt;&lt;p&gt;_year = year;&lt;/p&gt;&lt;p&gt;_month = month;&lt;/p&gt;&lt;p&gt;_day = 1;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;_maxDays = DateTime.DaysInMonth(year, month);&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public SpecialDateTime() { }&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;public int Removal {&lt;/p&gt;&lt;p&gt;get {&lt;/p&gt;&lt;p&gt;int removal = 7 - (int)(new DateTime(_year, _month, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;return removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public int Markup {&lt;/p&gt;&lt;p&gt;get {&lt;/p&gt;&lt;p&gt;return (6 - (int)(new DateTime(_year, _month, _maxDays).DayOfWeek));&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;public int WeeksInMonth {&lt;/p&gt;&lt;p&gt;get {&lt;/p&gt;&lt;p&gt;return (_maxDays + Markup - Removal) / 7;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public int DaysInMonth {&lt;/p&gt;&lt;p&gt;get { return (EndDateOfMonth - StartDateOfMonth).Days + 1; }&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public DateTime StartDateOfMonth {&lt;/p&gt;&lt;p&gt;get { return new DateTime(_year, _month, 1).AddDays(Removal); }&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public DateTime EndDateOfMonth {&lt;/p&gt;&lt;p&gt;get { return new DateTime(_year, _month, _maxDays).AddDays(Markup); }&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;public void GetWeekInMonthBy(int year, int month, int day, out int realMonth, out int week) {&lt;/p&gt;&lt;p&gt;int removal = 7 - (int)(new DateTime(year, month, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;removal = removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;TimeSpan ts = new DateTime(year, month, day) - new DateTime(year, month, 1).AddDays(removal);&lt;/p&gt;&lt;p&gt;if(ts.Days &amp;lt; 0) {&lt;/p&gt;&lt;p&gt;realMonth = month - 1;&lt;/p&gt;&lt;p&gt;removal = 7 - (int)(new DateTime(year, realMonth, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;removal = removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;TimeSpan ts1 = new DateTime(year, month, day) - new DateTime(year, realMonth, 1).AddDays(removal);&lt;/p&gt;&lt;p&gt;week = (int)Math.Ceiling(ts1.Days == 0 ? 1 : (ts1.Days) * 1.0 / 7);&lt;/p&gt;&lt;p&gt;} else {&lt;/p&gt;&lt;p&gt;realMonth = month;&lt;/p&gt;&lt;p&gt;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;if(ts.Days != 0 &amp;amp;&amp;amp; ts.Days % 7 == 0) {&lt;/p&gt;&lt;p&gt;week = week + 1;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public DateTime GetDateBy(int year, int month, int weekInMonth, DayOfWeek dayOfWeek) {&lt;/p&gt;&lt;p&gt;if(month &amp;gt; 12) {&lt;/p&gt;&lt;p&gt;year += month / 12;&lt;/p&gt;&lt;p&gt;month = month % 12;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;int removal = 7 - (int)(new DateTime(year, month, 1).DayOfWeek);&lt;/p&gt;&lt;p&gt;removal = removal == 7 ? 0 : removal;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;DateTime startDate = new DateTime(year, month, 1).AddDays(removal);&lt;/p&gt;&lt;p&gt;return startDate.AddDays((weekInMonth - 1) * 7 + (int)dayOfWeek);&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;public void GetDateRangeInWeek(int year, int month, int week, out DateTime startDate, out DateTime endDate) {&lt;/p&gt;&lt;p&gt;startDate = (new DateTime(year, month, 1).AddDays(Removal).AddDays((week - 1) * 7));&lt;/p&gt;&lt;p&gt;endDate = (new DateTime(year, month, 1).AddDays(Removal).AddDays((week - 1) * 7 + 6));&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;public IList&amp;lt;DateTime&amp;gt; GetDatesBy(int minWeekInMonth, DayOfWeek dow, int interval) {&lt;/p&gt;&lt;p&gt;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;while(GetDateBy(_year, _month, minWeekInMonth, dow) &amp;lt;= EndDateOfMonth) {&lt;/p&gt;&lt;p&gt;dates.Add(GetDateBy(_year, _month, minWeekInMonth, dow));&lt;/p&gt;&lt;p&gt;minWeekInMonth += interval;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;return dates;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;public IList&amp;lt;DateTime&amp;gt; GetDatesBy(DateTime date, int interval, string unit, int realMonth) {&lt;/p&gt;&lt;p&gt;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;SpecialDateTime sdt = new SpecialDateTime(date.Year, realMonth);&lt;/p&gt;&lt;p&gt;if(unit.Equals("W")) {&lt;/p&gt;&lt;p&gt;do {&lt;/p&gt;&lt;p&gt;DateTime d = date.AddDays(7 * interval);&lt;/p&gt;&lt;p&gt;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;dates.Add(d);&lt;/p&gt;&lt;p&gt;date = d;&lt;/p&gt;&lt;p&gt;} while(sdt.StartDateOfMonth &amp;lt;= date &amp;amp;&amp;amp; date &amp;lt;= sdt.EndDateOfMonth);&lt;/p&gt;&lt;p&gt;} else {&lt;/p&gt;&lt;p&gt;int week, month;&lt;/p&gt;&lt;p&gt;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;DateTime d = sdt.GetDateBy(date.Year, month + interval, week, date.DayOfWeek);&lt;/p&gt;&lt;p&gt;if(realMonth == month + interval) {&lt;/p&gt;&lt;p&gt;dates.Add(d);&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;return dates;&lt;/p&gt;&lt;p&gt;}&lt;/p&gt;&lt;p&gt;}&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  style="font-family: simsun; line-height: 23px; font-size: 12px; "&gt;&lt;pre&gt;&lt;div&gt;&lt;font  color="#008080"&gt;&lt;span  style="line-height: 18px;"&gt;轉載自：&lt;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;&lt;/p&gt;&lt;p&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;&lt;span  style="line-height: 23px; "&gt;&lt;span style="line-height: 18px; color: #000000; "&gt;Case具有两种格式。简单Case函数和Case搜索函数。 &#xD;
&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;&#xD;
&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&#xD;
         &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;&#xD;
         &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;&#xD;
&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;&#xD;
&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;&#xD;
&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;&#xD;
         &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;&#xD;
&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;&#xD;
&#xD;
这两种方式，可以实现相同的功能。简单Case函数的写法相对比较简洁，但是和Case搜索函数相比，功能方面会有些限制，比如写判断式。 &#xD;
还有一个需要注意的问题，Case函数只返回第一个符合条件的值，剩下的Case部分将会被自动忽略。 &#xD;
&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;&#xD;
&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;&#xD;
         &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;&#xD;
&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;&#xD;
&#xD;
下面我们来看一下，使用Case函数都能做些什么事情。 &#xD;
&#xD;
一，已知数据按照另外一种方式进行分组，分析。 &#xD;
&#xD;
有如下数据:(为了看得更清楚，我并没有使用国家代码，而是直接用国家名作为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;) &#xD;
国家（country）    人口（population）&#xD;
中国    &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;&#xD;
美国    &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;&#xD;
加拿大    &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;&#xD;
英国    &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;&#xD;
法国    &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;&#xD;
日本    &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;&#xD;
德国    &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;&#xD;
墨西哥    &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;&#xD;
印度    &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;&#xD;
&#xD;
根据这个国家人口数据，统计亚洲和北美洲的人口数量。应该得到下面这个结果。 &#xD;
洲    人口&#xD;
亚洲    &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;&#xD;
北美洲    &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;&#xD;
其他    &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;&#xD;
&#xD;
想要解决这个问题，你会怎么做？生成一个带有洲Code的View，是一个解决方法，但是这样很难动态的改变统计的方式。 &#xD;
如果使用Case函数，SQL代码如下: &#xD;
&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),&#xD;
        &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&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
        &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;&#xD;
&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&#xD;
&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&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
                &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;&#xD;
        &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;;&#xD;
&#xD;
同样的，我们也可以用这个方法来判断工资的等级，并统计每一等级的人数。SQL代码如下； &#xD;
&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;&#xD;
        &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;&#xD;
             &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;&#xD;
             &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;&#xD;
             &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;&#xD;
        &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,&#xD;
        &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;)&#xD;
&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&#xD;
&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;&#xD;
        &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;&#xD;
             &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;&#xD;
             &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;&#xD;
             &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;&#xD;
        &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;;&#xD;
&#xD;
二，用一个SQL语句完成不同条件的分组。 &#xD;
&#xD;
有如下数据 &#xD;
国家（country）    性别（sex）    人口（population）&#xD;
中国    &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;&#xD;
中国    &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;&#xD;
美国    &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;&#xD;
美国    &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;&#xD;
加拿大    &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;&#xD;
加拿大    &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;&#xD;
英国    &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;&#xD;
英国    &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;&#xD;
&#xD;
按照国家和性别进行分组，得出结果如下 &#xD;
国家    男    女&#xD;
中国    &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;&#xD;
美国    &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;&#xD;
加拿大    &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;&#xD;
英国    &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;&#xD;
&#xD;
普通情况下，用UNION也可以实现用一条语句进行查询。但是那样增加消耗(两个Select部分)，而且SQL语句会比较长。 &#xD;
下面是一个是用Case函数来完成这个功能的例子 &#xD;
&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,&#xD;
       &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; &#xD;
                      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;&#xD;
&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; &#xD;
                      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;&#xD;
&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&#xD;
&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;&#xD;
&#xD;
这样我们使用Select，完成对二维表的输出形式，充分显示了Case函数的强大。 &#xD;
&#xD;
三，在Check中使用Case函数。 &#xD;
&#xD;
在Check中使用Case函数在很多情况下都是非常不错的解决方法。可能有很多人根本就不用Check，那么我建议你在看过下面的例子之后也尝试一下在SQL中使用Check。 &#xD;
下面我们来举个例子 &#xD;
公司A，这个公司有个规定，女职员的工资必须高于1000块。如果用Check和Case来表现的话，如下所示 &#xD;
&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;&#xD;
           ( &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;&#xD;
                  &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;&#xD;
                        &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;&#xD;
                  &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; )&#xD;
&#xD;
如果单纯使用Check，如下所示 &#xD;
&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;&#xD;
           ( 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; )&#xD;
&#xD;
女职员的条件倒是符合了，男职员就无法输入了。 &#xD;
&lt;/span&gt;&lt;/span&gt;&#xD;
&#xD;
&lt;/span&gt;&lt;/p&gt;&lt;/pre&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;&lt;/p&gt;&lt;p&gt;有很多人不放心使用 using 語句來關閉而選擇顯式的使用 SqlConnection.Close() 方法來關閉。很有可能是因爲對 using 的不了解，現在帖出MSDN的一些解釋，如下：&lt;/p&gt;&lt;p&gt;&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  style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;div  xmlns=""&gt;&lt;div  style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span  style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;备注&lt;/span&gt;&lt;div  style="padding-top: 12px; "&gt;&lt;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  xmlns=""&gt;&lt;/div&gt;&lt;div  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  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 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&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  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  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 style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "&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  xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;span  style="font-size: 20px; color: #3f529c; font-weight: bold; "&gt;示例&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div  xmlns=""&gt;&lt;div  style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;div  style="padding-top: 12px; "&gt;&lt;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  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  style="vertical-align: middle; font-family: 'Segoe UI', Verdana, Arial; font-size: 10pt !important; height: 22px; position: relative; z-index: 1; "&gt;&lt;div  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  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"  style="font-weight: bold !important; color: #1364c4; text-decoration: none; "&gt;VB&lt;/a&gt;&lt;/div&gt;&lt;div  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  style="font-weight: bold !important; color: #e66a38; text-decoration: none; "&gt;C#&lt;/a&gt;&lt;/div&gt;&lt;div  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"  style="font-weight: normal !important; color: #1364c4; text-decoration: none; "&gt;C++&lt;/a&gt;&lt;/div&gt;&lt;div  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"  style="font-weight: normal !important; color: #1364c4; text-decoration: none; "&gt;F#&lt;/a&gt;&lt;/div&gt;&lt;div  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"  style="font-weight: normal !important; color: #1364c4; text-decoration: none; "&gt;JScript&lt;/a&gt;&lt;/div&gt;&lt;div  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  style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div  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  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"  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;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; "&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()&#xD;
{&#xD;
    &lt;span style="color: blue; "&gt;string&lt;/span&gt; connectionString = GetConnectionString();&#xD;
    &lt;span style="color: blue; "&gt;using&lt;/span&gt; (SqlConnection connection = &lt;span style="color: blue; "&gt;new&lt;/span&gt; SqlConnection(connectionString))&#xD;
    {&#xD;
        connection.Open();&#xD;
        Console.WriteLine(&lt;span style="color: #a31515; "&gt;"ServerVersion: {0}"&lt;/span&gt;, connection.ServerVersion);&#xD;
        Console.WriteLine(&lt;span style="color: #a31515; "&gt;"State: {0}"&lt;/span&gt;, connection.State);&#xD;
    }&#xD;
}&#xD;
&#xD;
&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()&#xD;
{&#xD;
    &lt;span style="color: green; "&gt;// To avoid storing the connection string in your code, &lt;/span&gt;&#xD;
    &lt;span style="color: green; "&gt;// you can retrieve it from a configuration file, using the &lt;/span&gt;&#xD;
    &lt;span style="color: green; "&gt;// System.Configuration.ConfigurationSettings.AppSettings property &lt;/span&gt;&#xD;
    &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;&#xD;
        + &lt;span style="color: #a31515; "&gt;"Integrated Security=SSPI;"&lt;/span&gt;;&#xD;
}&#xD;
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/span&gt;&lt;p&gt;&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  style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;div  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;&lt;/p&gt;&lt;p&gt;定义一个范围，将在此范围之外释放一个或多个对象。&lt;/p&gt;&lt;div  xmlns=""&gt;&lt;div  style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span  style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;语法&lt;/span&gt;&lt;div  style="padding-top: 12px; "&gt;&lt;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  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  style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div  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  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"  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;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; "&gt;using (Font font1 = new Font("Arial", 10.0f))&#xD;
{&#xD;
}&#xD;
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div  xmlns=""&gt;&lt;div  style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span  style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;备注&lt;/span&gt;&lt;div  style="padding-top: 12px; "&gt;&lt;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  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  style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div  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  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"  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;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; "&gt;Font font2 = new Font("Arial", 10.0f);&#xD;
using (font2)&#xD;
{&#xD;
    // use font2&#xD;
}&#xD;
&lt;/pre&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  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  style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div  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  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"  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;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; "&gt;using (Font font3 = new Font("Arial", 10.0f), &#xD;
           font4 = new Font("Arial", 10.0f))&#xD;
{&#xD;
    // Use font3 and font4.&#xD;
}&#xD;
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div  xmlns=""&gt;&lt;div  style="margin-top: 19px; margin-bottom: 19px; "&gt;&lt;span  style="font-family: 'Segoe UI', Verdana, Arial; font-size: 1.538em; color: #3f529c; font-weight: bold; float: left; "&gt;示例&lt;/span&gt;&lt;div  style="padding-top: 12px; "&gt;&lt;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  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  style="clear: both; margin-bottom: 12px; position: relative; top: -3px; "&gt;&lt;div  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  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"  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;pre style="padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: Consolas, Courier, monospace; word-break: break-all; word-wrap: break-word; font-style: normal; font-weight: normal; "&gt;using System;&#xD;
&#xD;
class C : IDisposable&#xD;
{&#xD;
    public void UseLimitedResource()&#xD;
    {&#xD;
        Console.WriteLine("Using limited resource...");&#xD;
    }&#xD;
&#xD;
    void IDisposable.Dispose()&#xD;
    {&#xD;
        Console.WriteLine("Disposing limited resource.");&#xD;
    }&#xD;
}&#xD;
&#xD;
class Program&#xD;
{&#xD;
    static void Main()&#xD;
    {&#xD;
        using (C c = new C())&#xD;
        {&#xD;
            c.UseLimitedResource();&#xD;
        }&#xD;
        Console.WriteLine("Now outside using statement.");&#xD;
        Console.ReadLine();&#xD;
    }&#xD;
}&#xD;
&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div  xmlns=""&gt;&lt;div  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;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;第三。總結：&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;由於using 語句是：&lt;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  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;&lt;/p&gt;&lt;p&gt;另外，在MSDN有人（&lt;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  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  style="font-family: 'Segoe UI', Verdana, Arial; line-height: normal; font-size: 13px; "&gt;&lt;div  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  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;&lt;pre&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;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;IL代码可为：&lt;/p&gt;&lt;pre&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;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;可以看到调用了SqlConnection的Dispose方法释放了资源。&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;p&gt;&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><entry><id>http://www.cnblogs.com/Koy/archive/2010/07/23/1784063.html</id><title type="text">一個簡單的排班方法</title><summary type="text">目的：排出 2010年1~12月的A.P.N排班記錄void Main(){	IList&amp;lt;Schedule&amp;gt; schedules = new List&amp;lt;Schedule&amp;gt;();		int year = 2010;	int[] months = {1,2,3,4,5,6,7,8,9,10,11,12};		foreach(int i in months){		int maxD...</summary><published>2010-07-23T15:06:00Z</published><updated>2010-07-23T15:06: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/07/23/1784063.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/07/23/1784063.html"/><content type="html">&lt;div&gt;目的：排出 2010年1~12月的A.P.N排班記錄&amp;nbsp;&lt;/div&gt;&lt;p&gt;void Main()&lt;/p&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;IList&amp;lt;Schedule&amp;gt; schedules = new List&amp;lt;Schedule&amp;gt;();&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;int year = 2010;&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;int[] months = {1,2,3,4,5,6,7,8,9,10,11,12};&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;foreach(int i in months){&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;		&lt;/span&gt;int maxDays = DateTime.DaysInMonth(year, i);&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;		&lt;/span&gt;for(int j=1; j&amp;lt;=maxDays; j++){&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;			&lt;/span&gt;foreach(string n in Enum.GetNames(typeof(ScheduleMode))){&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;				&lt;/span&gt;schedules.Add(new Schedule(){&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;					&lt;/span&gt;Year = year,&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;					&lt;/span&gt;Month = i,&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;					&lt;/span&gt;Date = j,&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;					&lt;/span&gt;Mode = (ScheduleMode)Enum.Parse(typeof(ScheduleMode), n),&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;					&lt;/span&gt;Description = string.Format("Work on {3}, {0}/{1}/{2}", year,i,j, new DateTime(year,i,j).DayOfWeek)&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;				&lt;/span&gt;});&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;			&lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;		&lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;schedules.Dump();&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;// Define other methods and classes here&lt;/div&gt;&lt;div&gt;public enum ScheduleMode {&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;A,P,N&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;div&gt;public class Schedule{&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;public int Year {get;set;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;public int Month {get;set;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;public int Date {get;set;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;		&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;public ScheduleMode Mode {get;set;}&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span  style="white-space:pre"&gt;	&lt;/span&gt;public string Description {get;set;}&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;p&gt;//////////////////////////////////////////////////////////////////////////////////////&lt;/p&gt;&lt;p&gt;這裏隱含了另一個功能，可以通過Enum.GetNames(typeof(ScheduleMode))獲得枚舉類型的各項名稱（用於綁定到UI界面），還可以通過(ScheduleMode)Enum.Parse(typeof(ScheduleMode), n)將某個字符串轉換成枚舉類型（用於從數據庫查詢到的值轉換成枚舉類型），這樣我們就可以很方便地將數據庫中的查詢到的值轉換成&amp;#8220;預先&amp;#8221;定義好的枚舉類型（可以有&amp;#8220;名稱&amp;#8221;和&amp;#8220;值&amp;#8221;）。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/Koy/aggbug/1784063.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/Koy/archive/2010/07/23/1784063.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/Koy/archive/2010/07/22/1782906.html</id><title type="text">轉載：利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題</title><summary type="text">[ASP.NET]利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題轉載自：http://www.dotblogs.com.tw/puma/archive/2009/05/28/asp.net-generic-list-sort-find-findall-exsit.aspx最近寫案子常常用到List&amp;lt;T&amp;gt;,這個東西還真好用因為它有下列東西：Li...</summary><published>2010-07-22T03:33:00Z</published><updated>2010-07-22T03:33: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/07/22/1782906.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/Koy/archive/2010/07/22/1782906.html"/></entry></feed>
