<?xml version="1.0" encoding="gb2312"?>
<rss version="2.0">
<channel>
<title><![CDATA[办法总比困难多]]></title>
        <image>
        <title>http://hi.baidu.com</title>
        <link>http://hi.baidu.com</link>
        <url>http://img.baidu.com/img/logo-hi.gif</url>
        </image>
<description><![CDATA[程序人生,美妙生活]]></description>
<link>http://hi.baidu.com/senty</link>
<language>zh-cn</language>
<generator>www.baidu.com</generator>
<ttl>5</ttl>


<item>
        <title><![CDATA[C#中声明类/属性/方法已过时--(Obsolete)]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/494a3901f86bde047aec2c66.html]]></link>
        <description><![CDATA[
		
		<p><tt><font face="新宋体"><strong>Obsolete</strong></font></tt> 属性将某个程序实体标记为一个建议不再使用的实体。每次使用被标记为已过时的实体时，随后将生成警告或错误，这取决于属性是如何配置的。例如：</p>
<span >
<div class="libCScode" >
<div class="CodeSnippetTitleBar">
<div class="CodeDisplay"> </div>
</div>
<div dir="ltr" style="background-color: rgb(221,221,221)">
<pre class="libCScode" space="preserve">[System.Obsolete(&quot;use class B&quot;)]
class A
{
    public void Method() { }
}
class B
{
    [System.Obsolete(&quot;use NewMethod&quot;, true)]
    public void OldMethod()  { }
    public void NewMethod()  { }
}</pre>
</div>
</div>
</span>
<p>在此例中，<tt><font face="新宋体"><strong>Obsolete</strong></font></tt> 属性应用于类 <tt><font face="新宋体">A</font></tt> 和方法 <tt><font face="新宋体">B.OldMethod</font></tt>。由于应用于 <tt><font face="新宋体">B.OldMethod</font></tt> 的属性构造函数的第二个参数设置为 <strong>true</strong>，因此使用此方法将导致编译器错误，而使用类 <tt><font face="新宋体">A</font></tt> 只会产生警告。但是，调用 <tt><font face="新宋体">B.NewMethod</font></tt> 既不产生警告也不产生错误。</p>
<p>向属性构造函数提供的作为第一个参数的字符串将显示为警告或错误的一部分。例如，当与前面的定义一起使用时，下面的代码将生成两个警告和一个错误：</p>
<span >
<div class="libCScode" >
<div class="CodeSnippetTitleBar">
<div class="CodeDisplay"> </div>
</div>
<div dir="ltr" style="background-color: rgb(221,221,221)">
<pre class="libCScode" space="preserve">// Generates 2 warnings:
A a = new A();
// Generate no errors or warnings:
B b = new B();
b.NewMethod();
// Generates an error, terminating compilation:
b.OldMethod();</pre>
</div>
</div>
</span>
<p>为类 <tt><font face="新宋体">A</font></tt> 产生两个警告：一个用于声明类引用，一个用于类构造函数。</p> <a href="http://hi.baidu.com/senty/blog/item/494a3901f86bde047aec2c66.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/494a3901f86bde047aec2c66.html#comment">查看评论</a>]]></description>
        <pubDate>2009-10-20  12:08</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/494a3901f86bde047aec2c66.html</guid>
</item>

<item>
        <title><![CDATA[javascript:instanceof的妙用]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/e92887d6c9a7742606088be2.html]]></link>
        <description><![CDATA[
		
		在javascript中，我们判断一个变量的类型时，经常会使用typeof，但它只能返回一些原始的类型：string,number,boolean等，当判断的对象是引用类型时，无论是引用的什么类型，统统会返回object，这样我们无法知道该对象的具体引用类型，这时instanceof就很有用了，如下面代码：<br>
<br>
&lt;script&gt;<br>
var t = new Array();<br>
var a = [];<br>
alert(t instanceof Array);&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;//output:true<br>
alert(a instanceof Array);&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;//output:true<br>
alert(typeof document);&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;&#160;&#160; &#160;//output:object<br>
alert(document instanceof Object);&#160;&#160; &#160;//output:true<br>
&lt;/script&gt; <a href="http://hi.baidu.com/senty/blog/item/e92887d6c9a7742606088be2.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/e92887d6c9a7742606088be2.html#comment">查看评论</a>]]></description>
        <pubDate>2009-09-02  17:12</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/e92887d6c9a7742606088be2.html</guid>
</item>

<item>
        <title><![CDATA[使用Serializable轻松实现类实例的序列化]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/a1f3352a4e7f8a315343c1b4.html]]></link>
        <description><![CDATA[
		
		<p>在工作流平台设计时，需要使用到类实例的序列化，在C＃中可以很容易的实现。</p>
<p><br>
<strong>一、首先引用几个命名空间</strong><br>
<br>
<font color="#0b5394">using System.IO;<br>
using System.Runtime.Serialization;<br>
using System.Runtime.Serialization.Formatters.Binary;</font></p>
<p><strong><font color="#000000">二、定义可序列化的类</font></strong></p>
<p><font color="#0b5394">[Serializable]<br>
public class Student<br>
{<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;  private string name;<br>
&nbsp;&nbsp;&nbsp;  private string id;<br>
&nbsp;&nbsp;&nbsp;  private string studentNum;<br>
&nbsp;&nbsp;&nbsp;  private ArrayList scores;</font></p>
<p><font color="#0b5394">&nbsp;&nbsp;  /// &lt;summary&gt;<br>
&nbsp;&nbsp;  /// 姓名<br>
&nbsp;&nbsp;  /// &lt;/summary&gt;<br>
&nbsp;&nbsp;  public string Name<br>
&nbsp;&nbsp;  {<br>
&nbsp;&nbsp;&nbsp;  get { return name; }<br>
&nbsp;&nbsp;&nbsp;  set { name = value; }<br>
&nbsp;&nbsp;  }</font></p>
<p><font color="#0b5394">&nbsp;&nbsp;  /// &lt;summary&gt;<br>
&nbsp;&nbsp;  /// 编号<br>
&nbsp;&nbsp;  /// &lt;/summary&gt;<br>
&nbsp;&nbsp;  public string Id<br>
&nbsp;&nbsp;  {<br>
&nbsp;&nbsp;&nbsp;  get { return id; }<br>
&nbsp;&nbsp;&nbsp;  set { id = value; }<br>
&nbsp;&nbsp;  }</font></p>
<p><font color="#0b5394">&nbsp;&nbsp;  /// &lt;summary&gt;<br>
&nbsp;&nbsp;  /// 学号<br>
&nbsp;&nbsp;  /// &lt;/summary&gt;<br>
&nbsp;&nbsp;  public string StudentNum<br>
&nbsp;&nbsp;  {<br>
&nbsp;&nbsp;&nbsp;  get { return studentNum; }<br>
&nbsp;&nbsp;&nbsp;  set { studentNum = value; }<br>
&nbsp;&nbsp;  }</font></p>
<p><font color="#0b5394">&nbsp;&nbsp;  /// &lt;summary&gt;<br>
&nbsp;&nbsp;  /// 成绩表<br>
&nbsp;&nbsp;  /// &lt;/summary&gt;<br>
&nbsp;&nbsp;  public ArrayList Scores<br>
&nbsp;&nbsp;  {<br>
&nbsp;&nbsp;&nbsp;  get { return scores; }<br>
&nbsp;&nbsp;&nbsp;  set { scores = value; }<br>
&nbsp;&nbsp;  }</font></p>
<p><font color="#0b5394">}<br>
</font></p>
<p> </p>
<p><strong>三、序列化到硬盘文件中</strong></p>
<p><font color="#0b5394">&nbsp;&nbsp;&nbsp;  Student student = new Student();<br>
&nbsp;&nbsp;&nbsp;  student.Id = &quot;002&quot;;<br>
&nbsp;&nbsp;&nbsp;  student.Name = &quot;senty&quot;;<br>
&nbsp;&nbsp;&nbsp;  student.StudentNum = &quot;090325&quot;;<br>
&nbsp;&nbsp;&nbsp;  ArrayList scores = new ArrayList();<br>
&nbsp;&nbsp;&nbsp;  scores.Add(90);<br>
&nbsp;&nbsp;&nbsp;  scores.Add(67);<br>
&nbsp;&nbsp;&nbsp;  scores.Add(89);<br>
&nbsp;&nbsp;&nbsp;  scores.Add(73);<br>
&nbsp;&nbsp;&nbsp;  student.Scores = scores;</font></p>
<p><font color="#0b5394">&nbsp;&nbsp;&nbsp;  #region 序列化对象<br>
&nbsp;&nbsp;&nbsp;  IFormatter formatter = new BinaryFormatter();<br>
&nbsp;&nbsp;&nbsp;  Stream stream = new FileStream(&quot;f:/student.dat&quot;, FileMode.Create, FileAccess.Write, FileShare.None);<br>
&nbsp;&nbsp;&nbsp;  formatter.Serialize(stream, student);<br>
&nbsp;&nbsp;&nbsp;  stream.Close();<br>
&nbsp;&nbsp;&nbsp;  #endregion<br>
</font></p>
<p><strong><font color="#000000">四、从文件中反序列类实例</font></strong></p>
<p><font color="#0b5394">&nbsp;&nbsp;&nbsp;  #region 反序列化<br>
&nbsp;&nbsp;&nbsp;  IFormatter formatter = new BinaryFormatter();<br>
&nbsp;&nbsp;&nbsp;  Stream stream = new FileStream(&quot;f:/student.dat&quot;, FileMode.Open, FileAccess.Read, FileShare.Read);<br>
&nbsp;&nbsp;&nbsp;  Student student = formatter.Deserialize(stream) as Student;<br>
&nbsp;&nbsp;&nbsp;  this.label1.Text = &quot;反序列化完成：&quot;;<br>
&nbsp;&nbsp;&nbsp;  this.label1.Text += &quot;\r\n编号：&quot; + student.Id;<br>
&nbsp;&nbsp;&nbsp;  this.label1.Text += &quot;\r\n姓名：&quot; + student.Name;<br>
&nbsp;&nbsp;&nbsp;  this.label1.Text += &quot;\r\n学号：&quot; + student.StudentNum;<br>
&nbsp;&nbsp;&nbsp;  this.label1.Text += &quot;\r\n成绩：&quot; + student.Scores[0] + &quot;,&quot; + student.Scores[1] + &quot;,&quot; + student.Scores[2] + &quot;,&quot; + student.Scores[3] + &quot;,&quot;;<br>
&nbsp;&nbsp;&nbsp;  #endregion<br>
</font></p>
<p> </p>
<p> </p>
<p>除了可以序列化类实例到文件中外，还可以保存到数据库中，至于如何保存Stream到数据库中，不在本文关注的范围，有兴趣可以自己百度一下。</p>
<p> </p>
<p> </p> <a href="http://hi.baidu.com/senty/blog/item/a1f3352a4e7f8a315343c1b4.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/a1f3352a4e7f8a315343c1b4.html#comment">查看评论</a>]]></description>
        <pubDate>2009-08-06  10:16</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/a1f3352a4e7f8a315343c1b4.html</guid>
