<?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>Louis Han Life Log &#187; 学习心得</title>
	<atom:link href="http://louishan.com/articles/category/study/feed" rel="self" type="application/rss+xml" />
	<link>http://louishan.com</link>
	<description>Life will find its way out!</description>
	<lastBuildDate>Tue, 22 May 2012 00:00:00 +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>解决ASP.NET替换WORD时，字符串参量过长</title>
		<link>http://louishan.com/articles/word-string-parameter-too-long-solutions.html</link>
		<comments>http://louishan.com/articles/word-string-parameter-too-long-solutions.html#comments</comments>
		<pubDate>Tue, 22 May 2012 00:00:00 +0000</pubDate>
		<dc:creator>Louis Han</dc:creator>
				<category><![CDATA[学习心得]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[word]]></category>
		<category><![CDATA[字符串参量过长]]></category>

		<guid isPermaLink="false">http://louishan.com/?p=31653</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160;&#160;&#160; 项目中需要最后能够到处一份 WORD 版本的报告，设计好模板并且在需要自动替换的地方设置标记是最简单实用的方法了。不过在测试的时候遇到一个问题，就是要替换的字符串超过一定的长度，就会提示“字符串参量过长”。

&#160;&#160;&#160;&#160;&#160;&#160;&#160; 搜索了一下，发现是因为 Word 空间替换中的字符限制造成的，不过有说最大长度255字符，也有说只能支持大约230字符左右。为了突破这个限制，我将须要替换的大段文字进行截断，采取逐步替换的方法实现。

