<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>萝卜根</title>
	<atom:link href="http://www.cxybase.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cxybase.com</link>
	<description>君子务本，本立而道生</description>
	<lastBuildDate>Sat, 12 May 2012 06:41:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>NuGet简介</title>
		<link>http://www.cxybase.com/201204/nuget%e7%ae%80%e4%bb%8b/</link>
		<comments>http://www.cxybase.com/201204/nuget%e7%ae%80%e4%bb%8b/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 15:38:26 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[软件工程]]></category>
		<category><![CDATA[NuGet]]></category>
		<category><![CDATA[NuGet使用]]></category>
		<category><![CDATA[NuGet简介]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2248</guid>
		<description><![CDATA[<p>NuGet 是一种 Visual Studio 扩展，它能够简化在 Visual Studio 项目中添加、更新和删除库（部署为程序包）的操作。 NuGet 程序包是打包成一个文件的文件集，扩展名是 . nupkg，使用开放打包约定 (OPC) 格式。</p><p>OPC 仅仅是具有某些元数据的 zip 文 <span class='read-more'><a href='http://www.cxybase.com/201204/nuget%e7%ae%80%e4%bb%8b/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p>NuGet 是一种 Visual Studio 扩展，它能够简化在 Visual Studio 项目中添加、更新和删除库（部署为程序包）的操作。 NuGet 程序包是打包成一个文件的文件集，扩展名是 . nupkg，使用开放打包约定 (OPC) 格式。</p>
<p>OPC 仅仅是具有某些元数据的 zip 文件的首字母缩写词。 事实上，您可能早已熟悉 OPC，因为 Word 和 Excel 文档正是使用此格式。 如果您取一个 .docx 文件并将文件扩展名改为 .zip，您实际可以打开它并浏览里面的内容。 . nupkg 文件同样如此。</p>
<p>NuGet 产品同样随附能够轻松创建和发布程序包的实用工具。 现在，我先重点介绍如何使用 NuGet 发现和安装程序包。 之后，我将讲述如何创建和发布程序包。</p>
<p>具体使用参考：</p>
<h1><a href="http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx" target="_blank">使用 NuGet 管理项目库</a></h1>
<h2  class="related_post_title">随机日志</h2><ul class="related_post"><li>2010/12/16 -- <a href="http://www.cxybase.com/201012/google-%e5%85%ac%e5%b8%83-2010-%e5%b9%b4%e6%90%9c%e7%b4%a2%e7%83%ad%e8%af%8d/" title="Google 公布 2010 年搜索热词">Google 公布 2010 年搜索热词</a> (0)</li><li>2011/08/28 -- <a href="http://www.cxybase.com/201108/c%e5%9f%ba%e7%a1%80%ef%bc%9alambda%e8%a1%a8%e8%be%be%e5%bc%8f/" title="C#基础：Lambda表达式">C#基础：Lambda表达式</a> (0)</li><li>2011/01/08 -- <a href="http://www.cxybase.com/201101/vim%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%88%e5%9b%9b%ef%bc%89%ef%bc%9avim%e5%b8%b8%e7%94%a8%e8%ae%be%e7%bd%ae%e5%91%bd%e4%bb%a4/" title="VIM学习笔记（四）：VIM常用设置命令">VIM学习笔记（四）：VIM常用设置命令</a> (0)</li><li>2011/01/24 -- <a href="http://www.cxybase.com/201101/open-source-physics-engine/" title="Open Source Physics Engine">Open Source Physics Engine</a> (0)</li><li>2011/01/08 -- <a href="http://www.cxybase.com/201101/2011%e5%b9%b4%e5%ba%94%e5%85%b3%e6%b3%a8%e7%9a%845%e9%a1%b9web%e6%8a%80%e6%9c%af/" title="2011年应关注的5项Web技术 ">2011年应关注的5项Web技术 </a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/nuget%e7%ae%80%e4%bb%8b/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity FrameWork Concurrency(并发)</title>
		<link>http://www.cxybase.com/201204/entity-framework-concurrency%e5%b9%b6%e5%8f%91/</link>
		<comments>http://www.cxybase.com/201204/entity-framework-concurrency%e5%b9%b6%e5%8f%91/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 13:14:27 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[EF Concurrency]]></category>
		<category><![CDATA[Entity FrameWork Concurrency]]></category>
		<category><![CDATA[Entity FrameWork 并发]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2244</guid>
		<description><![CDATA[<p><strong>一、什么是并发冲突</strong></p><p>让我们来看一个跟取款相关的例子：某年某月某日某时某分，老王在A取款机取钱，他儿子小王同时在B取款机取钱（不要问我为什么这么巧^_^），他俩从同一个账号上取。于是就发生了如下一序列的操作：</p><p>A取款机向中央数据库提问：这账上还有多少钱？</p><p>B取款机向中央数据库询问：这账上还 <span class='read-more'><a href='http://www.cxybase.com/201204/entity-framework-concurrency%e5%b9%b6%e5%8f%91/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p><strong>一、什么是并发冲突</strong></p>
<p>让我们来看一个跟取款相关的例子：某年某月某日某时某分，老王在A取款机取钱，他儿子小王同时在B取款机取钱（不要问我为什么这么巧^_^），他俩从同一个账号上取。于是就发生了如下一序列的操作：</p>
<p>A取款机向中央数据库提问：这账上还有多少钱？</p>
<p>B取款机向中央数据库询问：这账上还有多少钱？</p>
<p>中央数据库回答A取款机：2W。</p>
<p>中央数据库回答B取款机：2W。</p>
<p>然后，老王对A取款机说：我要取出1.5W。</p>
<p>同时，小王对B取款机说：我要取出1.8W。</p>
<p>A取款机就算了一下，2W-1.5W=0.5W&gt;0，于是就吐出1.5W现金给了老王，并且准备告诉中央数据库，现在还剩0.5W啦。但是，就在它告诉中央数据库之前，发生了以下的事情：</p>
<p>B取款机计算了一下，2W（此时，它还不知道余额已经成0.5W了，因为A取款机还没有告诉中央数据库）减去1.8W等于0.2W大于0，于是就吐出1.8W现金给了小王。然后，它当然也要知会中央数据库。</p>
<p>中央数据库于是收到A取款机的消息，说，这个账号还剩0.5W，于是刷新余额为0.5W。然后又收到B取款机说还剩0.2W，于是，就刷新余额为0.2W。</p>
<p>呵呵，于是，小王+老王的账户里一共存有2W元，结果老王取了1.5W元，小王取了1.8W元，账户里却还剩了0.2W元!~@#$%^&amp;</p>
<p>这就是一种并发冲突，由于同一时间有两个或者多个端在对同一数据进行操作，从而导致数据发生了错误。</p>
<p><strong>二、常见的并发冲突处理方式</strong></p>
<p>一般来说，我们把并发冲突处理方式归结为3类。</p>
<p>第一类：放任不管方式；第二类：开放式并发处理方式；第三类：保守式并发处理方式。</p>
<p>1. 放任不管方式：</p>
<p>与其说这是一种处理并发冲突的方式，不如说，它是一种没有对并发冲突做任何处理的方式。但是在许多过去的系统里，由于没有考虑到多用户、网络应用等情况，这种&#8221;处理方式&#8221;还真存在于不少系统中。</p>
<p>举例来说，A、B两人从数据库中获取了同一个笔记本的信息，例如：IBM ThinkPad T61吧。然后：A把牌子改成了：Lenovo ThinkPad，B把型号改成了T61 8890A24。然后，他们开始提交了。此时，如果A先提交，然后B提交，那么，最后的结果是：IBM ThinkPad T61 8890A24；反之，则变成Lenovo ThinkPad T61。</p>
<p>总之一句话，谁最后提交谁老大。想像一下，如果A修改了1000个属性的值，B修改了1个属性的值，那么，对于先提交的A来说，这将是一个多么惨痛的打击:-)</p>
<p>虽然这种放任不管的方式似乎不太负责任，但是，其处理性能却是相对较高的。</p>
<p>2. 开放式并发处理</p>
<p>开放式并发处理，老外叫做Optimistic Concurrency——乐观的并发。这种并发处理方式要求我们对并发抱有一种乐观的态度：百分之九十九点九九不会发生并发冲突，万一发生了，系统也能捕获到冲突，或者根据策略自动处理，或者，就提醒一下用户，让用户来决定是不是要继续提交。</p>
<p>仍然用上面的例子来说这事儿：A、B两个人同时获取了笔记本的信息：IBM ThinkPad T61。然后……（此处跟上例做一样的修改，直到提交）此时，如果A先提交，那么，B提交的时候，系统会发现，哎哟，不好，有并发冲突了，就会抛个异常给B，让B知道，发生并发冲突了，然后，B就可以根据实际情况，选择相应的处理策略（比如，继续提交进行覆盖或者取消提交等等）；相反，如果B先提交，那么，A提交时，就会得到相应的提醒。</p>
<p>这样的并发处理方式，可以说在可靠性与性能上取得平衡，适合于对数据可靠性要求不是特别严格，需要较高的性能，并且不会大量发生并发的场合。</p>
<p>3. 保守式并发处理</p>
<p>这是最为严谨的一种并发冲突的处理方式。它把并发转化为了串行操作。</p>
<p>例如，A从数据库中获取了笔记本信息：IBM ThinkPad T61，B也要对其进行修改，但此时由于A已经从数据库中将数据取出，因此，B被置于等待状态。直到A把数据修改完提交了，数据库数据更新为Lenovo ThinkPad T61了，此时，数据库才把数据给B，那么B就可以在Lenovo ThinkPad T61的基础上，把它修改为Lenovo ThinkPad T61 8890A24。而在B提交前，其它一切针对此记录的操作都得排除等着B。</p>
<p>这样子当然非常理想，由于不存在并发，自然也就消除了并发冲突的问题。但是，这种锁也存在着较为隐蔽的风险：如果A修改了数据，一直不提交，或者A因为故障，没有办法提交，那么，其它所有的相关的操作，都将被阻碍住。因此，只有对数据准确性要求极高并且用户可以忍受等待的情况下，使用这种并发冲突的处理方法。<br />
<strong>三、EF并发冲突处理实例</strong></p>
<p>EF发布时，提供了两种并发冲突处理方式：放任不管方式和开放式并发。默认采用放任不管的方式处理。如果要使用开放式并发，那么，必须设置相应属性上的Concurrency Mode值为Fixed。</p>
<p>原文：http://www.cnblogs.com/xiaomi7732/archive/2008/08/27/1278062.html</p>
<h2  class="related_post_title">随机日志</h2><ul class="related_post"><li>2011/05/11 -- <a href="http://www.cxybase.com/201105/google%e7%9a%84%e9%87%8e%e5%bf%83-android%e6%9c%aa%e6%9d%a5%e6%96%b9%e5%90%91%e5%88%86%e6%9e%90/" title="Google的野心 Android未来方向分析">Google的野心 Android未来方向分析</a> (0)</li><li>2011/01/03 -- <a href="http://www.cxybase.com/201101/webkit%e7%bc%96%e8%af%91%e5%b0%8f%e7%bb%93/" title="WebKit编译小结">WebKit编译小结</a> (0)</li><li>2010/11/21 -- <a href="http://www.cxybase.com/201011/wordpress-continuous-announcement/" title="wordpress 的公告插件Continuous announcement 使用">wordpress 的公告插件Continuous announcement 使用</a> (6)</li><li>2011/02/26 -- <a href="http://www.cxybase.com/201102/%e8%a1%a5%e7%a0%81%e5%8a%a0%e6%b3%95%e8%bf%90%e7%ae%97/" title="补码加法运算">补码加法运算</a> (1)</li><li>2011/04/05 -- <a href="http://www.cxybase.com/201104/90%e5%90%8e%e8%8b%b1%e5%9b%bd%e4%b8%ad%e5%ad%a6%e7%94%9f%e5%bb%ba%e7%ab%8b%e9%bb%91%e5%ae%a2%e7%a4%be%e4%ba%a4%e7%bd%91-%e6%b6%89%e6%a1%88%e9%87%91%e9%a2%9d%e8%be%be1-8%e4%ba%bf%e5%85%83/" title="90后英国中学生建立黑客社交网 涉案金额达1.8亿元">90后英国中学生建立黑客社交网 涉案金额达1.8亿元</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/entity-framework-concurrency%e5%b9%b6%e5%8f%91/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL SERVER JOB程序操作</title>
		<link>http://www.cxybase.com/201204/sql-server-job%e6%93%8d%e4%bd%9c/</link>
		<comments>http://www.cxybase.com/201204/sql-server-job%e6%93%8d%e4%bd%9c/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 12:13:48 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[job]]></category>
		<category><![CDATA[SQL Server Agent]]></category>
		<category><![CDATA[SQL SERVER JOB]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2235</guid>
		<description><![CDATA[<p>我们有两种方式通过程序对JOB操作，系统SP和SMO类API。</p><p>一、SMO类</p><p>需要添加的Reference DLL：</p><p>Microsoft.SqlServer.ConnectionInfo.dll</p><p>Microsoft.SqlServer.Smo.dll</p><p>Microsoft.SqlS <span class='read-more'><a href='http://www.cxybase.com/201204/sql-server-job%e6%93%8d%e4%bd%9c/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p>我们有两种方式通过程序对JOB操作，系统SP和SMO类API。</p>
<p>一、SMO类</p>
<p>需要添加的Reference DLL：</p>
<p>Microsoft.SqlServer.ConnectionInfo.dll</p>
<p>Microsoft.SqlServer.Smo.dll</p>
<p>Microsoft.SqlServer.Management.Sdk.Sfc.dll</p>
<p>Microsoft.SqlServer.SqlEnum.dll</p>
<p>命名空间：</p>
<p>Using Microsoft.SqlServer.Management.Smo</p>
<p>Using Microsoft.SqlServer.Management.Common</p>
<p>Using Microsoft.SqlServer.Management.Smo.Agent</p>
<p>MS官方实例：</p>
<p>Creating a Job with Steps and a Schedule in Visual C#：</p>
<pre class="brush:csharp"> //Connect to the local, default instance of SQL Server.
            Server srv = new Server();

            //Define an Operator object variable by supplying the Agent (parent JobServer object) and the name in the constructor.
            Operator op = new Operator(srv.JobServer, "Test_Operator");

            //Set the Net send address.
            op.NetSendAddress = "Network1_PC";

            //Create the operator on the instance of SQL Server Agent.
            op.Create();

            //Define a Job object variable by supplying the Agent and the name arguments in the constructor and setting properties.
            Job jb = new Job(srv.JobServer, "Test_Job");

            //Specify which operator to inform and the completion action.
            jb.OperatorToNetSend = "Test_Operator";
            jb.NetSendLevel = CompletionAction.Always;

            //Create the job on the instance of SQL Server Agent.
            jb.Create();

            //Define a JobStep object variable by supplying the parent job and name arguments in the constructor.
            JobStep jbstp = new JobStep(jb, "Test_Job_Step");
            jbstp.Command = "Test_StoredProc";
            jbstp.OnSuccessAction = StepCompletionAction.QuitWithSuccess;
            jbstp.OnFailAction = StepCompletionAction.QuitWithFailure;

            //Create the job step on the instance of SQL Agent.
            jbstp.Create();

            //Define a JobSchedule object variable by supplying the parent job and name arguments in the constructor. 

            JobSchedule jbsch = new JobSchedule(jb, "Test_Job_Schedule");

            //Set properties to define the schedule frequency, and duration.
            jbsch.FrequencyTypes = FrequencyTypes.Daily;
            jbsch.FrequencySubDayTypes = FrequencySubDayTypes.Minute;
            jbsch.FrequencySubDayInterval = 30;
            TimeSpan ts1 = new TimeSpan(9, 0, 0);
            jbsch.ActiveStartTimeOfDay = ts1;

            TimeSpan ts2 = new TimeSpan(17, 0, 0);
            jbsch.ActiveEndTimeOfDay = ts2;
            jbsch.FrequencyInterval = 1;

            System.DateTime d = new System.DateTime(2003, 1, 1);
            jbsch.ActiveStartDate = d;

            //Create the job schedule on the instance of SQL Agent.
            jbsch.Create();</pre>
<p>Server srv = new Server();这个默认连接的是本地的SQL SERVER 实例，如果需要连接远程的只需要<br />
Server srv = new Server(“192.168.108.16”,&#8221;sa&#8221;,&#8221;sa&#8221;);即可。<br />
更多有关操作可以参考：<br />
<a href="http://www.yaldex.com/sql_server/progsqlsvr-CHP-16-SECT-1.html" target="_blank">Programming SQL Server Agent</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2  class="related_post_title">随机日志</h2><ul class="related_post"><li>2011/11/27 -- <a href="http://www.cxybase.com/201111/%e5%b7%a5%e4%bd%9c%e6%80%bb%e7%bb%93%ef%bc%88%e4%b8%80%ef%bc%892011-11-27/" title="工作总结（一）2011.11.27">工作总结（一）2011.11.27</a> (9)</li><li>2011/08/05 -- <a href="http://www.cxybase.com/201108/%e7%bd%91%e6%98%93itown%e6%b8%b8%e6%88%8f%e5%b9%b3%e5%8f%b0%ef%bc%9a%e5%bc%80%e6%94%be%e7%9a%84%e4%b8%8d%e5%8f%aa%e6%98%af%e6%8e%a5%e5%8f%a3/" title="网易iTown游戏平台：开放的不只是接口">网易iTown游戏平台：开放的不只是接口</a> (2)</li><li>2011/04/06 -- <a href="http://www.cxybase.com/201104/c%e8%af%ad%e8%a8%80%e7%bc%96%e7%a0%81%e9%a3%8e%e6%a0%bcindent%e5%b7%a5%e5%85%b7/" title="C语言编码风格（五）——indent工具">C语言编码风格（五）——indent工具</a> (0)</li><li>2011/04/07 -- <a href="http://www.cxybase.com/201104/vs2010%e7%ac%ac%e5%9b%9b%e7%af%87%ef%bc%9a%e4%b8%ba%e7%88%b1debug/" title="VS2010第四篇：为爱Debug">VS2010第四篇：为爱Debug</a> (0)</li><li>2011/07/10 -- <a href="http://www.cxybase.com/201107/staruml/" title="StarUML">StarUML</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/sql-server-job%e6%93%8d%e4%bd%9c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>T-SQL循环遍历并读取数据集的方法</title>
		<link>http://www.cxybase.com/201204/t-sql%e5%be%aa%e7%8e%af%e9%81%8d%e5%8e%86%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e9%9b%86%e7%9a%84%e6%96%b9%e6%b3%95/</link>
		<comments>http://www.cxybase.com/201204/t-sql%e5%be%aa%e7%8e%af%e9%81%8d%e5%8e%86%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e9%9b%86%e7%9a%84%e6%96%b9%e6%b3%95/#comments</comments>
		<pubDate>Sun, 15 Apr 2012 14:03:20 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[t-sql循环遍历]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2226</guid>
		<description><![CDATA[<p>在T-SQL中遍历读取数据集是件痛苦的事情，目前常见的方法主要有两种：游标和临时表/表变量。</p><p>游标是邪恶的，一般是在离开游标就不能活的情况下才去使用游标。为什么呢？详细的可以参考：</p><p><a href="http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html" target="_blank">T-SQL查询进阶-10分钟理解游标</a></p><p>这篇文章分析的很好。</p><p>1. 使用临时表进行循环遍历</p><p>（1）T <span class='read-more'><a href='http://www.cxybase.com/201204/t-sql%e5%be%aa%e7%8e%af%e9%81%8d%e5%8e%86%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e9%9b%86%e7%9a%84%e6%96%b9%e6%b3%95/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p>在T-SQL中遍历读取数据集是件痛苦的事情，目前常见的方法主要有两种：游标和临时表/表变量。</p>
<p>游标是邪恶的，一般是在离开游标就不能活的情况下才去使用游标。为什么呢？详细的可以参考：</p>
<h1><a href="http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html" target="_blank">T-SQL查询进阶-10分钟理解游标</a></h1>
<p>这篇文章分析的很好。</p>
<p>1. 使用临时表进行循环遍历</p>
<p>（1）TOP 1方法</p>
<pre class="brush:sql">SET ROWCOUNT 0

DECLARE @ID INT

CREATE TABLE #tb_ids (uID INT)
INSERT INTO #tb_ids
	SELECT userID FROM User

SELECT TOP 1 @ID = uID FROM #tb_ids
WHILE @@ROWCOUNT &lt;&gt; 0
BEGIN
	DELETE FROM #tb_ids WHERE uID = @ID

	--这里做你想干的事（根据uID）。

	SELECT @ID = uID TOP 1 FROM #tb_ids
END</pre>
<p>（2）SET ROWCOUNT 1方法</p>
<p>这个其实和方法1原理是一样的。</p>
<pre class="brush:sql">declare @au_id INT

set rowcount 0

select * into #mytemp from authors

set rowcount 1
select @au_id = au_id from #mytemp
while @@rowcount &lt;&gt; 0
begin
  set rowcount 0
  select * from #mytemp where au_id = @au_id
  delete #mytemp where au_id = @au_id

  -- 这里做你想做的事

  set rowcount 1
  select @au_id = au_id from #mytemp&lt;BR/&gt;
end

set rowcount 0</pre>
<p>&nbsp;</p>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li>2012/04/15 -- <a href="http://www.cxybase.com/201204/sql-server%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90%e6%b1%87%e6%80%bb/" title="SQL Server学习资源汇总">SQL Server学习资源汇总</a> (0)</li><li>2012/04/04 -- <a href="http://www.cxybase.com/201204/t-sql%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90/" title="T-SQL学习笔记">T-SQL学习笔记</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/t-sql%e5%be%aa%e7%8e%af%e9%81%8d%e5%8e%86%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e9%9b%86%e7%9a%84%e6%96%b9%e6%b3%95/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server学习资源汇总</title>
		<link>http://www.cxybase.com/201204/sql-server%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90%e6%b1%87%e6%80%bb/</link>
		<comments>http://www.cxybase.com/201204/sql-server%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90%e6%b1%87%e6%80%bb/#comments</comments>
		<pubDate>Sun, 15 Apr 2012 13:42:38 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[sql server学习]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2224</guid>
		<description><![CDATA[<p><a href="http://blog.csdn.net/downmoon/article/details/6238407" target="_blank">1. SQL Server 2008中SQL应用系列及BI学习笔记系列--目录索引 </a></p><p>2.<a href="http://sd.csdn.net/a/20120411/2804419.html" target="_blank">11招教你如何玩转数据库设计</a></p><p>&#160;</p><p>&#160; <span class='read-more'><a href='http://www.cxybase.com/201204/sql-server%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90%e6%b1%87%e6%80%bb/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.csdn.net/downmoon/article/details/6238407" target="_blank">1. SQL Server 2008中SQL应用系列及BI学习笔记系列&#8211;目录索引 </a></p>
<p>2.<a href="http://sd.csdn.net/a/20120411/2804419.html" target="_blank">11招教你如何玩转数据库设计</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li>2012/04/15 -- <a href="http://www.cxybase.com/201204/t-sql%e5%be%aa%e7%8e%af%e9%81%8d%e5%8e%86%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e9%9b%86%e7%9a%84%e6%96%b9%e6%b3%95/" title="T-SQL循环遍历并读取数据集的方法">T-SQL循环遍历并读取数据集的方法</a> (0)</li><li>2012/04/04 -- <a href="http://www.cxybase.com/201204/t-sql%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90/" title="T-SQL学习笔记">T-SQL学习笔记</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/sql-server%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90%e6%b1%87%e6%80%bb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL学习笔记</title>
		<link>http://www.cxybase.com/201204/t-sql%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90/</link>
		<comments>http://www.cxybase.com/201204/t-sql%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90/#comments</comments>
		<pubDate>Wed, 04 Apr 2012 09:03:14 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2214</guid>
		<description><![CDATA[<p>以下处理适用sql server 2008R2，其他版本的大都也可以。具体的没有去检查。</p><p>1. T-SQL抛出异常</p><p>官方文档 ： Generates an error message and initiates error processing for the session. RAISER <span class='read-more'><a href='http://www.cxybase.com/201204/t-sql%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p>以下处理适用sql server 2008R2，其他版本的大都也可以。具体的没有去检查。</p>
<p>1. T-SQL抛出异常</p>
<p>官方文档 ： Generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the <strong>sys.messages</strong> catalog view or build a message dynamically. The message is returned as a server error message to the calling application or to an associated CATCH block of a TRY…CATCH construct.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms178592%28v=sql.105%29.aspx" target="_blank">更多参考。</a></p>
<p>2. T-SQL  Transaction中使用TRY CATCH</p>
<pre class="brush:csharp">CREATE PROCEDURE [Name]
AS
BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
        [...Perform work, call nested procedures...]
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION
        RAISERROR [rethrow caught error using @ErrorNumber, @ErrorMessage, etc]
    END CATCH
END</pre>
<p>以上应该是个比较主流的模式，但仅仅只能对单独的SP，如果SP里面再调用其他的SP，那么调用的那个SP的ERROR抛出ROLLBACK就有问题了。</p>
<p>3. 临时表和表变量</p>
<p>基本原则：能用表变量就用表变量。实在不行才使用临时表。<br />
如与动态语句结合、外部需要使用等，就需要临时表。</p>
<p>表变量主要开销系统的内存，而临时表则使用tempdb。对于小数据量的中间数据存储，可以使用表变量，而当需要临时保存的数据量很庞大时，建议使用临时表。具体使用表变量还是临时表，可以根据系统的运行状况来调整。</p>
<pre class="brush:csharp">declare @tb table(id int,name varchar(50),age int) --创建表变量

insert @tb select 1,'nn',14
union all select 1,'nn',14

select * from @tb

create table #t(id int,name varchar(50),years int,nums int)--创建临时表

insert #t select 1,'nn',14,15
union all select 1,'nn',14,15
insert into #t  exec sp_gets  --可以用于存储过程或动态SQL结合

select * from #t
drop table #t</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2  class="related_post_title">相关文章</h2><ul class="related_post"><li>2012/04/15 -- <a href="http://www.cxybase.com/201204/t-sql%e5%be%aa%e7%8e%af%e9%81%8d%e5%8e%86%e5%b9%b6%e8%af%bb%e5%8f%96%e6%95%b0%e6%8d%ae%e9%9b%86%e7%9a%84%e6%96%b9%e6%b3%95/" title="T-SQL循环遍历并读取数据集的方法">T-SQL循环遍历并读取数据集的方法</a> (0)</li><li>2012/04/15 -- <a href="http://www.cxybase.com/201204/sql-server%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90%e6%b1%87%e6%80%bb/" title="SQL Server学习资源汇总">SQL Server学习资源汇总</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/t-sql%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%ba%90/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>C#委托及事件</title>
		<link>http://www.cxybase.com/201204/c%e5%a7%94%e6%89%98%e5%8f%8a%e4%ba%8b%e4%bb%b6/</link>
		<comments>http://www.cxybase.com/201204/c%e5%a7%94%e6%89%98%e5%8f%8a%e4%ba%8b%e4%bb%b6/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 06:53:01 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#事件]]></category>
		<category><![CDATA[C#委托]]></category>
		<category><![CDATA[C#委托及事件]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2209</guid>
		<description><![CDATA[<p>在C#中，委托（delegate）是一种引用类型，在其他语言中，与委托最接近的是函数指针，但委托不仅存储对方法入口点的引用，还存储对用于调用方法的对象实例的引用。</p><p><strong> 一、建立委托</strong></p><p>建立委托（delegate&#62;），过程有点类似于建立一个函数指针。过程如下：</p><p>1. 建立一个委托类型，并 <span class='read-more'><a href='http://www.cxybase.com/201204/c%e5%a7%94%e6%89%98%e5%8f%8a%e4%ba%8b%e4%bb%b6/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p>在C#中，委托（delegate）是一种引用类型，在其他语言中，与委托最接近的是函数指针，但委托不仅存储对方法入口点的引用，还存储对用于调用方法的对象实例的引用。</p>
<p><strong> 一、建立委托</strong></p>
<p>建立委托（delegate&gt;），过程有点类似于建立一个函数指针。过程如下：</p>
<p>1. 建立一个委托类型，并声明该委托可以指向的方法的签名（函数原型）</p>
<pre class="brush:csharp">delegate void MyDelegate(int a,int b);</pre>
<p>2.建立一个委托类的实例，并指向要调用的方法</p>
<pre class="brush:csharp">//利用委托类的构造方法指定，这是最为常见的一种方式

MyDelegate md=new MyDelegate(Max);

//利用自动推断方式来指明要调用的方法，该形式更类型于函数指针

MyDelegate md=Max;</pre>
<p>3.利用委托类实例调用所指向的方法</p>
<pre class="brush:csharp">int c=md(4,5);</pre>
<p><strong>利用委托实现方法的动态调用</strong><br />
首先，添加如下控件：<br />
Ø<span style="font-size: small;">  </span><span style="font-size: small;">两个RadioButton，分别用来让用户选择求最大值以及求最小值</span></p>
<p><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">  </span><span style="font-size: small;">二个TextBox，用来输入两个操作数</span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">  </span><span style="font-size: small;">一个TextBox，用来显示运算结果</span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">  </span><span style="font-size: small;">一个Button，用来执行运算</span></span></p>
<p><a href="http://www.cxybase.com/wp-content/uploads/2012/04/1.png"><img class="aligncenter size-full wp-image-2210" title="1" src="http://www.cxybase.com/wp-content/uploads/2012/04/1.png" alt="" width="337" height="176" /></a></p>
<p><span style="font-size: small;"><span style="font-size: small;">下一步，在窗口中添加两个方法：</span><span style="font-size: small;">Max</span><span style="font-size: small;">，</span><span style="font-size: small;">Min</span><span style="font-size: small;">，这两方法的代码如下：</span></span></p>
<pre class="brush:csharp">int Max(int x,int y)
{
   return x&gt;y?x:y;
}

int Min(int x,int y)
{
   return x&lt; font=""&gt;&lt;&gt;
}</pre>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">为了使用委托来实现动态指向，我们需要建立一个委托类“</span><span style="font-size: small;">MyDelegate</span><span style="font-size: small;">”，并建立该委托类型的一个实例。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">分别为两个</span><span style="font-size: small;">RadioButton</span><span style="font-size: small;">编写它们的“</span><span style="font-size: small;">CheckedChanged</span><span style="font-size: small;">”事件</span></span></span>：</p>
<pre class="brush:csharp">private void rbtMax_CheckedChanged(object sender, EventArgs e)
{
    if (this.rbtMax .Checked ==true)
    {
        this.md = new MyDelegate(this.Max );
    }
}
private void rbtMin_CheckedChanged(object sender, EventArgs e)
{
    if (this.rbtMin .Checked ==true)
    {
        this.md = new MyDelegate(this.Min );
    }
}</pre>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">这段代码是，如果用户选择了求最大值的</span><span style="font-size: small;">RadioButton</span><span style="font-size: small;">，则让</span><span style="font-size: small;">MyDelegate</span><span style="font-size: small;">类型的实例“</span><span style="font-size: small;">md</span><span style="font-size: small;">”指向</span><span style="font-size: small;">Max</span><span style="font-size: small;">方法，如果用户选择了求最小值的</span><span style="font-size: small;">RadioButton</span><span style="font-size: small;">，则让</span><span style="font-size: small;">MyDelegate</span><span style="font-size: small;">类型的实例“</span><span style="font-size: small;">md</span><span style="font-size: small;">”指向</span><span style="font-size: small;">Min</span><span style="font-size: small;">方法。这样作的目的，就是要把选择的过程交给用户。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">下一步，我们为界面中的</span><span style="font-size: small;">Button</span><span style="font-size: small;">编写</span><span style="font-size: small;">Click</span><span style="font-size: small;">事件，并利用委托来调用求最值的方法。代码如下所示：</span></span></span></p>
<pre class="brush:csharp">private void btGetResult_Click(object sender, EventArgs e)
{
    if (this.md ==null )
    {
        MessageBox.Show("委托md没有指向任何方法！");
        return;
    }
    int a = int.Parse(this.tbxOP1 .Text );
    int b = int.Parse(this.tbxOP2 .Text );
    int c = this.md(a,b);
    this.tbxResult.Text = c.ToString();
}</pre>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">从上面的代码中，可以发现，在使用委托之前，先要判断其值是否为空，如果不为空，则可以进行调用，同时，使用者可以看到，在调用</span><span style="font-size: small;">md</span><span style="font-size: small;">时，我们并没有关心</span><span style="font-size: small;">md</span><span style="font-size: small;">到底指向了哪一个方法，总之，</span><span style="font-size: small;">md</span><span style="font-size: small;">不为空的时候，就一定会指向</span><span style="font-size: small;">Max</span><span style="font-size: small;">和</span><span style="font-size: small;">Min</span><span style="font-size: small;">当中的一个。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><strong><span style="font-size: small;">委托使用的注意事项</span></strong></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">在C＃中，所有的委托都是从System.MulticastDelegate类派生的。 </span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">委托隐含具有sealed属性，即不能用来派生新的类型。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">委托最大的作用就是为类的事件绑定事件处理程序。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">在通过委托调用函数前，必须先检查委托是否为空（null），若非空，才能调用函数。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">在委托实例中可以封装静态的方法也可以封装实例方法。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">在创建委托实例时，需要传递将要映射的方法或其他委托实例以指明委托将要封装的函数原型（.NET中称为方法签名：signature）。注意，如果映射的是静态方法，传递的参数应该是类名.方法名，如果映射的是实例方法，传递的参数应该是实例名.方法名。</span></span></span></p>
<p><span style="font-size: small;"><span style="font-size: small;"><span style="font-size: small;">Ø</span><span style="font-size: small;">   </span><span style="font-size: small;">只有当两个委托实例所映射的方法以及该方法所属的对象都相同时，才认为它们是想等的（从函数地址考虑）。</span></span></span></p>
<p>内容太多啦，就不贴那么多了。。。</p>
<p>原文地址： http://www.cnblogs.com/warensoft/archive/2010/03/19/1689806.html</p>
<h2  class="related_post_title">随机日志</h2><ul class="related_post"><li>2010/12/14 -- <a href="http://www.cxybase.com/201012/phone-gap-introduce/" title="phone gap 简介">phone gap 简介</a> (0)</li><li>2011/04/29 -- <a href="http://www.cxybase.com/201104/pppoe%e5%8d%8f%e8%ae%ae%e5%b7%a5%e4%bd%9c%e5%8e%9f%e7%90%86/" title="PPPoE协议工作原理">PPPoE协议工作原理</a> (0)</li><li>2011/04/07 -- <a href="http://www.cxybase.com/201104/visual-stdio-2010-%e7%ac%ac%e4%b8%89%e7%af%87%ef%bc%9a%e5%b9%b8%e7%a6%8f%e4%b9%9f%e8%a6%81%e6%95%8f%e6%8d%b7/" title="Visual Stdio 2010 第三篇：幸福也要敏捷">Visual Stdio 2010 第三篇：幸福也要敏捷</a> (0)</li><li>2011/03/01 -- <a href="http://www.cxybase.com/201103/android%e5%85%a5%e9%97%a8%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%96%99%e6%94%b6%e9%9b%86%e3%80%90%e8%bd%ac%e3%80%91/" title="Android入门学习资料收集【转】">Android入门学习资料收集【转】</a> (0)</li><li>2010/12/04 -- <a href="http://www.cxybase.com/201012/linux%e7%8e%af%e5%a2%83%e4%b8%8b%e7%9a%84cc%e5%9f%ba%e7%a1%80%e8%b0%83%e8%af%95%e6%8a%80%e6%9c%af1%e2%80%94%e2%80%94%e5%88%9d%e6%ad%a5%e4%ba%86%e8%a7%a3/" title="Linux环境下的C/C++基础调试技术1——初步了解">Linux环境下的C/C++基础调试技术1——初步了解</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/c%e5%a7%94%e6%89%98%e5%8f%8a%e4%ba%8b%e4%bb%b6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>宇宙的奥秘</title>
		<link>http://www.cxybase.com/201204/%e5%ae%87%e5%ae%99%e7%9a%84%e5%a5%a5%e7%a7%98/</link>
		<comments>http://www.cxybase.com/201204/%e5%ae%87%e5%ae%99%e7%9a%84%e5%a5%a5%e7%a7%98/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 05:22:06 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[叽歪生活]]></category>
		<category><![CDATA[宇宙]]></category>
		<category><![CDATA[宇宙的奥秘]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2206</guid>
		<description><![CDATA[<p> <span class='read-more'><a href='http://www.cxybase.com/201204/%e5%ae%87%e5%ae%99%e7%9a%84%e5%a5%a5%e7%a7%98/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p><center><br />
<object width="480" height="400" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://player.youku.com/player.php/sid/XMTczNDMyNzA4/v.swf" /><param name="allowfullscreen" value="true" /><param name="quality" value="high" /><param name="allowscriptaccess" value="always" /><embed width="550" height="450" type="application/x-shockwave-flash" src="http://player.youku.com/player.php/sid/XMTczNDMyNzA4/v.swf" allowfullscreen="true" quality="high" allowscriptaccess="always" /></object><br />
</center></p>
<h2  class="related_post_title">随机日志</h2><ul class="related_post"><li>2010/11/27 -- <a href="http://www.cxybase.com/201011/linux-ftp-mgets/" title="linux下FTP命令下载多个文件">linux下FTP命令下载多个文件</a> (1)</li><li>2011/04/06 -- <a href="http://www.cxybase.com/201104/android%e9%9d%a2%e8%af%95%e9%a2%98%e6%95%b4%e7%90%86%e3%80%90%e8%bd%ac%e3%80%91/" title="Android面试题">Android面试题</a> (0)</li><li>2011/06/15 -- <a href="http://www.cxybase.com/201106/%e8%85%be%e8%ae%afim%e6%8e%a8%e5%87%baq%e5%b9%b3%e5%8f%b0-%e4%b8%89%e5%a4%a7%e5%bc%80%e6%94%be%e5%b9%b3%e5%8f%b0%e5%b8%83%e5%b1%80%e5%ae%8c%e6%88%90/" title="腾讯IM推出Q+平台 三大开放平台布局完成">腾讯IM推出Q+平台 三大开放平台布局完成</a> (0)</li><li>2011/02/19 -- <a href="http://www.cxybase.com/201102/%e9%9d%9e%e5%9f%ba%e5%8b%bf%e6%89%b0/" title="牛人强悍剪辑《让子弹飞》神作《非基勿扰》">牛人强悍剪辑《让子弹飞》神作《非基勿扰》</a> (0)</li><li>2011/05/11 -- <a href="http://www.cxybase.com/201105/android%e7%9a%84%e6%9c%aa%e6%9d%a5%e5%ae%b6%e5%ba%ad%e8%87%aa%e5%8a%a8%e5%8c%96%e5%8f%91%e5%b1%95/" title="Android的未来家庭自动化发展">Android的未来家庭自动化发展</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.cxybase.com/201204/%e5%ae%87%e5%ae%99%e7%9a%84%e5%a5%a5%e7%a7%98/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#的类型安全</title>
		<link>http://www.cxybase.com/201204/c%e7%9a%84%e7%b1%bb%e5%9e%8b%e5%ae%89%e5%85%a8/</link>
		<comments>http://www.cxybase.com/201204/c%e7%9a%84%e7%b1%bb%e5%9e%8b%e5%ae%89%e5%85%a8/#comments</comments>
		<pubDate>Sun, 01 Apr 2012 02:27:29 +0000</pubDate>
		<dc:creator>萝卜根</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[c# type safe]]></category>
		<category><![CDATA[C#的类型安全]]></category>

		<guid isPermaLink="false">http://www.cxybase.com/?p=2198</guid>
		<description><![CDATA[<p><strong>一、什么是类型安全（Type Safe）</strong></p><p>“静态类型安全”，在编译时检查类型是否一致。</p><p>“动态类型安全”，.net4.0里的dynamic类型，在运行时，会提前获得dynamic类型实际的类型。</p><p>对任何引用的对象进行操作，CLR都知道此对象是何种类型的； CLR在对一个引用对象进行操作时 <span class='read-more'><a href='http://www.cxybase.com/201204/c%e7%9a%84%e7%b1%bb%e5%9e%8b%e5%ae%89%e5%85%a8/'>[更多…]</a></span></p><div class="clear-float"></div>
		]]></description>
			<content:encoded><![CDATA[<p><strong>一、什么是类型安全（Type Safe）</strong></p>
<p>“静态类型安全”，在编译时检查类型是否一致。</p>
<p>“动态类型安全”，.net4.0里的dynamic类型，在运行时，会提前获得dynamic类型实际的类型。</p>
<p>对任何引用的对象进行操作，CLR都知道此对象是何种类型的； CLR在对一个引用对象进行操作时，它先会检查此引用的类型， 查看本次操作是否有效；若无效，编译将不能通过。</p>
<p>从另一个方面来说，   “C#是类型安全的”指的是：</p>
<p>CLR在程序被编译时就会检查所有对象的有效性，而不是在运行时再检查所有对象的有效性，这样就会避免了在运行时 出现大的错误。</p>
<p>类型安全近似于所谓的内存安全（就是限制从内存的某处，将任意的字节合复制到另一处的能力）。<br />
CLR一个很重要的特性就是Type安全。CLR在任何时候都知道某个对象的类型。你可以通过GetType方法获得，由于该方法不是虚函数，因此不能被覆盖，也就是说返回的值是永远真实的。</p>
<p>类型安全可以静态方式实施，及早在编译时期就捕捉到潜藏的错误；或者以动态方式，在执行时期关联类型的资讯，并在必要时检测即将发生的错误。类型安 全是编程语言的性质，而不是程式所自有的。例如，有可能以类型不安全的语言，编写出类型安全的程式。在此是以编程语言为主，而不讨论以个人能力维护的类型 安全。</p>
<p>在C#中，被传递的引用参数是类型安全的。</p>
<p><strong>二、类型安全</strong></p>
<p>1. C#中如果一个方法的参数类型是string，那么，如果你传入了一个int，就会抛出异常。这就是类型安全。VB.net中，编译器自动把int转换为string,然后传递给方法。这就是非类型安全。</p>
<p>2. 类型安全主要包括两个部分，强类型检查和运行期的类型数据。强类型检查就像第一条所说的，C#的编译器决不会允许int   i   =   &#8220;1 &#8220;;这样的语句通过编译。同时，这样的语句也不会通过：</p>
<pre class="brush:csharp">string s= " ";
int i = s;</pre>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<pre class="brush:csharp">int FunctionA(string s)
string s = FunctionA(" ");//Error
int i = FunctionA(FunctionA(" "));//Error</pre>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<pre class="brush:csharp">long l = 17382;
int i = l;//Error</pre>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
运行期的类型数据则主要是is、as运算符、强制类型转换和GetType方法。</p>
<p>3. 类型安全要求可以相互转换的不同类型数据在转换时 显式转换</p>
<p>4. 类型安全代码只访问被授权可以访问的内存位置。（在本讨论中，类型安全特指内存类型安全，不应与范围更广的类型安全混淆。）例如，类型安全代码不能从其他对象的私有字段读取值。它只以定义完善的允许方式访问类型。<br />
在 实时   (JIT)   编译期间，可选的验证过程检查要实时编译为本机代码的方法的元数据和   Microsoft   中间语言   (MSIL)，以验证它们是否为类型安全。如果代码具有忽略验证的权