</item>

<item>
        <title><![CDATA[.NET2.0使用SMTP发邮件的方法]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/f6aa6b63368b22690d33fa38.html]]></link>
        <description><![CDATA[
		
		<p> </p>
<p><font size="+0">System.Net.Mail是.NET2.0新增的组件，利用它可以很方便实现邮件的收发，下面是通过SMTP发送邮件的方法</font></p>
<p><strong>//先引用命名空间</strong><br>
<font style="background-color: #c0c0c0">using System.Net.Mail;</font></p>
<p><strong>//方法一：纯代码的方法</strong><br>
<font style="background-color: #c0c0c0">MailMessage message = new MailMessage();<br>
message.From = new MailAddress(&quot;</font><a href="mailto:fname@gmail.com"><font style="background-color: #c0c0c0">name@gmail.com</font></a><font style="background-color: #c0c0c0">&quot;, &quot;盛天&quot;);<br>
message.To.Add(new MailAddress(&quot;</font><a href="mailto:nto@qq.com"><font style="background-color: #c0c0c0">to@qq.com</font></a><font style="background-color: #c0c0c0">&quot;)); // the email you want to send email to<br>
message.Subject = &quot;A test email&quot; ;</font></p>
<p><font style="background-color: #c0c0c0">message.IsBodyHtml = true;<br>
message.BodyEncoding = System.Text.Encoding.UTF8;<br>
message.Body = &quot;this is just a simple test!&lt;br&gt; Jack&quot;;<br>
message.Priority = MailPriority.Normal; </font></p>
<p><font style="background-color: #c0c0c0">SmtpClient client = new SmtpClient(&quot;smtp.gmail.com&quot;, 587); // 587是Gmail使用的端口<br>
client.Credentials = new System.Net.NetworkCredential(&quot;</font><a href="mailto:tname@gmail.com"><font style="background-color: #c0c0c0">name@gmail.com</font></a><font style="background-color: #c0c0c0">&quot;, &quot;123456&quot;); // Your user name &amp; password<br>
client.EnableSsl = true; //经过ssl加密 <br>
object userState = message; <br>
try<br>
{<br>
&nbsp;&nbsp;&nbsp;  client.Send(message);<br>
&nbsp;&nbsp;&nbsp;  MessageBox.Show(&quot;邮件成功发送到&quot; + message.To.ToString());<br>
}<br>
catch (Exception exp)<br>
{<br>
&nbsp;&nbsp;&nbsp;  MessageBox.Show(exp.Message + &quot;\n&quot; + exp.InnerException.Message);<br>
} </font></p>
<p><font color="#999999">////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////<br>
</font><strong><font size="+0">//方法二：通过配置文件</font></strong></p>
<p><font style="background-color: #c0c0c0">MailMessage message = new MailMessage();<br>
message.To.Add(new MailAddress(&quot;<a href="mailto:senty2008@qq.com">to@qq.com</a>&quot;)); // the email you want to send email to<br>
message.Subject = &quot;A test email&quot;;</font></p>
<p><font style="background-color: #c0c0c0">message.IsBodyHtml = true;<br>
message.BodyEncoding = System.Text.Encoding.UTF8;<br>
message.Body = &quot;this is just a simple test!&lt;br&gt; Jack&quot;;<br>
message.Priority = MailPriority.Normal;</font></p>
<p><font style="background-color: #c0c0c0">SmtpClient client = new SmtpClient();<br>
client.EnableSsl = true; //经过ssl加密 <br>
try<br>
{<br>
&nbsp;&nbsp;&nbsp;  client.Send(message);<br>
&nbsp;&nbsp;&nbsp;  MessageBox.Show(&quot;邮件成功发送到&quot; + message.To.ToString());<br>
}<br>
catch (Exception exp)<br>
{<br>
&nbsp;&nbsp;&nbsp;  MessageBox.Show(exp.Message + &quot;\n&quot; + exp.InnerException.Message);<br>
}<br>
<br>
</font></p>
<p><strong>//配置文件</strong><br>
<font style="background-color: #c0c0c0">&lt;system.net&gt;<br>
&nbsp;&nbsp;&nbsp;  &lt;mailSettings&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &lt;smtp from=&quot;</font><a href="mailto:from@gmail.com"><font style="background-color: #c0c0c0">from@gmail.com</font></a><font style="background-color: #c0c0c0">&quot;&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &lt;network host=&quot;smtp.gmail.com&quot; port=&quot;587&quot; userName=&quot;</font><a href="mailto:tname@gmail.com"><font style="background-color: #c0c0c0">name@gmail.com</font></a><font style="background-color: #c0c0c0">&quot; password=&quot;123456&quot; /&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &lt;!-- if has 'defaultCredentials=&quot;true&quot;' , using Gmail can not send success --&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  &lt;/smtp&gt;<br>
&nbsp;&nbsp;&nbsp;  &lt;/mailSettings&gt;<br>
&lt;/system.net&gt;</font></p>
<p> </p> <a href="http://hi.baidu.com/senty/blog/item/f6aa6b63368b22690d33fa38.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/f6aa6b63368b22690d33fa38.html#comment">查看评论</a>]]></description>
        <pubDate>2009-07-10  10:09</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/f6aa6b63368b22690d33fa38.html</guid>