<span class="readmore"><a href="http://louishan.com/articles/word-string-parameter-too-long-solutions.html" title="解决ASP.NET替换WORD时，字符串参量过长">阅读全文——共1777字</a></span>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 项目中需要最后能够到处一份 WORD 版本的报告，设计好模板并且在需要自动替换的地方设置标记是最简单实用的方法了。不过在测试的时候遇到一个问题，就是要替换的字符串超过一定的长度，就会提示“<strong>字符串参量过长</strong>”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 搜索了一下，发现是因为 Word 空间替换中的字符限制造成的，不过有说最大长度255字符，也有说只能支持大约230字符左右。为了突破这个限制，我将须要替换的大段文字进行截断，采取逐步替换的方法实现。</p>
<p>下面是替换的算法：</p>
<pre></pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;summary&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// 查找替换Word内容并进行替换</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// 用于文档中大段文字替换</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// 解决WORD文字写入时255个字符限制</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;/summary&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;param name=&quot;wordApp&quot;&gt;WORD文档&lt;/param&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;param name=&quot;findText&quot;&gt;查找的标记&lt;/param&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;param name=&quot;replaceText&quot;&gt;替换的文字&lt;/param&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#0000FF">public</span> <span style="color:#0000FF">void</span> FindAndReplaceLong(Word.Application wordApp, <span style="color:#0000FF">object</span> findText, <span style="color:#0000FF">object</span> replaceText)
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">int</span> len = replaceText.ToString().Length;   <span style="color:#008000">//要替换的文字长度</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">int</span> cnt= len / 220;                     <span style="color:#008000">//不超过220个字</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">string</span> newstr;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> newStrs;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">if</span> (len &lt; 220)           <span style="color:#008000">//小于220字直接替换</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">            FindAndReplace(wordApp, findText, replaceText);
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        }
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">else</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">            <span style="color:#0000FF">for</span> (<span style="color:#0000FF">int</span> i = 0; i &lt;= cnt; i++)
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">            {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                <span style="color:#0000FF">if</span> (i != cnt)
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                    newstr = replaceText.ToString().Substring(i * 220, 220) + findText;  <span style="color:#008000">//新的替换字符串</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                }
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                <span style="color:#0000FF">else</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                    newstr = replaceText.ToString().Substring(i * 220, len - i * 220);    <span style="color:#008000">//最后一段需要替换的文字</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                }
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                newStrs = (<span style="color:#0000FF">object</span>)newstr;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">                FindAndReplace(wordApp, findText, newStrs);                              <span style="color:#008000">//进行替换</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">            }
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        }
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    }
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;summary&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// 查找替换Word内容</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// 主要用于文档中多个标记的逐一替换</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;/summary&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;param name=&quot;wordApp&quot;&gt;WORD文档&lt;/param&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;param name=&quot;findText&quot;&gt;查找的标记&lt;/param&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#808080">/// &lt;param name=&quot;replaceText&quot;&gt;替换的文字&lt;/param&gt;</span>
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    <span style="color:#0000FF">public</span> <span style="color:#0000FF">void</span> FindAndReplace(Word.Application wordApp, <span style="color:#0000FF">object</span> findText, <span style="color:#0000FF">object</span> replaceText)
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    {
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchCase = <span style="color:#0000FF">true</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchWholeWord = <span style="color:#0000FF">true</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchWildCards = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchSoundsLike = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchAllWordForms = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> forward = <span style="color:#0000FF">true</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> format = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchKashida = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchDiacritics = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchAlefHamza = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> matchControl = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> read_only = <span style="color:#0000FF">false</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> visible = <span style="color:#0000FF">true</span>;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> replace = 2;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        <span style="color:#0000FF">object</span> wrap = 1;
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">        wordApp.Selection.Find.Execute(<span style="color:#0000FF">ref</span> findText, <span style="color:#0000FF">ref</span> matchCase, <span style="color:#0000FF">ref</span> matchWholeWord, <span style="color:#0000FF">ref</span> matchWildCards,
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">            <span style="color:#0000FF">ref</span> matchSoundsLike, <span style="color:#0000FF">ref</span> matchAllWordForms, <span style="color:#0000FF">ref</span> forward, <span style="color:#0000FF">ref</span> wrap, <span style="color:#0000FF">ref</span> format, <span style="color:#0000FF">ref</span> replaceText,
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">            <span style="color:#0000FF">ref</span> replace, <span style="color:#0000FF">ref</span> matchKashida, <span style="color:#0000FF">ref</span> matchDiacritics, <span style="color:#0000FF">ref</span> matchAlefHamza, <span style="color:#0000FF">ref</span> matchControl);
</pre>
<pre style="background-color: #FFFFFF; font-family: consolas,'Courier New',courier,monospace; font-size: 10px; width: 100%; margin: 0em">    }</pre>
<h3>Related Posts</h3><ul><li><a href="http://louishan.com/articles/keep-microsoft-word-format-in-windows-live-writer.html" title="在Windows Live Writer中保持WORD格式粘贴 2010.06.7">在Windows Live Writer中保持WORD格式粘贴</a>(82)</li>
<li><a href="http://louishan.com/articles/become-software-wizard-with-cheat-sheets-and-posters.html" title="14个图表和画片让你成为软件大魔导[译文107] 2009.09.16">14个图表和画片让你成为软件大魔导[译文107]</a>(29)</li>
<li><a href="http://louishan.com/articles/msdn-sql-aspnet.html" title="MSDN: Sql与Asp.Net数据类型对应 2008.11.2">MSDN: Sql与Asp.Net数据类型对应</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-regexstring-validator.html" title="ASP.NET：利用RegexStringValidator验证字符串格式 2008.07.17">ASP.NET：利用RegexStringValidator验证字符串格式</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-datalist-nest-solution.html" title="ASP.NET 2.0：DataList嵌套DataList解决方案 2008.07.17">ASP.NET 2.0：DataList嵌套DataList解决方案</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-comparevalidator.html" title="ASP.NET：CompareValidator的使用 2008.07.15">ASP.NET：CompareValidator的使用</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-gridview-word-break.html" title="ASP.NET：GridView的一般换行与强制换行 2008.07.8">ASP.NET：GridView的一般换行与强制换行</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-gridview-rowcommand.html" title="ASP.NET：GridView中模板列、RowCommand和行号获取 2008.06.20">ASP.NET：GridView中模板列、RowCommand和行号获取</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-the-server-tag-is-not-well-formed.html" title="ASP.NET：The Server tag is not well formed 解决 2008.06.20">ASP.NET：The Server tag is not well formed 解决</a>(0)</li>
<li><a href="http://louishan.com/articles/aspnet-textbox-attributes.html" title="ASP.NET：在TextBox中实现鼠标点击文本消失 2008.05.30">ASP.NET：在TextBox中实现鼠标点击文本消失</a>(0)</li>
</ul><hr />
<p>© Louis for <a href="http://louishan.com">Louis Han Life Log</a>, 2012. |
<a href="http://louishan.com/articles/word-string-parameter-too-long-solutions.html">Permalink</a> |
<a href="http://louishan.com/articles/word-string-parameter-too-long-solutions.html#comments">17 条评论</a> |
<br/>
标签(Post tags): <a href="http://louishan.com/articles/tag/asp-net" rel="tag">Asp.Net</a>, <a href="http://louishan.com/articles/tag/word" rel="tag">word</a>, <a href="http://louishan.com/articles/tag/%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%8f%82%e9%87%8f%e8%bf%87%e9%95%bf" rel="tag">字符串参量过长</a><br/>
订阅(Subscribe): <a href="http://louishan.com/feed">直接订阅</a> | <a href="http://feed.feedsky.com/louishan">FeedSky</a> | <a href="http://feeds.feedburner.com/Louis_Han">FeedBurner</a></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://louishan.com/articles/word-string-parameter-too-long-solutions.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>SQL Server 2005：error 0 - 管道的另一端上无任何进程error 40 - 无法打开到 SQL Server 的连接</title>
		<link>http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html</link>
		<comments>http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html#comments</comments>
		<pubDate>Tue, 28 Jun 2011 00:00:00 +0000</pubDate>
		<dc:creator>Louis Han</dc:creator>
				<category><![CDATA[学习心得]]></category>
		<category><![CDATA[error 0]]></category>
		<category><![CDATA[error 40]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[外围应用配置器]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160;&#160;&#160; 程序调试的时候，因为数据库服务器装在本机，SQL Server 连接字符串一直将 Provider 设置为 “(local)”，发布程序的时候为了在不同机器进行测试，修改成了本机的 IP 地址，可是却出现了数据库连接错误。连续执行两次连接数据库操作，出现的错误提示如下：



<span class="readmore"><a href="http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html" title="SQL Server 2005：error 0 - 管道的另一端上无任何进程error 40 - 无法打开到 SQL Server 的连接">阅读全文——共820字</a></span>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 程序调试的时候，因为数据库服务器装在本机，SQL Server 连接字符串一直将 Provider 设置为 “(local)”，发布程序的时候为了在不同机器进行测试，修改成了本机的 IP 地址，可是却出现了数据库连接错误。连续执行两次连接数据库操作，出现的错误提示如下：</p>
<ul>
<li>已成功与服务器建立连接，但是在登录前的握手期间发生错误。在连接到 SQL Server 2005 时，在默认的设置下 SQL Server 不允许远程连接可能会导致此失败。（provider: 命名管道提供程序, error: 0 - 管道的另一端上无任何进程。） </li>
<li>在建立与服务器的连接时出错。在连接到 SQL Server 2005 时，在默认的设置下 SQL Server 不允许远程连接可能会导致此失败。（provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接） </li>
</ul>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/7f5725241dc94518ba3701420ed87e08" width="550" height="175" /></p>
<p>根据网上收集到的资料修改情况如下：</p>
<p>打开 SQL Server 2005 的 SQL Server Management Studio，</p>
<p>①在对象资源管理器的数据库服务器上右键选择属性，在打开的“服务器属性”窗口左侧树形菜单，点击“安全性”，在打开的右侧面板中“服务器身份验证”一项，点选“SQL Server 和 Windows 身份验证模式(S)”；</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/17f82b409ca54e06bd1d683108a1e299" width="524" height="315" /></p>
<p>②在对象资源管理器树形菜单中，展开“安全性→登录名→sa”，在打开的登录属性窗口选择“状态”，点选右侧的“登录”选项为“启用”状态；</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/7e02693435d44eab8b0c2659b1620cbb" width="550" height="302" /></p>
<p>实际上上面两项默认设置就是如此，如果安装之后没有修改，不需要执行上述两项操作，接下来执行，</p>
<p>③在系统开始菜单，选择“Microsoft SQL Server 2005→配置工具→SQL Server外围应用配置器”，打开配置窗口，点击窗口下方的“服务和连接的外围应用配置器”链接打开配置窗口，点击左侧树形菜单“MSSQLSERVER→Database Engine→远程连接”，右侧选择“本地连接和远程连接（仅适用TCP/IP）”。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/b58f34b62bd0472885e648d73bf9b97c" width="550" height="328" /></p>
<p>设置完成，重启 SQL Server 服务，搞定。</p>
<h3>Related Posts</h3><ul><li><a href="http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html" title="VS2005 C#动态递归绑定TreeView（WinForm） 2011.05.31">VS2005 C#动态递归绑定TreeView（WinForm）</a>(34)</li>
<li><a href="http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html" title="VS2005 C#连接带密码的Access数据库 2011.05.23">VS2005 C#连接带密码的Access数据库</a>(72)</li>
<li><a href="http://louishan.com/articles/weather-warmed-up.html" title="天气转暖，欣喜 2008.04.15">天气转暖，欣喜</a>(0)</li>
<li><a href="http://louishan.com/articles/sqlserver-database-log-shrink.html" title="解析SQLServer事务日志压缩与删除的方法 2008.04.11">解析SQLServer事务日志压缩与删除的方法</a>(0)</li>
<li><a href="http://louishan.com/articles/last-final-exam-fight.html" title="明天最后上战场 2008.01.9">明天最后上战场</a>(0)</li>
</ul><hr />
<p>© Louis for <a href="http://louishan.com">Louis Han Life Log</a>, 2011. |
<a href="http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html">Permalink</a> |
<a href="http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html#comments">22 条评论</a> |
<br/>
标签(Post tags): <a href="http://louishan.com/articles/tag/error-0" rel="tag">error 0</a>, <a href="http://louishan.com/articles/tag/error-40" rel="tag">error 40</a>, <a href="http://louishan.com/articles/tag/sql-server" rel="tag">SQL Server</a>, <a href="http://louishan.com/articles/tag/%e5%a4%96%e5%9b%b4%e5%ba%94%e7%94%a8%e9%85%8d%e7%bd%ae%e5%99%a8" rel="tag">外围应用配置器</a>, <a href="http://louishan.com/articles/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" rel="tag">数据库</a><br/>
订阅(Subscribe): <a href="http://louishan.com/feed">直接订阅</a> | <a href="http://feed.feedsky.com/louishan">FeedSky</a> | <a href="http://feeds.feedburner.com/Louis_Han">FeedBurner</a></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>VS2005 C#程序部署到没有安装.net的机器</title>
		<link>http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html</link>
		<comments>http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html#comments</comments>
		<pubDate>Tue, 21 Jun 2011 00:00:00 +0000</pubDate>
		<dc:creator>Louis Han</dc:creator>
				<category><![CDATA[学习心得]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[vs2005]]></category>
		<category><![CDATA[卸载程序]]></category>
		<category><![CDATA[安装部署项目]]></category>

		<guid isPermaLink="false">http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160;&#160;&#160; 要将C#程序部署到没有安装 .net Framework 2.0 的机器上，打包方法总结如下：

1.新建安装部署项目：     &#160;&#160;&#160;&#160;&#160;&#160;&#160; 点击新建项目，选择：其他项目类型-&#62;安装与部署-&#62;安装向导(安装项目也一样)，然后点击确定。

<span class="readmore"><a href="http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html" title="VS2005 C#程序部署到没有安装.net的机器">阅读全文——共1075字</a></span>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 要将C#程序部署到没有安装 .net Framework 2.0 的机器上，打包方法总结如下：</p>
<p><strong>1.新建安装部署项目：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 点击新建项目，选择：其他项目类型-&gt;安装与部署-&gt;安装向导(安装项目也一样)，然后点击确定。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/2973ea0585d94b0bbb8893e9bc709d70" width="520" height="346" /></p>
<p><strong>2.安装向导：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 第一步点击确定后出现安装向导，选择：创建用于Windows应用程序的安装程序（S）。一路点击下一步，或者直接点击完成。最终会在当前的解决方案中添加一个名称为“Setup1”的安装项目。</p>
<p><strong>3.开始制作：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 安装向导完成后进入项目文件夹，在窗口“文件系统（Setup1）”下，有三个文件夹：“应用程序文件夹”、“用户的‘程序’菜单”、“用户桌面”。     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 双击“应用程序文件夹”在右边的空白处右击，选择添加-&gt;文件，将你的做的应用程序的可执行文件和相应的类库和组件添加进来。然后右击你的文件，创建快捷方式，修改为你希望的名称，然后把快捷方式分别复制或剪切到左边的“用户的‘程序’菜单”和“用户桌面”中。这样安装程序安装完成后会在 “开始-&gt;所有程序”和“桌面”上生成程序的快捷方式。也可以直接在“用户的‘程序’菜单”和“用户桌面”相应目录下新建快捷方式，然后定位到你的文件。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/bb64cd43912f4a84ac68a11bfd3016fe" width="520" height="173" /></p>
<p><strong>4.修改安装目录：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 编辑“应用程序文件夹”属性，将属性中的“DefaultLocation”的路径中的“[Manufacturer]”去掉，不然的话做好的安装程序默认安装目录会是“C:\Program Files\你的用户名\安装解决方案名称”。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/ad070326938d4974ab1e566da5ebc69e" width="221" height="338" /></p>
<p><strong>5.关联必备组件：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 打开解决方案管理器，右击解决方案名称，在“Setup1”属性页，选择-&gt;“系统必备”，在打开的系统必备页中，在“指定系统必备安装组件的位置”选择-&gt;“从与我的应用程序相同的位置下载系统必备组件(D)”。选上以后，在生成的安装文件包中包含 .Net Framework 组件。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/12d11598687f4ede83fac5b83a6c7c80" width="520" height="378" /></p>
<p><strong>6.添加卸载功能：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 在安装项目“应用程序文件夹”，添加“X:\windows\system32”文件夹下的“msiexec.exe”，为了直观改名为“Uninstall.exe”，创建一个快捷方式放到“用户的‘程序’菜单”。鼠标左键单击部署项目，在属性标签中找到“ProductCode”，在卸载程序快捷方式的属性中，填写“/x {ProductCode}”（不含引号），即可生成带有卸载功能的安装程序（调用系统卸载功能）。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/af8b23c46d914caeb22f4c9abde3f397" width="221" height="338" />&#160; <img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/feac9e7a96db482aa5a01fdbecec36ca" width="221" height="338" /></p>
<p><strong>7.生成安装文件：</strong>     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; 在 VS 2005 菜单选择：生成-&gt;生成解决方案，打开解决方案文件夹下的Release/Debug文件夹，就可以看到生成的安装文件 Setup1.exe 和 Setup1.msi 了。</p>
<p>（感谢Google搜索到的众多参考资料作者）</p>
<h3>Related Posts</h3><ul><li><a href="http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html" title="VS2005 C#动态递归绑定TreeView（WinForm） 2011.05.31">VS2005 C#动态递归绑定TreeView（WinForm）</a>(34)</li>
<li><a href="http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html" title="VS2005 C#连接带密码的Access数据库 2011.05.23">VS2005 C#连接带密码的Access数据库</a>(72)</li>
<li><a href="http://louishan.com/articles/vs-2005-debug.html" title="VS2005实时调试的禁用和恢复 2009.06.30">VS2005实时调试的禁用和恢复</a>(0)</li>
<li><a href="http://louishan.com/articles/dotnet-date-format.html" title=".NET 中日期和时间的格式化处理 2008.06.20">.NET 中日期和时间的格式化处理</a>(0)</li>
<li><a href="http://louishan.com/articles/vs2005-microsoft-build-engine.html" title="VS2005未能加载文件或程序集Microsoft.Build.Engine 2008.06.1">VS2005未能加载文件或程序集Microsoft.Build.Engine</a>(0)</li>
<li><a href="http://louishan.com/articles/some-dropdownlist-updatepanel-solutions.html" title="又解决了几个问题：DropdownList和UpdatePanel 2008.04.23">又解决了几个问题：DropdownList和UpdatePanel</a>(0)</li>
<li><a href="http://louishan.com/articles/iis-aspnet-20-debug.html" title="IIS + ASP.NET 2.0调试过程 2008.04.10">IIS + ASP.NET 2.0调试过程</a>(0)</li>
</ul><hr />
<p>© Louis for <a href="http://louishan.com">Louis Han Life Log</a>, 2011. |
<a href="http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html">Permalink</a> |
<a href="http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html#comments">40 条评论</a> |
<br/>
标签(Post tags): <a href="http://louishan.com/articles/tag/net" rel="tag">.NET</a>, <a href="http://louishan.com/articles/tag/csharp" rel="tag">csharp</a>, <a href="http://louishan.com/articles/tag/vs2005" rel="tag">vs2005</a>, <a href="http://louishan.com/articles/tag/%e5%8d%b8%e8%bd%bd%e7%a8%8b%e5%ba%8f" rel="tag">卸载程序</a>, <a href="http://louishan.com/articles/tag/%e5%ae%89%e8%a3%85%e9%83%a8%e7%bd%b2%e9%a1%b9%e7%9b%ae" rel="tag">安装部署项目</a><br/>
订阅(Subscribe): <a href="http://louishan.com/feed">直接订阅</a> | <a href="http://feed.feedsky.com/louishan">FeedSky</a> | <a href="http://feeds.feedburner.com/Louis_Han">FeedBurner</a></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html/feed</wfw:commentRss>
		<slash:comments>40</slash:comments>
		</item>
		<item>
		<title>VS2005 C#动态递归绑定TreeView（WinForm）</title>
		<link>http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html</link>
		<comments>http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html#comments</comments>
		<pubDate>Tue, 31 May 2011 00:00:00 +0000</pubDate>
		<dc:creator>Louis Han</dc:creator>
				<category><![CDATA[学习心得]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[TreeView]]></category>
		<category><![CDATA[vs2005]]></category>
		<category><![CDATA[WinForm]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[递归绑定]]></category>

		<guid isPermaLink="false">http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160;&#160;&#160; 继续分享我在写程序当中的一些心得体会。其实早在学校的时候写博客，就经常分享一些自己的学习所得，不过那时候写程序都是 WebForm 的，基本上没有写过 WinForm 的程序。现在不同了，工作中用到 web 程序的机会不多，反而桌面端的程序用的多了，只好改行做 WinForm 程序了。

&#160;&#160;&#160;&#160;&#160;&#160;&#160; 我定义了一个单位数据表，用来存储单位信息，程序运行时要从数据库里取出单位信息动态绑定在 TreeView 树形菜单。这个数据表的结构是这样的：

<span class="readmore"><a href="http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html" title="VS2005 C#动态递归绑定TreeView（WinForm）">阅读全文——共2008字</a></span>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 继续分享我在写程序当中的一些心得体会。其实早在学校的时候写博客，就经常分享一些自己的学习所得，不过那时候写程序都是 WebForm 的，基本上没有写过 WinForm 的程序。现在不同了，工作中用到 web 程序的机会不多，反而桌面端的程序用的多了，只好改行做 WinForm 程序了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 我定义了一个单位数据表，用来存储单位信息，程序运行时要从数据库里取出单位信息动态绑定在 TreeView 树形菜单。这个数据表的结构是这样的：</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/7d7e0735fbde479eb42a27891d2580f7/renditions/fullsize.jpg" width="321" height="311" /></p>
<p>下面是相关的程序代码：</p>
<pre></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  1:         OleDbConnection connection = <span style="color: #0000ff">null</span>;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  2:         OleDbCommand command = <span style="color: #0000ff">null</span>;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  3:         OleDbDataAdapter dataAdapter = <span style="color: #0000ff">null</span>;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  4:         <span style="color: #0000ff">string</span> connectionString = &quot;<span style="color: #8b0000">Provider=Microsoft.Jet.OLEDB.4.0;Data Source = </span>&quot; +
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  5:                     Application.StartupPath + @&quot;<span style="color: #8b0000">\Database\TEST.mdb</span>&quot; + &quot;<span style="color: #8b0000">; Jet OleDb:Database Password = 12345</span>&quot;;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  6:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  7:         <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> FormMain_Load(<span style="color: #0000ff">object</span> sender, EventArgs e)
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  8:         {
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">  9:                 <span style="color: #008000">//初始化TreeView信息</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 10:                 TreeViewUnit_DataBind();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 11:         }
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 12:         <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> TreeViewUnit_DataBind()
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 13:         {
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 14:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 15:             <span style="color: #008000">//初始化连接</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 16:             connection = <span style="color: #0000ff">new</span> OleDbConnection(connectionString);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 17:             <span style="color: #008000">//打开连接</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 18:             connection.Open();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 19:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 20:             <span style="color: #008000">//初始化TreeView信息</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 21:             <span style="color: #0000ff">this</span>.treeViewUnit.Nodes.Clear();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 22:             TreeNode root = <span style="color: #0000ff">new</span> TreeNode(&quot;<span style="color: #8b0000">单位列表</span>&quot;);     <span style="color: #008000">//添加根节点</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 23:             root.Expand();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 24:             <span style="color: #0000ff">this</span>.treeViewUnit.Nodes.Add(root);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 25:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 26:             <span style="color: #0000ff">string</span> sqlStr = &quot;<span style="color: #8b0000">SELECT * FROM UNIT WHERE SuperiorID = 0</span>&quot;;  <span style="color: #008000">//选择大单位节点</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 27:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 28:             dataAdapter = <span style="color: #0000ff">new</span> OleDbDataAdapter(sqlStr, connection);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 29:             DataSet ds = <span style="color: #0000ff">new</span> DataSet();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 30:             dataAdapter.Fill(ds);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 31:             DataView dv = ds.Tables[0].DefaultView;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 32:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 33:             <span style="color: #0000ff">foreach</span> (DataRowView row <span style="color: #0000ff">in</span> dv)
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 34:             {
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 35:                 <span style="color: #0000ff">string</span> unitName = row[&quot;<span style="color: #8b0000">Unit_Name</span>&quot;].ToString().Trim();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 36:                 <span style="color: #0000ff">int</span> unitID = Convert.ToInt32(row[&quot;<span style="color: #8b0000">ID</span>&quot;].ToString().Trim());
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 37:                 TreeNode unit=<span style="color: #0000ff">new</span> TreeNode(unitName);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 38:                 unit.Expand();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 39:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 40:                 TreeView_AddChildNodes(unit, unitID);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 41:                 <span style="color: #0000ff">this</span>.treeViewUnit.Nodes.Add(unit);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 42:             }
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 43:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 44:             <span style="color: #008000">//关闭连接</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 45:             connection.Close();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 46:         }
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 47:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 48:         <span style="color: #008000">//TreeView递归添加子节点</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 49:         <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> TreeView_AddChildNodes(TreeNode node, <span style="color: #0000ff">int</span> nodeID)
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 50:         {
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 51:             <span style="color: #0000ff">string</span> sqlStr=&quot;<span style="color: #8b0000">SELECT * FROM UNIT WHERE SuperiorID = </span>&quot; + nodeID;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 52:             dataAdapter = <span style="color: #0000ff">new</span> OleDbDataAdapter(sqlStr, connection);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 53:             DataSet ds = <span style="color: #0000ff">new</span> DataSet();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 54:             dataAdapter.Fill(ds);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 55:             DataView dv = ds.Tables[0].DefaultView;
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 56:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 57:             <span style="color: #0000ff">foreach</span> (DataRowView row <span style="color: #0000ff">in</span> dv)
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 58:             {
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 59:                 <span style="color: #0000ff">string</span> unitName = row[&quot;<span style="color: #8b0000">Unit_Name</span>&quot;].ToString().Trim();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 60:                 <span style="color: #0000ff">int</span> unitID = Convert.ToInt32(row[&quot;<span style="color: #8b0000">ID</span>&quot;].ToString().Trim());
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 61:                 TreeNode unit = <span style="color: #0000ff">new</span> TreeNode(unitName);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 62:                 unit.Expand();
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 63:
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 64:                 TreeView_AddChildNodes(unit, unitID);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 65:                 node.Nodes.Add(unit);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 66:             }
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"> 67:         }</pre>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 程序的运行结果是这样的，满足了我的需求。因为是小程序，数据也比较少，所以没有考虑数据库存储优化等等。</p>
<p align="center"><img src="http://api.photoshop.com/v1.0/accounts/2ef56cf9f72d4754b1f9e2fe822f3cd4/assets/b512158c79614405bf23c6246b5c43cc/renditions/fullsize.jpg" width="150" height="215" /></p>
<h3>Related Posts</h3><ul><li><a href="http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html" title="VS2005 C#连接带密码的Access数据库 2011.05.23">VS2005 C#连接带密码的Access数据库</a>(72)</li>
<li><a href="http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html" title="SQL Server 2005：error 0 - 管道的另一端上无任何进程error 40 - 无法打开到 SQL Server 的连接 2011.06.28">SQL Server 2005：error 0 - 管道的另一端上无任何进程error 40 - 无法打开到 SQL Server 的连接</a>(22)</li>
<li><a href="http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html" title="VS2005 C#程序部署到没有安装.net的机器 2011.06.21">VS2005 C#程序部署到没有安装.net的机器</a>(40)</li>
<li><a href="http://louishan.com/articles/vs-2005-debug.html" title="VS2005实时调试的禁用和恢复 2009.06.30">VS2005实时调试的禁用和恢复</a>(0)</li>
<li><a href="http://louishan.com/articles/jvm-c-fiction.html" title="关于JVM 和C 的科普小说! [转载] 2008.07.2">关于JVM 和C 的科普小说! [转载]</a>(0)</li>
<li><a href="http://louishan.com/articles/vs2005-microsoft-build-engine.html" title="VS2005未能加载文件或程序集Microsoft.Build.Engine 2008.06.1">VS2005未能加载文件或程序集Microsoft.Build.Engine</a>(0)</li>
<li><a href="http://louishan.com/articles/some-dropdownlist-updatepanel-solutions.html" title="又解决了几个问题：DropdownList和UpdatePanel 2008.04.23">又解决了几个问题：DropdownList和UpdatePanel</a>(0)</li>
<li><a href="http://louishan.com/articles/weather-warmed-up.html" title="天气转暖，欣喜 2008.04.15">天气转暖，欣喜</a>(0)</li>
<li><a href="http://louishan.com/articles/sqlserver-database-log-shrink.html" title="解析SQLServer事务日志压缩与删除的方法 2008.04.11">解析SQLServer事务日志压缩与删除的方法</a>(0)</li>
<li><a href="http://louishan.com/articles/iis-aspnet-20-debug.html" title="IIS + ASP.NET 2.0调试过程 2008.04.10">IIS + ASP.NET 2.0调试过程</a>(0)</li>
</ul><hr />
<p>© Louis for <a href="http://louishan.com">Louis Han Life Log</a>, 2011. |
<a href="http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html">Permalink</a> |
<a href="http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html#comments">34 条评论</a> |
<br/>
标签(Post tags): <a href="http://louishan.com/articles/tag/c" rel="tag">c</a>, <a href="http://louishan.com/articles/tag/treeview" rel="tag">TreeView</a>, <a href="http://louishan.com/articles/tag/vs2005" rel="tag">vs2005</a>, <a href="http://louishan.com/articles/tag/winform" rel="tag">WinForm</a>, <a href="http://louishan.com/articles/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" rel="tag">数据库</a>, <a href="http://louishan.com/articles/tag/%e9%80%92%e5%bd%92%e7%bb%91%e5%ae%9a" rel="tag">递归绑定</a><br/>
订阅(Subscribe): <a href="http://louishan.com/feed">直接订阅</a> | <a href="http://feed.feedsky.com/louishan">FeedSky</a> | <a href="http://feeds.feedburner.com/Louis_Han">FeedBurner</a></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html/feed</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>VS2005 C#连接带密码的Access数据库</title>
		<link>http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html</link>
		<comments>http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html#comments</comments>
		<pubDate>Mon, 23 May 2011 07:09:41 +0000</pubDate>
		<dc:creator>Louis Han</dc:creator>
				<category><![CDATA[学习心得]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[vs2005]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://louishan.com/articles/vs2005-c%e8%bf%9e%e6%8e%a5%e5%b8%a6%e5%af%86%e7%a0%81%e7%9a%84access%e6%95%b0%e6%8d%ae%e5%ba%93.html</guid>
		<description><![CDATA[&#160;&#160;&#160;&#160;&#160;&#160;&#160; 最近在写一个简单的人员管理小程序，既然是自己人用，Access 数据库就已经足够了。不过以前用VS的时候一般习惯就用微软自己的Sqlserver数据库了，对于Access的操作还是比较生疏的。

&#160;&#160;&#160;&#160;&#160;&#160;&#160; 虽然Access数据库设置的密码是明码，可以直接读取。可是为了一丁点的安全考虑，还是设置了密码，于是就碰到了第一个问题：怎么连接带密码的Access数据库？

<span class="readmore"><a href="http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html" title="VS2005 C#连接带密码的Access数据库">阅读全文——共415字</a></span>]]></description>
			<content:encoded><![CDATA[<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 最近在写一个简单的人员管理小程序，既然是自己人用，Access 数据库就已经足够了。不过以前用VS的时候一般习惯就用微软自己的Sqlserver数据库了，对于Access的操作还是比较生疏的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 虽然Access数据库设置的密码是明码，可以直接读取。可是为了一丁点的安全考虑，还是设置了密码，于是就碰到了第一个问题：怎么连接带密码的Access数据库？</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 查了半天资料，网上到处都是垃圾信息，而且很多都是误导性的信息。经过层层筛选，终于找到了最合理的语句如下：</p>
<pre></pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px"><span style="color: #0000ff">string</span>  conn= &quot;<span style="color: #8b0000">Provider=Microsoft.Jet.OLEDB.4.0;Data Source = </span>&quot; +
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">               Application.StartupPath + @&quot;<span style="color: #8b0000">\database\test.mdb</span>&quot; +
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px">               &quot;<span style="color: #8b0000">;Jet OLEDB:Database Password=123456</span>&quot;;</pre>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; 很久没有写代码了，感觉手生得很。不知道再过几年，我的专业知识就完全忘掉了~ 一年写一次代码的悲哀啊！！</p>
<h3>Related Posts</h3><ul><li><a href="http://louishan.com/articles/vs2005-csharp-recursion-binding-treeview-in-winform.html" title="VS2005 C#动态递归绑定TreeView（WinForm） 2011.05.31">VS2005 C#动态递归绑定TreeView（WinForm）</a>(34)</li>
<li><a href="http://louishan.com/articles/sql-server-2005-error-0-and-error-40.html" title="SQL Server 2005：error 0 - 管道的另一端上无任何进程error 40 - 无法打开到 SQL Server 的连接 2011.06.28">SQL Server 2005：error 0 - 管道的另一端上无任何进程error 40 - 无法打开到 SQL Server 的连接</a>(22)</li>
<li><a href="http://louishan.com/articles/vs2005-csharp-deploy-application-to-system-without-dotnet.html" title="VS2005 C#程序部署到没有安装.net的机器 2011.06.21">VS2005 C#程序部署到没有安装.net的机器</a>(40)</li>
<li><a href="http://louishan.com/articles/vs-2005-debug.html" title="VS2005实时调试的禁用和恢复 2009.06.30">VS2005实时调试的禁用和恢复</a>(0)</li>
<li><a href="http://louishan.com/articles/tonido-cloudless-computing.html" title="Tonido：桌面上的云计算[译文44] 2009.04.12">Tonido：桌面上的云计算[译文44]</a>(0)</li>
<li><a href="http://louishan.com/articles/jvm-c-fiction.html" title="关于JVM 和C 的科普小说! [转载] 2008.07.2">关于JVM 和C 的科普小说! [转载]</a>(0)</li>
<li><a href="http://louishan.com/articles/vs2005-microsoft-build-engine.html" title="VS2005未能加载文件或程序集Microsoft.Build.Engine 2008.06.1">VS2005未能加载文件或程序集Microsoft.Build.Engine</a>(0)</li>
<li><a href="http://louishan.com/articles/some-dropdownlist-updatepanel-solutions.html" title="又解决了几个问题：DropdownList和UpdatePanel 2008.04.23">又解决了几个问题：DropdownList和UpdatePanel</a>(0)</li>
<li><a href="http://louishan.com/articles/weather-warmed-up.html" title="天气转暖，欣喜 2008.04.15">天气转暖，欣喜</a>(0)</li>
<li><a href="http://louishan.com/articles/sqlserver-database-log-shrink.html" title="解析SQLServer事务日志压缩与删除的方法 2008.04.11">解析SQLServer事务日志压缩与删除的方法</a>(0)</li>
</ul><hr />
<p>© Louis for <a href="http://louishan.com">Louis Han Life Log</a>, 2011. |
<a href="http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html">Permalink</a> |
<a href="http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html#comments">72 条评论</a> |
<br/>
标签(Post tags): <a href="http://louishan.com/articles/tag/access" rel="tag">access</a>, <a href="http://louishan.com/articles/tag/c" rel="tag">c</a>, <a href="http://louishan.com/articles/tag/vs2005" rel="tag">vs2005</a>, <a href="http://louishan.com/articles/tag/%e6%95%b0%e6%8d%ae%e5%ba%93" rel="tag">数据库</a><br/>
订阅(Subscribe): <a href="http://louishan.com/feed">直接订阅</a> | <a href="http://feed.feedsky.com/louishan">FeedSky</a> | <a href="http://feeds.feedburner.com/Louis_Han">FeedBurner</a></p>
<p><small>Feed enhanced by <a href='http://planetozh.com/blog/my-projects/wordpress-plugin-better-feed-rss/'>Better Feed</a> from  <a href='http://planetozh.com/blog/'>Ozh</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://louishan.com/articles/vs2005-csharp-connect-access-database-with-password.html/feed</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
	</channel>
</rss>

