<?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.silenk.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.silenk.com</link>
	<description>My territory, My way —— No Obstacle！</description>
	<lastBuildDate>Wed, 28 Apr 2010 12:28:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>T-SQL中产生随机数</title>
		<link>http://www.silenk.com/t-sql-rand-number/</link>
		<comments>http://www.silenk.com/t-sql-rand-number/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 12:28:31 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[Asp.Net[C#]]]></category>
		<category><![CDATA[DataBase]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=86</guid>
		<description><![CDATA[SQL开发中会有生成随机数的需求，下面说几种常用的需求和解决办法（基于MS SQL），最后总结出通用的办法：
1、0-9间的随机整数，包括0和9本身：

1
abs&#40;checksum&#40;newid&#40;&#41;&#41;&#41;%10

扩展从a-b间的随机整数（a,b本身也是整数）:

1
a+abs&#40;checksum&#40;newid&#40;&#41;&#41;&#41;%&#40;b-a+1&#41;

2、0-1间的随机实数：

1
rand&#40;&#41;

&#8211;或是

1
rand&#40;checksum&#40;newid&#40;&#41;&#41;&#41;

&#8211;推荐使用后一种。
由此扩展从a-b间的随机实数: a+rand(checksum(newid()))*(b-a)
注意上面的取法使得取值范围(a,b],若是想要取得(a,b)，可以采用下面的表达式
a+rand(checksum(newid()))*(b-a)*0.99999 &#8212; 此处0.99999可以更换为0.999999999，精度根据需要来取
]]></description>
			<content:encoded><![CDATA[<p>SQL开发中会有生成随机数的需求，下面说几种常用的需求和解决办法（基于MS SQL），最后总结出通用的办法：</p>
<p>1、0-9间的随机整数，包括0和9本身：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">abs<span style="color: #000000;">&#40;</span>checksum<span style="color: #000000;">&#40;</span>newid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%</span>10</pre></td></tr></table></div>

<p>扩展从a-b间的随机整数（a,b本身也是整数）:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">a<span style="color: #008000;">+</span>abs<span style="color: #000000;">&#40;</span>checksum<span style="color: #000000;">&#40;</span>newid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">%</span><span style="color: #000000;">&#40;</span>b<span style="color: #008000;">-</span>a<span style="color: #008000;">+</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>2、0-1间的随机实数：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">rand<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>&#8211;或是</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">rand<span style="color: #000000;">&#40;</span>checksum<span style="color: #000000;">&#40;</span>newid<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>&#8211;推荐使用后一种。</p>
<p>由此扩展从a-b间的随机实数: a+rand(checksum(newid()))*(b-a)</p>
<p>注意上面的取法使得取值范围(a,b],若是想要取得(a,b)，可以采用下面的表达式</p>
<p>a+rand(checksum(newid()))*(b-a)*0.99999 &#8212; 此处0.99999可以更换为0.999999999，精度根据需要来取</p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/t-sql-rand-number/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>DedeCMS 5.3升级后栏目无法更新和TAG无法使用等问题解决方法</title>
		<link>http://www.silenk.com/updata-dedecms-5-3-problem-tag-channel/</link>
		<comments>http://www.silenk.com/updata-dedecms-5-3-problem-tag-channel/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 09:11:50 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[Web-Research]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=82</guid>
		<description><![CDATA[dede5.3升级到dede5.5过程中遇到栏目无法更新和TAG无法使用的问题解决方法
有的用户由dede5.3升级到dede5.5过程中,会遇到栏目无法更新的问题.
即：我们要更新栏目设置的时候,提示无法更新栏目.
同时文章TAG无论填加修改都无效。
这通常是在线升级过程中的升级包bug造成的!

下面提供解决方法:
在dede后台中找到:
系统-&#62;SQL命令行工具
将下面的命令输入到文本框中,选择多行sql命令,运行一下即可!!
ALTER TABLE `dede_admintype` CHANGE `rank` `rank` FLOAT( 6 ) DEFAULT &#8216;1&#8242;;
UPDATE `dede_admintype` set `rank`=(5 + (`rank`/10)) where `rank` &#62; 10;
ALTER TABLE `dede_admin` CHANGE `usertype` `usertype` FLOAT( 6 ) UNSIGNED DEFAULT &#8216;0&#8242;;
UPDATE `dede_admin` set `usertype`=(5 + (`usertype`/10)) where `usertype` &#62; 10;
INSERT INTO `dede_sysconfig` (`aid`, `varname`, `info`, `groupid`, `type`, `value`) VALUES (746, &#8216;cfg_album_mark&#8217;, &#8216;图集是否使用水印(小图也会受影响)&#8217;, 3, &#8216;bool&#8217;, &#8216;N&#8217;);
ALTER [...]]]></description>
			<content:encoded><![CDATA[<p>dede5.3升级到dede5.5过程中遇到栏目无法更新和TAG无法使用的问题解决方法</p>
<p>有的用户由dede5.3升级到dede5.5过程中,会遇到栏目无法更新的问题.</p>
<p>即：我们要更新栏目设置的时候,提示无法更新栏目.</p>
<p>同时文章TAG无论填加修改都无效。</p>
<p>这通常是在线升级过程中的升级包bug造成的!</p>
<p><span id="more-82"></span></p>
<p>下面提供解决方法:</p>
<p>在dede后台中找到:</p>
<p>系统-&gt;SQL命令行工具</p>
<p>将下面的命令输入到文本框中,选择多行sql命令,运行一下即可!!</p>
<p>ALTER TABLE `dede_admintype` CHANGE `rank` `rank` FLOAT( 6 ) DEFAULT &#8216;1&#8242;;</p>
<p>UPDATE `dede_admintype` set `rank`=(5 + (`rank`/10)) where `rank` &gt; 10;</p>
<p>ALTER TABLE `dede_admin` CHANGE `usertype` `usertype` FLOAT( 6 ) UNSIGNED DEFAULT &#8216;0&#8242;;</p>
<p>UPDATE `dede_admin` set `usertype`=(5 + (`usertype`/10)) where `usertype` &gt; 10;</p>
<p>INSERT INTO `dede_sysconfig` (`aid`, `varname`, `info`, `groupid`, `type`, `value`) VALUES (746, &#8216;cfg_album_mark&#8217;, &#8216;图集是否使用水印(小图也会受影响)&#8217;, 3, &#8216;bool&#8217;, &#8216;N&#8217;);</p>
<p>ALTER TABLE `dede_arctype` ADD `seotitle` VARCHAR( 80 ) NOT NULL DEFAULT &#8221; AFTER `keywords` ;</p>
<p>UPDATE `dede_arctype` set `seotitle` = `typename`;</p>
<p>ALTER TABLE `dede_arctype` CHANGE `keywords` `keywords` VARCHAR( 60 ) NOT NULL DEFAULT &#8221;;</p>
<p>ALTER TABLE `dede_tagindex` ADD `typeid` SMALLINT( 5 ) UNSIGNED DEFAULT &#8216;0&#8242; NOT NULL AFTER `tag`;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/updata-dedecms-5-3-problem-tag-channel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>mysql导入数据库文件最大限制更改解决方法</title>
		<link>http://www.silenk.com/mysql-upload-limit/</link>
		<comments>http://www.silenk.com/mysql-upload-limit/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 13:06:46 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[DataBase]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=79</guid>
		<description><![CDATA[You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.]]></description>
			<content:encoded><![CDATA[<p>最近一次在用phpmyadmin导入mysql数据库时，偶的15M的数据库不能导入,mysql数据库最大只能导入2M..</p>
<p>phpmyadmin数据库导入出错：<br />
You probably tried to upload too large file. Please refer to documentation for ways to workaround this limit.</p>
<p>可以修改导入数据库文件最大限制吗?</p>
<p><span id="more-79"></span></p>
<p>到网上搜索了一下前人的解决办法，大多数都说修改php.ini中的upload_max_filesize，但修改了这个以后，还是提示这个问题；但phpmyadmin在提示这个问题的时候，右下角有一行英文提示，大致意思是说，解决这个问题，可以参考phpmyadmin文档；直接点击这个链接，phpmyadmin自动查找到了以下说明：</p>
<p>[1.16] I cannot upload big dump files (memory, http or timeout problems).<br />
Starting with version 2.7.0, the import engine has been re–written and these problems should not occur. If possible, upgrade your phpMyAdmin to the latest version to take advantage of the new import features.</p>
<p>The first things to check (or ask your host provider to check) are the values of upload_max_filesize, memory_limit and post_max_size in the php.ini configuration file. All of these three settings limit the maximum size of data that can be submitted and handled by PHP. One user also said that post_max_size and memory_limit need to be larger than upload_max_filesize.</p>
<p>以上文件大致说明的意思就是说，遇到导入过大文件时，首先检查php.ini 配置文件中的以下三个地方，upload_max_filesize, memory_limit 和post_max_size，并且推荐修改的值要稍大于导入的巨大sql数据库文件；依照这个提示，我修改了以上三个在php.ini中的值以后，重启了php环境，再次导入时，虽然phpmyadmin还是显示导入最大限制：20,480 KB，但巨大的80M数据库文件已经被成功的导入了。<br />
所用phpMyAdmin的版本为 2.8.1，环境：APMServ 5.1.2</p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/mysql-upload-limit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dede CMS 数据库结构简介</title>
		<link>http://www.silenk.com/dede-cms-database-structure/</link>
		<comments>http://www.silenk.com/dede-cms-database-structure/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 08:20:52 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[Web-Research]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=76</guid>
		<description><![CDATA[Dede CMS 数据库和配置文件结构一览]]></description>
			<content:encoded><![CDATA[<p>Dede CMS 数据库和配置文件结构一览</p>
<p><span id="more-76"></span></p>
<p><strong>1、程序核心程序目录及简介</strong></p>
<p>/include目录 程序核心目录</p>
<p>config_base.php 环境定义文件。用于检测系统环境，定义工作目录，保存数据库链接信息，引入常用函数等，建议不要修改。<br />
config_hand.php 系统配置文件。定义系统常用的配置信息定义，可从后台管理直接生成该文件。<br />
config_passport.php 通行证文件<br />
config_rglobals.php 检测系统外部变量<br />
config_rglobals_magic.php 同上<br />
inc_archives_view.php 用于浏览文档或对文档生成HTML<br />
inc_arclist_view.php 用于浏览频道列表或对内容列表生成HTML<br />
inc_arcmember_view.php 用于浏览会员发布的文档<br />
inc_arcpart_view.php 用于解析和创建全局性质的模板，如频道封面，主页，单个页面等<br />
inc_arcsearch_view.php 用于文档搜索<br />
inc_arcspec_view.php 用于浏览所有专题列表或对专题列表生成HTML<br />
inc_channel_unit.php 用户解析特定频道的附加数据结构信息<br />
inc_channel_unit_functions.php 系统共用函数集合<br />
inc_downclass.php 防采集随机字符串函数<br />
inc_freelist_view.php 用于对特定内容列表生成HTML<br />
inc_functions.php 可供用户使用的函数集合<br />
inc_imgbt.php GetTypeidSelMember<br />
inc_memberlogin.php 用于用户登录及获得会员状态<br />
inc_photograph.php 用于处理系统中的图片，例如水印，缩略图等<br />
inc_photowatermark_config.php 图片处理参数定义<br />
inc_rss_view.php 用于浏览频道RSS或对RSS生成静态文件<br />
inc_separate_functions.php SpGetArcList函数，用于获得文档列表<br />
inc_sitemap.php 用于生成网站地图<br />
inc_type_tree.php 用于选择栏目的目录树<br />
inc_type_tree_member.php 同上，会员使用<br />
inc_typelink.php 用于显示文章的位置和栏目位置等<br />
inc_typeunit_admin.php 用于频道管理时的一些复杂操作，主要用于后台<br />
inc_typeunit_menu.php 同上<br />
inc_userlogin.php 用于管理员登录<br />
inc_vote.php 用于管理投票<br />
jump.php 用于超链接跳转<br />
pub_charset.php 共用字符处理函数，GB/UTF-8/Unicode/BIG5等互换<br />
pub_collection.php 用于采集<br />
pub_collection_functions.php 采集用函数<br />
pub_datalist.php 后台管理用数据列表<br />
pub_datalist_dm.php 同上，不使用模板<br />
pub_db_mysql.php 用于操作数据库<br />
pub_dedehtml2.php 用于采集中的HTML解析<br />
pub_dedehtml.php HTML解析器<br />
pub_dedetag.php 用于dede模板标签解析<br />
pub_httpdown.php 用于下载http中的资源<br />
pub_oxwindow.php 后台程序扩展<br />
pub_splitword_www.php 织梦分词算法<br />
validateimg.php 验证码<br />
vdimgck.php 验证码</p>
<p><strong>/inc 共用函数目录</strong></p>
<p>inc_fun_funAdmin.php 获取拼音码等函数<br />
inc_fun_funString.php html代码处理等函数<br />
inc_fun_SpGetArcList.php 获取文档列表SpGetArcList<br />
对于菜鸟来说，对于菜鸟来说，config_base.php，这个文件，当你的程序出现问题，需要覆盖，当你的程序出现问题，需要覆盖。不要覆盖 config_base.php和config_hand.php文件，里面都是dede一些核心配置文件，对于高手来说，也许修改几个php页面已经达不到目的了，就需要了解dede的数据库结构，下面是部分关键数据表的结构和说明，不完全，还没有整理完全，过几天发布完整版本</p>
<p><strong>主要数据结构表</strong><br />
已经可以指导开发了<br />
<strong>dede_addonarticle　　　附加文章表 </strong><br />
aid int(11) 文章编号<br />
typeid int(11) 分类栏目编号<br />
body mediumtext 文章内容<br />
<strong>dede_addonflash 附加Flash表 </strong><br />
aid int(11) FLASH编号<br />
typeid int(11) 分类栏目编号<br />
filesize varchar(10) 文件大小<br />
playtime varchar(10) 播放时长<br />
flashtype varchar(10) 作品类型<br />
flashrank smallint(6) 作品等级<br />
width smallint(6) 影片宽度<br />
height smallint(6) 影片高度<br />
flashurl varchar(80) FLASH地址<br />
<strong>dede_addonimages 附加图集表 </strong><br />
aid int(11) 图集编号<br />
typeid int(11) 分类栏目编号<br />
pagestyle smallint(6) 表现方式（1单页显示 2分多页显示 3多行多列展示）<br />
maxwidth smallint(6) 大图限制宽度<br />
imgurls text 图片集内容（标签存放）<br />
row smallint(6) 多列式参数（行）<br />
col smallint(6) 多列式参数（列）<br />
isrm smallint(6) 是否下载远程图片<br />
ddmaxwidth smallint(6) 小图片宽度限制<br />
<strong>dede_addonsoft 附加软件表 </strong><br />
aid int(11) 软件编号<br />
typeid int(11) 分类栏目编号<br />
filetype varchar(10) 文件类型<br />
language varchar(10) 界面语言<br />
softtype varchar(10) 软件类型<br />
accredit varchar(10) 授权方式<br />
os varchar(30) 运行环境<br />
softrank int(11) 软件等级<br />
officialUrl varchar(30) 官方网址<br />
officialDemo varchar(50) 程序演示地址<br />
softsize varchar(10) 软件大小<br />
softlinks text 软件下载链接列表<br />
introduce text 软件介绍<br />
<strong>dede_addonspec 附加专题表 </strong><br />
aid int(11) 专题编号<br />
typeid int(11) 分类栏目编号<br />
note text 专题内容（仅存放标签代码）<br />
<strong>dede_admin 管理员信息表 </strong><br />
ID int(10) 自动编号<br />
usertype int(10) 用户类型<br />
userid varchar(30) 用户登录ID<br />
pwd varchar(50) 用户密码<br />
uname varchar(20) 用户笔名<br />
tname varchar(30) 真实姓名<br />
email varchar(30) 电子邮箱<br />
typeid int(11) 负责频道（0表示全部）<br />
logintime datetime 登录时间<br />
loginip varchar(20) 登录IP<br />
<strong>dede_admintype 系统用户组管理表 </strong><br />
rank smallint(6) 组级别编号<br />
typename varchar(30) 组名称<br />
system smallint(6) 是否为系统默认组<br />
purviews text 权限列表<br />
dede_arcatt 文档自定义属性表<br />
att smallint(6) 编号<br />
attname varchar(30) 属性名称<br />
<strong>dede_archives 文章表 </strong><br />
ID int(11) 自动编号<br />
typeid int(11) 所属主栏目编号<br />
typeid2 int(11) 所属副栏目编号<br />
sortrank int(11) 文章排序（置顶方法）<br />
iscommend smallint(6) 是否推荐<br />
ismake smallint(6) 是否生成静态<br />
channel int(11) 文章所属模型<br />
arcrank smallint(6) 阅读权限<br />
click int(11) 点击次数<br />
money smallint(6) 消费点数<br />
title varchar(80) 标题<br />
shorttitle varchar(36) 简略标题<br />
color varchar(10) 标题颜色<br />
writer varchar(30) 作者<br />
source varchar(50) 来源<br />
litpic varchar(100) 缩略图<br />
pubdate int(11) 录入时间<br />
senddate int(11) 发布时间<br />
arcatt smallint(6) 自定属性（att）<br />
adminID int(11) 发布管理员ID<br />
memberID int(11) 发布会员ID<br />
description varchar(250) 摘要<br />
keywords varchar(60) 关键词<br />
templet varchar(60) 文档模板<br />
lastpost int(11) 最近评论时间<br />
postnum int(11) 评论数目<br />
redirecturl varchar(150) 跳转网址<br />
mtype int(11) 用户自定义分类<br />
userip varchar(20) 用户IP<br />
locklikeid smallint(6) 是否锁定相关文章<br />
likeid varchar(240) 相关文章ID<br />
<strong>dede_arcrank 阅读权限表 </strong><br />
ID int(10) 自动编号<br />
rank smallint(10) 权限等级<br />
membername varchar(20) 等级名称<br />
adminrank smallint(10) 管理等级<br />
money int(11) 消费点数<br />
<strong>dede_arctype 栏目管理表 </strong><br />
ID int(10) 栏目编号（自动编号）<br />
reID int(10) 父栏目编号<br />
topID int(10)<br />
sortrank smallint(6) 排序编号<br />
typename varchar(30) 栏目名称<br />
typedir varchar(100) 栏目目录<br />
isdefault smallint(6) 栏目列表选项（1链接到默认页 0链接到列表第一页 -1使用动态页）<br />
defaultname varchar(20) 默认页的名称<br />
issend smallint(6) 是否支持投稿<br />
channeltype smallint(6) 频道类型<br />
maxpage int(11) 保留<br />
ispart smallint(6) 栏目属性<br />
corank smallint(6) 浏览权限<br />
tempindex varchar(60) 封面模板<br />
templist varchar(60) 列表模板<br />
temparticle varchar(60) 文章模板<br />
tempone varchar(60) 单独页面模板<br />
namerule varchar(50) 文章命名规则<br />
namerule2 varchar(50) 列表命名规则<br />
modname varchar(30) 模板名称<br />
description varchar(200) 栏目介绍<br />
keywords varchar(100) 关键词<br />
moresite smallint(6) 多站点支持<br />
siterefer smallint(6) 多站点站点根目录属性<br />
sitepath varchar(60) 多站点站点根目录<br />
siteurl varchar(60) 多站点绑定域名<br />
ishidden smallint(6) 是否隐藏栏目<br />
<strong>dede_area 地区表 </strong><br />
eid int(11) 地区编号<br />
name varchar(20) 地区名称<br />
rid int(11) 编号属性</p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/dede-cms-database-structure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET文件下载的几种方法</title>
		<link>http://www.silenk.com/asp-net-method-to-download/</link>
		<comments>http://www.silenk.com/asp-net-method-to-download/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 09:28:40 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[Asp.Net[C#]]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[下载]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=64</guid>
		<description><![CDATA[    当服务器要提供文件下载时，HttpResponse有这么几种方法可以使用。

用Response.WriteFile，如：

1
2
Response.ContentType = &#34;application/octet-stream&#34;;
Response.WriteFile&#40;@&#34;whatever.zip&#34;&#41;;

采用aspnet2.0的新方法 Response.TransmitFile,注意此方法将指定的文件直接写入 HTTP 响应输出流，而不在内存中缓冲该文件。如：

1
2
3
Response.ContentType = &#34;application/x-zip-compressed&#34;;
Response.AddHeader&#40;&#34;Content-Disposition&#34;, &#34;attachment;filename=downloadfilename.zip&#34;&#41;;
Response.TransmitFile&#40;@&#34;whatever.zip&#34;&#41;;

(假设同文件夹下有个需要下载的文件叫whatever.zip，而用户下载时默认名称为downloadfilename.zip)
需要注意的是，我们都知道Server.ScriptTimeout 的默认值是90秒，而当我们在web.config中打开调试模式，此值变为30，000，000秒。这也是为什么我在开发时一般不会发现超时问题。当下载大文件时，用Response.WriteFile会使Aspnet_wp.exe缓存了太大空间而导致下载失败。
这时建议采用文件流形式。如：

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
System.IO.Stream iStream = null;
&#160;
        //以10K为单位缓存:
        byte&#91;&#93; buffer = new Byte&#91;10000&#93;;
&#160;
       int length;
&#160;
       long dataToRead;
&#160;
   [...]]]></description>
			<content:encoded><![CDATA[<p>    当服务器要提供文件下载时，HttpResponse有这么几种方法可以使用。<span id="more-64"></span></p>
<ol>
<li>用Response.WriteFile，如：</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/octet-stream&quot;</span><span style="color: #008000;">;</span>
Response.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;whatever.zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<li>采用aspnet2.0的新方法 Response.TransmitFile,注意此方法将指定的文件直接写入 HTTP 响应输出流，而不在内存中缓冲该文件。如：</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-zip-compressed&quot;</span><span style="color: #008000;">;</span>
Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Disposition&quot;</span>, <span style="color: #666666;">&quot;attachment;filename=downloadfilename.zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Response.<span style="color: #0000FF;">TransmitFile</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;whatever.zip&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>(假设同文件夹下有个需要下载的文件叫whatever.zip，而用户下载时默认名称为downloadfilename.zip)</p>
<li>需要注意的是，我们都知道Server.ScriptTimeout 的默认值是90秒，而当我们在web.config中打开调试模式，此值变为30，000，000秒。这也是为什么我在开发时一般不会发现超时问题。当下载大文件时，用Response.WriteFile会使Aspnet_wp.exe缓存了太大空间而导致下载失败。</li>
<p>这时建议采用文件流形式。如：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">Stream</span> iStream <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//以10K为单位缓存:</span>
        <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #FF0000;">int</span> length<span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #FF0000;">long</span> dataToRead<span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// 制定文件路径.</span>
        <span style="color: #FF0000;">string</span> filepath <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;D:\mybigfile.zip&quot;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">//  得到文件名.</span>
        <span style="color: #FF0000;">string</span> filename <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">Path</span>.<span style="color: #0000FF;">GetFileName</span><span style="color: #000000;">&#40;</span>filepath<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">try</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// 打开文件.</span>
            iStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileStream</span><span style="color: #000000;">&#40;</span>filepath, <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileMode</span>.<span style="color: #0000FF;">Open</span>,
                        <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileAccess</span>.<span style="color: #0000FF;">Read</span>, <span style="color: #000000;">System.<span style="color: #0000FF;">IO</span></span>.<span style="color: #0000FF;">FileShare</span>.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
&nbsp;
            <span style="color: #008080; font-style: italic;">// 得到文件大小:</span>
            dataToRead <span style="color: #008000;">=</span> iStream.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
&nbsp;
            Response.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/octet-stream&quot;</span><span style="color: #008000;">;</span>
            Response.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Content-Disposition&quot;</span>, <span style="color: #666666;">&quot;attachment; filename=&quot;</span><span style="color: #008000;">+</span>filename<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>dataToRead <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//保证客户端连接</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Response.<span style="color: #0000FF;">IsClientConnected</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                   length <span style="color: #008000;">=</span> iStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buffer, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">10000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                   Response.<span style="color: #0000FF;">OutputStream</span>.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>buffer, <span style="color: #FF0000;">0</span>, length<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    Response.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    buffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">Byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">10000</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                    dataToRead <span style="color: #008000;">=</span> dataToRead <span style="color: #008000;">-</span> length<span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0600FF;">else</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #008080; font-style: italic;">//结束循环</span>
                    dataToRead <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #008080; font-style: italic;">// 出错.</span>
            Response.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error : &quot;</span> <span style="color: #008000;">+</span> ex.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">finally</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>iStream <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">//关闭文件</span>
                iStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/asp-net-method-to-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>20条.net【C#】编码良好习惯</title>
		<link>http://www.silenk.com/20-tips-csharp-habit/</link>
		<comments>http://www.silenk.com/20-tips-csharp-habit/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 08:50:25 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[Asp.Net[C#]]]></category>
		<category><![CDATA[.net]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=54</guid>
		<description><![CDATA[为了提高代码质量，经过搜索整理出以下20条，望共同进步！！！

  不要硬编string/ numeric，可以使用一些常量，例如：
不好的习惯：

1
2
3
4
5
6
7
8
9
static void Main&#40;string&#91;&#93; args&#41;
        &#123;
            int Count;
            Count = 100;
            if &#40;Count [...]]]></description>
			<content:encoded><![CDATA[<p><strong>为了提高代码质量，经过搜索整理出以下20条，望共同进步！！！</strong><span id="more-54"></span></p>
<ol>
<li>  不要硬编string/ numeric，可以使用一些常量，例如：</li>
<p>不好的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">int</span> Count<span style="color: #008000;">;</span>
            Count <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Count <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// 执行一些操作</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>好点的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> ZERO <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #FF0000;">int</span> Count<span style="color: #008000;">;</span>
            Count <span style="color: #008000;">=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Count <span style="color: #008000;">==</span> ZERO<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// 执行一些操作</span>
&nbsp;
            <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>  对于字符串比较-使用String. Empty ，而不是&#8221;"</li>
<li>  默认情况下将成员变量定义为 ‘private’，如果有需要，将其扩展为protected 、public 、 internal。</li>
<p>默认情况下使私人范围的优点是，在XMLSerilaization期间，默认情况下它会序列化的所有public成员。</p>
<li>  当我们要在循环操作字符串，使用StringBuilder，而不是字符串。</li>
<p>示例如下。<br />
不好的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">String</span>  temp <span style="color: #008000;">=</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Empty</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">;</span> i<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                temp <span style="color: #008000;">+=</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>好点的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">;</span> i<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>  简单的操作，比起Collection更倾向使用Array</li>
<li>  比起ArrayList更倾向使用Generic Collection</li>
<li>  比起HashTable更倾向使用Generic Dictionary</li>
<li>  对于字符串的操作和存储，倾向与StringCollection和StringDictionary</li>
<li>  使用适合的数据类型。</li>
<p>例如：你想要判断状态，使用bool比int要好。<br />
不好的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">int</span> Check <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> Check <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// 执行一些操作</span>
&nbsp;
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>好点的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">bool</span> Check <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>Check<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// 执行一些操作</span>
            <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;使用as做类型转换的时候，对转换后的值进行null值判断</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> A
   <span style="color: #000000;">&#123;</span>
&nbsp;
   <span style="color: #000000;">&#125;</span>
   <span style="color: #FF0000;">class</span> B <span style="color: #008000;">:</span> A
   <span style="color: #000000;">&#123;</span>
&nbsp;
   <span style="color: #000000;">&#125;</span>
    B objB <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> B<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    A objA1  <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>A<span style="color: #000000;">&#41;</span> objB<span style="color: #008000;">;</span>
    A objA2 <span style="color: #008000;">=</span> objB <span style="color: #0600FF;">as</span> A<span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> objA2 <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">//执行所需的操作</span>
     <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;创建wcf代理，使用using表达式</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span><span style="color: #000000;">&#40;</span>Cerate the proxy<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">//执行所需的操作</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;对于昂贵的资源(例如Connection, File 等等)，遵照&#8217;Acquire late, release early’ (尽量晚的获取，尽量早的释放)准则。</li>
<p>例子：如果你想在数据操作时，使用的SqlConnection对象，请在方法级别，而不是在类级别创建实例。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> MyData
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> MyData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> List GetAllCustomer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
         <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>SqlConnection objConnection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Connection string&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
             <span style="color: #008080; font-style: italic;">//执行一些操作得到需要的数据</span>
          <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
 <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>如果你想创建的类级别SqlConnection实例，确保您的类实现了IDisposable接口，并在Dispose()中清理SqlConnection实例。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #FF0000;">class</span> MyData <span style="color: #008000;">:</span> IDisposable
<span style="color: #000000;">&#123;</span>
    SqlConnection objConnection <span style="color: #008000;">;</span>
    <span style="color: #0600FF;">public</span> MyData<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
         objConnection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SqlConnection<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Connection string&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> List GetAllCustomer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
         <span style="color: #008080; font-style: italic;">//通过objConnection得到需要的数据</span>
     <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
         <span style="color: #008080; font-style: italic;">//清理SqlConnection实例</span>
          <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> objConnection <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
              <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> objConnection.<span style="color: #0000FF;">State</span> <span style="color: #008000;">==</span> ConnectionState.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#41;</span>
              <span style="color: #000000;">&#123;</span>
                   objConnection.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
              <span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
 <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;如果你不想别人扩展你的类功能，使用‘sealed’。</li>
<li>&nbsp;&nbsp;避免为每个类都声明‘destructor’ ，因为它会增加不需要常驻内存的类的生命周期。</li>
<li>&nbsp;&nbsp;相对manual threading，更倾向用Thread Pool 。</li>
<li>&nbsp;&nbsp;在循环内不要去调用其它方法。</li>
<p>例如：</p>
<p>不好的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Calculate<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>好点的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">//直接写Calculate逻辑。</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;不要在循环内处理异常，而是将循环处理的逻辑放在try/catch里面</li>
<p>不好的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">;</span> i<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #0600FF;">try</span>
   <span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
       <span style="color: #0600FF;">throw</span> ex<span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>好点的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">try</span>
<span style="color: #000000;">&#123;</span>
     <span style="color: #0600FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">;</span> i<span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
     <span style="color: #000000;">&#123;</span>
     <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">throw</span> ex<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;不用通过异常处理应用程序的逻辑</li>
<p>例如：</p>
<p>不好的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">try</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> x,y,z<span style="color: #008000;">;</span>
    x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    y <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
    z <span style="color: #008000;">=</span> y<span style="color: #008000;">/</span>x<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>DevideByZeroException ex<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">Throw</span> ex<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>好点的习惯：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">try</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> x,y,z<span style="color: #008000;">;</span>
    x <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    y <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> x <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        z <span style="color: #008000;">=</span> y<span style="color: #008000;">/</span>x<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<li>&nbsp;&nbsp;相对 foreach，倾向使用for/while 循环</li>
<li>&nbsp;&nbsp;在层与层之间的交互，比起使用DataSet/DataTables更倾向于使用数据对象传递。</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/20-tips-csharp-habit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS代码格式化工具</title>
		<link>http://www.silenk.com/format-css/</link>
		<comments>http://www.silenk.com/format-css/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 10:07:12 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[JavaScript&CSS]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[格式化]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=44</guid>
		<description><![CDATA[CSS 代码美化主要是解决那些写在一行CSS代码或经过压缩过的CSS代码造成的缺乏可读性问题。这个工具呢能够按大众化对这些CSS代码进行规范，如自动对齐、缩进、换行等处理。
工具可以选择横排或者竖排显示。





]]></description>
			<content:encoded><![CDATA[<p>CSS 代码美化主要是解决那些<strong>写在一行</strong>CSS代码或<strong>经过压缩</strong>过的CSS代码造成的缺乏可读性问题。这个工具呢能够按大众化对这些CSS代码进行规范，如<strong>自动对齐、缩进、换行</strong>等处理。</p>
<p>工具可以选择横排或者竖排显示。<span id="more-44"></span></p>
<p><textarea id="abc" cols="88" rows="12" title="在这里输入CSS代码"></textarea></p>
<input value=" 横排 " onclick="formatCSS(1)" type="button">
<input value=" 竖排 " onclick="formatCSS(2)" type="button">
<input value=" 清空 " onclick="formatCSS(3)" type="button">
<p><textarea id="def" cols="88" rows="12" title="这里将输出CSS代码"></textarea></p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/format-css/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript[JS]代码格式化工具</title>
		<link>http://www.silenk.com/format-js/</link>
		<comments>http://www.silenk.com/format-js/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 18:47:33 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[JavaScript&CSS]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[format]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[格式化]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=34</guid>
		<description><![CDATA[     Javascript（JS）代码美化主要是解决那些写在一行Javascript代码或经过压缩过的JS代码造成的缺乏可读性问题。这个工具呢能够按大众化对这些Javascript代码进行规范，如自动对齐、缩进、换行等处理。
     当然，因为JS代码的规范也较多，更鉴于很多人的代码书写也不尽规范，所以这个小工具整理出来的代码在直接使用上存在困难（因为小错误啊~~），不过整理出来的效果对大家学习人家的代码和修改代码是有很大的帮助的哦~~~！！~！
恩，使用如下咯：
/*这个是一个类*/function    xx   (num,str){//说明
var a = num;this.aa = a;this.bb = function(){alert(str);}this.cc = function(){for(var i=0;i2)  {   alert(); } else { alert(&#8221; hell &#8220;); }a.bb();b.dd();alert(a.aa); 

    格式化
    压缩


    经典
   [...]]]></description>
			<content:encoded><![CDATA[<p>     Javascript（JS）代码美化主要是解决那些<strong>写在一行</strong>Javascript代码或<strong>经过压缩</strong>过的JS代码造成的缺乏可读性问题。这个工具呢能够按大众化对这些Javascript代码进行规范，如<strong>自动对齐、缩进、换行</strong>等处理。</p>
<p>     当然，因为JS代码的规范也较多，更鉴于很多人的代码书写也不尽规范，所以这个小工具整理出来的代码在直接使用上存在困难（因为小错误啊~~），不过整理出来的效果对大家学习人家的代码和修改代码是有很大的帮助的哦~~~！！~！<span id="more-34"></span><br />
恩，使用如下咯：</p>
<p><TEXTAREA id=code rows=12 cols=100>/*这个是一个类*/function    xx   (num,str){//说明<br />
var a = num;this.aa = a;this.bb = function(){alert(str);}this.cc = function(){for(var i=0;i<10;i++){document.title=i;}}//str=str.replace(/(\\)/gi,'\\\\');<br />
};<br />
var o = {a:function(){},b:345}xx.prototype.dd= function(){alert("d  d        kd");}var a = new xx(100,"hello"),b=new xx(0,"ttyp");if(1>2)  {   alert(); } else { alert(&#8221; hell &#8220;); }a.bb();b.dd();alert(a.aa);</TEXTAREA> <BR></p>
<select id="cboOperate" onChange="if(this.selectedIndex==1)document.getElementById('cboStyle').disabled=true;else document.getElementById('cboStyle').disabled=false;">
    <option value="0">格式化</option><br />
    <option value="1">压缩</option><br />
</select>
<select id="cboStyle">
    <option value="0">经典</option><br />
    <option value="1">微软</option><br />
</select>
<p><INPUT onclick=go() type=button value=DoFormat><br />
<textarea id=display rows=12 cols=100></textarea></p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/format-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS:用CSS等比例缩放图片</title>
		<link>http://www.silenk.com/use-css-and-scaling-for-image/</link>
		<comments>http://www.silenk.com/use-css-and-scaling-for-image/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 10:32:08 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[JavaScript&CSS]]></category>
		<category><![CDATA[比例缩放]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=25</guid>
		<description><![CDATA[对于图片的缩放问题，一般情况下，貌似用一句
MAX-WIDTH: 40px; MAX-HEIGHT: 40px;
就搞顶了，在IE7，FF和Chrome中都比较正常，可是。。。IE6。。。
不想用JS，所以试了N久后：

width:expression(this.width > 100 &#038;&#038; this.width > this.height ? 100 : true);
height: expression(this.height > 100 ? 100 : true);
基本这样就兼容支持IE6、IE7、FF、和Chrome.
]]></description>
			<content:encoded><![CDATA[<p>对于图片的缩放问题，一般情况下，貌似用一句</p>
<blockquote><p>MAX-WIDTH: 40px; MAX-HEIGHT: 40px;</p></blockquote>
<p>就搞顶了，在IE7，FF和Chrome中都比较正常，可是。。。IE6。。。<br />
不想用JS，所以试了N久后：</p>
<blockquote><p>
width:expression(this.width > 100 &#038;&#038; this.width > this.height ? 100 : true);<br />
height: expression(this.height > 100 ? 100 : true);</p></blockquote>
<p>基本这样就兼容支持IE6、IE7、FF、和Chrome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/use-css-and-scaling-for-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>把CSS和JS写在同一个文件里</title>
		<link>http://www.silenk.com/combine-css-with-js-into-one-download/</link>
		<comments>http://www.silenk.com/combine-css-with-js-into-one-download/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 16:46:09 +0000</pubDate>
		<dc:creator>silenkee</dc:creator>
				<category><![CDATA[JavaScript&CSS]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[JS]]></category>

		<guid isPermaLink="false">http://www.silenk.com/?p=16</guid>
		<description><![CDATA[为了减少服务器连接数，加快网页执行效率。或者是为了确保JS执行时，所依赖CSS文件已经下载完成。你是否想过要把CSS和JS写到一个文件里呢？]]></description>
			<content:encoded><![CDATA[<p>为了减少服务器连接数，加快网页执行效率。<br />
或者是为了确保JS执行时，所依赖CSS文件已经下载完成。<br />
你是否想过要把CSS和JS写到一个文件里呢？</p>
<p>今天看到了一个方法，正好解决这个问题！<br />
[原作者的BLOG：<a href="http://blogs.msdn.com/shivap/archive/2007/05/01/combine-css-with-js-and-make-it-into-a-single-download.aspx" target="_blank">http://blogs.msdn.com/shivap/archive/2007/05/01/combine-css-with-js-and-make-it-into-a-single-download.aspx</a> ]<br />
这其实是利用了浏览器特性的一种HACK手法！<br />
作者说在IE和FF下能正常使用。我测试了下，显示正常。</p>
<p>关键点：CSS和JS解析器在处理注释的区别<br />
1.CSS解析器在碰到“&lt;!&#8211;”的时候会直接抛弃<br />
2.JS解析器在碰到“&lt;!&#8211;”的时候，会把他当作&#8221;//&#8221;处理。也就是说把他当做行注释处理！</p>
<p>看看下面这个代码片段：<br />
&lt;!&#8211; /*<br />
 function test(){alert(&#8220;虽然使用性有待商榷。\n但想法值得称赞！&#8221;);}<br />
&lt;!&#8211; */<br />
&lt;!&#8211; body { background-color: #f00; }CSS解析时，它等价于下面这段代码<br />
 /*<br />
 function test(){alert(&#8220;虽然使用性有待商榷。\n但想法值得称赞！&#8221;);}<br />
 */<br />
 body { background-color: #f00; }而JS解析时，他却等价于如下代码</p>
<p> ///*                                              &#8212;-行注释<br />
 function test(){alert(&#8220;虽然使用性有待商榷。\n但想法值得称赞！&#8221;);}<br />
 //*/                                              &#8212;-行注释<br />
 //body { background-color: #f00; }                &#8212;-行注释问题是不是解决了呢？<br />
将这段代码，保存成1.jcs。[或其他任意文件名，引用的时候做对应修改就行了]<br />
然后，将下面代码保存成任意文件。和他放在同一目录下即可！</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>link type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span> rel<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;stylesheet&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.jcs&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span> language<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.jcs&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>input type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;button&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;点我一下，看包含的函数是否可以执行&quot;</span>  onclick<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;test()&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>link 和 script标签虽然两次链接该文件。但是由于缓存的缘故，只会发一次请求！<br />
当然，代价是增加了约等于“4*css行数”的冗于代码。正常情况下，这个冗于在1K以内！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.silenk.com/combine-css-with-js-into-one-download/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