</item>

<item>
        <title><![CDATA[SQLServer索引不能不说的痛]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/d081720e456a6ec27acbe121.html]]></link>
        <description><![CDATA[
		
		<p>这里有一个用户表(Users)，建了几个索引，如图1，我在OrganId字段上建了一个索引<strong>Index_User_2</strong></p>
<p><img class="blogimg" border="0" small="0" src="http://hiphotos.baidu.com/senty/pic/item/2faac93d444b76e39e3d622f.jpg"><br>
(图1)</p>
<p>现在来看看下面的执行计划：</p>
<p><img class="blogimg" border="0" small="0" src="http://hiphotos.baidu.com/senty/pic/item/b559af51f548f9ab8d543039.jpg"><br>
(图2)</p>
<p>从图中可以看出来，<strong>根本没应用到Index_User_2索引</strong>，而是应用主键索引，开始对SQLServer摇头了；不着急，更严重的问题还在后面，再来看看下面的执行计划：</p>
<p><img class="blogimg" border="0" small="0" src="http://hiphotos.baidu.com/senty/pic/item/b5bc4134bda27d6a241f140b.jpg"><br>
(图3)</p>
<p>没有什么比这更糟糕了，<strong>竟然也没使用到Index_User_2索引</strong>，这是怎么回事？难道SQLServer的优化程序是白痴吗？看了下来的执行计划，我终于明白了－－<br>
<img class="blogimg" border="0" small="0" src="http://hiphotos.baidu.com/senty/pic/item/c4f794eefaf7770d2cf534cb.jpg"></p>
<p>这回终于使用上索引了，SQLServer果然非常&ldquo;高明&rdquo;，相当地佩服！！！</p> <a href="http://hi.baidu.com/senty/blog/item/d081720e456a6ec27acbe121.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/d081720e456a6ec27acbe121.html#comment">查看评论</a>]]></description>
        <pubDate>2009-05-16  16:06</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/d081720e456a6ec27acbe121.html</guid>
</item>

<item>
        <title><![CDATA[C#定义事件的三种方式]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/a367b745d895a72ecffca38c.html]]></link>
        <description><![CDATA[
		
		定义一个事件,可以有三种方式吧: <br>
1: <br>
public   event   EventHandler   SomeEvent; <br>
<br>
2: <br>
public   event   EventHandler &lt;EventArgs&gt;   SomeEvent; <br>
<br>
3: <br>
public   delegate   void   EventDelegate(EventArgs); <br>
public   event   EventDelegate   SomeEvent; 
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/a367b745d895a72ecffca38c.html#comment">查看评论</a>]]></description>
        <pubDate>2009-03-15  14:35</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/a367b745d895a72ecffca38c.html</guid>
</item>

<item>
        <title><![CDATA[使用sp_executesql输出参数]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/afb290ef59c5b9e8ce1b3ec9.html]]></link>
        <description><![CDATA[
		
		<p><br>
DECLARE @SQL NVARCHAR(MAX)<br>
DECLARE @SumValue INT<br>
SET @SQL = N'SELECT @SumValue = Sum(Money) FROM CONTRACT'<br>
execute sp_executesql @SQL,N'@SumValue INT OUTPUT',@SumValue OUTPUT<br>
print @v_SumValue</p> 
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/afb290ef59c5b9e8ce1b3ec9.html#comment">查看评论</a>]]></description>
        <pubDate>2008-11-12  11:29</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/afb290ef59c5b9e8ce1b3ec9.html</guid>
</item>

<item>
        <title><![CDATA[SQL Server中动态执行SQL语句]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/5d6d36d3dc89a5073bf3cf3e.html]]></link>
        <description><![CDATA[
		
		<h3><a name="_using_sp_executesql"></a>使用 <font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></h3>
<p>建议使用 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 而不要使用 EXECUTE 语句执行字符串。支持参数替换不仅使 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> </strong>比 EXECUTE 更通用，而且还使 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 更有效，因为它生成的执行计划更有可能被 SQL Server 重新使用。</p>
<h5>自包含批处理</h5>
<p><strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 或 EXECUTE 语句执行字符串时，字符串被作为其自包含批处理执行。SQL Server 将Transact-SQL 语句或字符串中的语句编译进一个执行计划，该执行计划独立于包含 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 或 EXECUTE 语句的批处理的执行计划。下列规则适用于自含的批处理：</p>
<ul type="disc">
    <li>直到执行 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 或EXECUTE 语句时才将<strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> </strong>或 EXECUTE 字符串中的 Transact-SQL 语句编译进执行计划。执行字符串时才开始分析或检查其错误。执行时才对字符串中引用的名称进行解析。</li>
    <li>执行的字符串中的 Transact-SQL 语句，不能访问 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 或 EXECUTE 语句所在批处理中声明的任何变量。包含 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 或 EXECUTE 语句的批处理不能访问执行的字符串中定义的变量或局部游标。</li>
    <li>如果执行字符串有更改数据库上下文的 USE 语句，则对数据库上下文的更改仅持续到 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 或 EXECUTE 语句完成。</li>
</ul>
<p>通过执行下列两个批处理来举例说明：</p>
<pre><code>/* Show not having access to variables from the calling batch. */
DECLARE @CharVariable CHAR(3)
SET @CharVariable = 'abc'
/* <font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> fails because @CharVariable has gone out of scope. */
<font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> N'PRINT @CharVariable'
GO

/* Show database context resetting after <font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> completes. */
USE pubs
GO
<font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> N'USE Northwind'
GO
/* This statement fails because the database context
   has now returned to pubs. */
SELECT * FROM Shippers
GO
</code></pre>
<h5>替换参数值</h5>
<p><strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 支持对 Transact-SQL 字符串中指定的任何参数的参数值进行替换，但是 EXECUTE 语句不支持。因此，由 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 生成的 Transact-SQL 字符串比由 EXECUTE 语句所生成的更相似。SQL Server 查询优化器可能将来自 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 的 Transact-SQL 语句与以前所执行的语句的执行计划相匹配，以节约编译新的执行计划的开销。</p>
<p>使用 EXECUTE 语句时，必须将所有参数值转换为字符或 Unicode 并使其成为 Transact-SQL 字符串的一部分：</p>
<pre><code>DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
/* Build and execute a string with one parameter value. */
SET @IntVariable = 35
SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
                 CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)
/* Build and execute a string with a second parameter value. */
SET @IntVariable = 201
SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' +
                 CAST(@IntVariable AS NVARCHAR(10))
EXEC(@SQLString)
</code></pre>
<p>如果语句重复执行，则即使仅有的区别是为参数所提供的值不同，每次执行时也必须生成全新的 Transact-SQL 字符串。从而在下面几个方面产生额外的开销：</p>
<ul type="disc">
    <li>SQL Server 查询优化器具有将新的 Transact-SQL 字符串与现有的执行计划匹配的能力，此能力被字符串文本中不断更改的参数值妨碍，特别是在复杂的 Transact-SQL 语句中。</li>
    <li>每次执行时均必须重新生成整个字符串。</li>
    <li>每次执行时必须将参数值（不是字符或 Unicode 值）投影到字符或 Unicode 格式。</li>
</ul>
<p><strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 支持与 Transact-SQL 字符串相独立的参数值的设置：</p>
<pre><code>DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)

/* Build the SQL string once. */
SET @SQLString =
     N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
/* Specify the parameter format once. */
SET @ParmDefinition = N'@level tinyint'

/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE <font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> @SQLString, @ParmDefinition,
                      @level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE <font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> @SQLString, @ParmDefinition,
                      @level = @IntVariable
</code></pre>
<p>此 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 示例完成的任务与前面的 EXECUTE 示例所完成的相同，但有下列额外优点：</p>
<ul type="disc">
    <li>因为 Transact-SQL 语句的实际文本在两次执行之间未改变，所以查询优化器应该能将第二次执行中的 Transact-SQL 语句与第一次执行时生成的执行计划匹配。这样，SQL Server 不必编译第二条语句。</li>
    <li>Transact-SQL 字符串只生成一次。</li>
    <li>整型参数按其本身格式指定。不需要转换为 Unicode。</li>
</ul>
<p class="indent"></p>
<img style="margin-left: 0em" height="11" src="mk:@MSITStore:C: Program%20Files Microsoft%20SQL%20Server 80 Tools Books acdata.chm::/Basics/note.gif" width="12" border="0">
<p style="margin-top: -1.1em; margin-left: 1.5em"><strong>说明</strong>&nbsp;&nbsp; 为了使 SQL Server 重新使用执行计划，语句字符串中的对象名称必须完全符合要求。</p>

<p> </p>
<h5>重新使用执行计划</h5>
<p>在 SQL Server 早期的版本中要重新使用执行计划的唯一方式是，将 Transact-SQL 语句定义为存储过程然后使应用程序执行此存储过程。这就产生了管理应用程序的额外开销。使用 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 有助于减少此开销，并使 SQL Server 得以重新使用执行计划。当要多次执行某个 Transact-SQL 语句，且唯一的变化是提供给该 Transact-SQL 语句的参数值时，可以使用 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 来代替存储过程。因为 Transact-SQL 语句本身保持不变仅参数值变化，所以 SQL Server 查询优化器可能重复使用首次执行时所生成的执行计划。</p>
<p>下例为服务器上除四个系统数据库之外的每个数据库生成并执行 DBCC CHECKDB 语句：</p>
<pre><code>USE master
GO
SET NOCOUNT ON
GO
DECLARE AllDatabases CURSOR FOR
SELECT name FROM sysdatabases WHERE dbid &gt; 4

OPEN AllDatabases

DECLARE @DBNameVar NVARCHAR(128)
DECLARE @Statement NVARCHAR(300)

FETCH NEXT FROM AllDatabases INTO @DBNameVar
WHILE (@@FETCH_STATUS = 0)
BEGIN
   PRINT N'CHECKING DATABASE ' + @DBNameVar
   SET @Statement = N'USE ' + @DBNameVar + CHAR(13)
      + N'DBCC CHECKDB (' + @DBNameVar + N')'
</code>   <code>EXEC <font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> @Statement</code>
<code>   PRINT CHAR(13) + CHAR(13)</code>
<code>   FETCH NEXT FROM AllDatabases INTO @DBNameVar</code>
<code>END</code>

<code>CLOSE AllDatabases</code>
<code>DEALLOCATE AllDatabases</code>
<code>GO</code>
<code>SET NOCOUNT OFF</code>
<code>GO</code></pre>
<p>当目前所执行的 Transact-SQL 语句包含绑定参数标记时，SQL Server ODBC 驱动程序使用 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 完成 <strong>SQLExecDirect</strong>。但例外情况是 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 不用于执行中的数据参数。这使得使用标准 ODBC 函数或使用在 ODBC 上定义的 API（如 RDO）的应用程序得以利用 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font> </strong>所提供的优势。定位于 SQL Server 2000 的现有的 ODBC 应用程序不需要重写就可以自动获得性能增益。有关更多信息，请参见<a href=" :hhobj_1.Click()">使用语句参数</a>。</p>
<p>用于 SQL Server 的 Microsoft OLE DB 提供程序也使用 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 直接执行带有绑定参数的语句。使用 OLE DB 或 ADO 的应用程序不必重写就可以获得 <strong><font style="background-color: #0a246a" color="#ffffff">sp_executesql</font></strong> 所提供的优势。</p>
 <a href="http://hi.baidu.com/senty/blog/item/5d6d36d3dc89a5073bf3cf3e.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/5d6d36d3dc89a5073bf3cf3e.html#comment">查看评论</a>]]></description>
        <pubDate>2008-11-07  17:42</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/5d6d36d3dc89a5073bf3cf3e.html</guid>
</item>

<item>
        <title><![CDATA[SQL server2005中插入汉字变成问号的解决办法]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/0a04d933775b3c45ac4b5f1c.html]]></link>
        <description><![CDATA[
		
		关于SQL server2005中插入汉字变成问号的解决办法，经过网友的提示与自己的摸索，总结如下： <br>
<br>
1、右击你所建的数据库，点击&ldquo;属性&rdquo;，选择&ldquo;选项&rdquo;并点击。将其中的排序规则设置为：Chinese_PRC_CI_AS(选择下拉列表框即可设置)，然后&ldquo;确定&ldquo;。 <br>
<br>
2、在表中将你的汉字字段的数据类型设置为nvarchar型或ntext型，设置前最好先将表中的数据全部删除。（不要使用varchar或text型，因为它们只会继续使你的数据乱码）。 <br>
<br>
3、设置好后，关闭服务器，再重新启动，就可生效了。 <a href="http://hi.baidu.com/senty/blog/item/0a04d933775b3c45ac4b5f1c.html">阅读全文</a>
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B3%CC%D0%F2%C8%CB%C9%FA">程序人生</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/0a04d933775b3c45ac4b5f1c.html#comment">查看评论</a>]]></description>
        <pubDate>2008-10-21  11:50</pubDate>
        <category><![CDATA[程序人生]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/0a04d933775b3c45ac4b5f1c.html</guid>
</item>

<item>
        <title><![CDATA[百度推出C2C“有啊”，首体验]]></title>
        <link><![CDATA[http://hi.baidu.com/senty/blog/item/85b2728bdbac5414c9fc7a02.html]]></link>
        <description><![CDATA[
		
		<a href="http://youa.baidu.com/">http://youa.baidu.com</a>　 
		
		<br/><b>类别：</b><a href="http://hi.baidu.com/senty/blog/category/%B1%A6%B1%B4%B5%EA%C6%CC">宝贝店铺</a>&nbsp;<a href="http://hi.baidu.com/senty/blog/item/85b2728bdbac5414c9fc7a02.html#comment">查看评论</a>]]></description>
        <pubDate>2008-10-21  11:11</pubDate>
        <category><![CDATA[宝贝店铺]]></category>
        <author><![CDATA[盛天]]></author>
		<guid>http://hi.baidu.com/senty/blog/item/85b2728bdbac5414c9fc7a02.html</guid>
</item>


</channel>
</rss>