public static void CreateStaticHtml(string sql,string connection)
{
//FileStream fs = new FileStream(@"F:\New120mmm\template\news.html", FileMode.CreateNew);
StreamReader objReader = new StreamReader(@"F:\New120mmm\template\news.html", UnicodeEncoding.GetEncoding("GB2312"));
string HtmlText = objReader.ReadToEnd();
objReader.Close();
string file = @"F:\New120mmm\yaopinchaxun\{0}.html";
string info = "{0}个文件已生成!";
string total = "共生成{0}个文件";
int i = 0;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//HtmlText = String.Format(HtmlText, reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["chu"], reader["content"].ToString(), reader["PreviewId"].ToString() + ". html", reader["PreviewTitle"].ToString(), reader["NextId"].ToString() + ".html", reader["NextTitle"].ToString());
HtmlText = HtmlText.Replace("{0}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{1}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{2}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{3}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{4}", reader["chu"].ToString());
HtmlText = HtmlText.Replace("{5}", reader["content"].ToString());
HtmlText = HtmlText.Replace("{6}", reader["PreviewId"].ToString() + ".html");
HtmlText = HtmlText.Replace("{7}", reader["PreviewTitle"].ToString());
HtmlText = HtmlText.Replace("{8}", reader["NextId"].ToString() + ".html");
HtmlText = HtmlText.Replace("{9}", reader["NextTitle"].ToString());
file = String.Format(file, reader[0].ToString());
info = String.Format(info, reader[0].ToString());
if (!File.Exists(file))
{
FileStream fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, UnicodeEncoding.GetEncoding("GB2312"));
sw.Write(HtmlText);
sw.Close();
sw.Dispose();
fs.Close();
}
Console.WriteLine(info);
i++;
//Console.WriteLine(reader["title"].ToString());
}
reader.Close();
}
Console.WriteLine(string.Format(total, i));
Console.Read();
}
加注释的地方报错
未处理 System.FormatException
Message="输入字符串的格式不正确。"
Source="mscorlib"
StackTrace:
在 System.Text.StringBuilder.FormatError()
在 System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
在 System.String.Format(IFormatProvider provider, String format, Object[] args)
在 System.String.Format(String format, Object[] args)
在 CreateHtmlFile.Program.CreateStaticHtml(String sql, String connection) 位置 E:\project\CreateHtmlFile\CreateHtmlFile\Program.cs:行号 36
在 CreateHtmlFile.Program.Main(String[] args) 位置 E:\project\CreateHtmlFile\CreateHtmlFile\Program.cs:行号 15
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
还有一个怪事,SqlDataReader 不循环,永远都执行第一条 请问怎么解决呢?谢谢
{
//FileStream fs = new FileStream(@"F:\New120mmm\template\news.html", FileMode.CreateNew);
StreamReader objReader = new StreamReader(@"F:\New120mmm\template\news.html", UnicodeEncoding.GetEncoding("GB2312"));
string HtmlText = objReader.ReadToEnd();
objReader.Close();
string file = @"F:\New120mmm\yaopinchaxun\{0}.html";
string info = "{0}个文件已生成!";
string total = "共生成{0}个文件";
int i = 0;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//HtmlText = String.Format(HtmlText, reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["chu"], reader["content"].ToString(), reader["PreviewId"].ToString() + ". html", reader["PreviewTitle"].ToString(), reader["NextId"].ToString() + ".html", reader["NextTitle"].ToString());
HtmlText = HtmlText.Replace("{0}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{1}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{2}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{3}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{4}", reader["chu"].ToString());
HtmlText = HtmlText.Replace("{5}", reader["content"].ToString());
HtmlText = HtmlText.Replace("{6}", reader["PreviewId"].ToString() + ".html");
HtmlText = HtmlText.Replace("{7}", reader["PreviewTitle"].ToString());
HtmlText = HtmlText.Replace("{8}", reader["NextId"].ToString() + ".html");
HtmlText = HtmlText.Replace("{9}", reader["NextTitle"].ToString());
file = String.Format(file, reader[0].ToString());
info = String.Format(info, reader[0].ToString());
if (!File.Exists(file))
{
FileStream fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, UnicodeEncoding.GetEncoding("GB2312"));
sw.Write(HtmlText);
sw.Close();
sw.Dispose();
fs.Close();
}
Console.WriteLine(info);
i++;
//Console.WriteLine(reader["title"].ToString());
}
reader.Close();
}
Console.WriteLine(string.Format(total, i));
Console.Read();
}
加注释的地方报错
未处理 System.FormatException
Message="输入字符串的格式不正确。"
Source="mscorlib"
StackTrace:
在 System.Text.StringBuilder.FormatError()
在 System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
在 System.String.Format(IFormatProvider provider, String format, Object[] args)
在 System.String.Format(String format, Object[] args)
在 CreateHtmlFile.Program.CreateStaticHtml(String sql, String connection) 位置 E:\project\CreateHtmlFile\CreateHtmlFile\Program.cs:行号 36
在 CreateHtmlFile.Program.Main(String[] args) 位置 E:\project\CreateHtmlFile\CreateHtmlFile\Program.cs:行号 15
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
还有一个怪事,SqlDataReader 不循环,永远都执行第一条 请问怎么解决呢?谢谢
解决方案 »
- C# 新手,求助,急,在线等
- 高手快快现身,看看我的钩子程序哪里错了?
- vs2005新建网站对话框中三种位置选项(文件系统、http、ftp)的区别?
- 在WinForm中,如何设置TreeView各级子节点的Checkbox
- 在vs2003中自定义安装问题,请高手指教
- 在ListView的单击事件中如何获得单击项的行、列索引?
- 怎么才能格式化指定的磁盘
- 现有的系统 需要添加功能 的问题
- 请问哪有水晶报表9的下载?最好中文版的,解密的,或带注册码
- 访问SQL-server如何应用组件,连接字符串怎么写?在线等待!
- 急,急,sql问题?
- C#应用程序上传附件到数据库并读取的例子.(附件类型分为pdf 和doc)
file变量本来是一个模板,你却去赋值,那不就在第一次循环的时候把这个模板冲掉了?第二次当然报错了。
{
//FileStream fs = new FileStream(@"F:\New120mmm\template\news.html", FileMode.CreateNew);
StreamReader objReader = new StreamReader(@"F:\New120mmm\template\news.html", UnicodeEncoding.GetEncoding("GB2312"));
string HtmlText = objReader.ReadToEnd();
objReader.Close();
string file1 = @"F:\New120mmm\yaopinchaxun\{0}.html";
string info1 = "{0}个文件已生成!";
string file = "";
string info = "";
string total = "共生成{0}个文件";
int i = 0;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//HtmlText = String.Format(HtmlText, reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["chu"], reader["content"].ToString(), reader["PreviewId"].ToString() + ". html", reader["PreviewTitle"].ToString(), reader["NextId"].ToString() + ".html", reader["NextTitle"].ToString());
HtmlText = HtmlText.Replace("{0}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{1}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{2}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{3}", reader["title"].ToString());
HtmlText = HtmlText.Replace("{4}", reader["chu"].ToString());
HtmlText = HtmlText.Replace("{5}", reader["content"].ToString());
HtmlText = HtmlText.Replace("{6}", reader["PreviewId"].ToString() + ".html");
HtmlText = HtmlText.Replace("{7}", reader["PreviewTitle"].ToString());
HtmlText = HtmlText.Replace("{8}", reader["NextId"].ToString() + ".html");
HtmlText = HtmlText.Replace("{9}", reader["NextTitle"].ToString());
file = String.Format(file1, reader[0].ToString());
info = String.Format(info1, reader[0].ToString());
if (!File.Exists(file))
{
FileStream fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, UnicodeEncoding.GetEncoding("GB2312"));
sw.Write(HtmlText);
sw.Close();
sw.Dispose();
fs.Close();
}
Console.WriteLine(info);
i++;
//Console.WriteLine(reader["title"].ToString());
}
reader.Close();
}
Console.WriteLine(string.Format(total, i));
Console.Read();
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
string info1 = "{0}个文件已生成!";
string file = "";
string info = "";
file = String.Format(file1, reader[0].ToString());
info = String.Format(info1, reader[0].ToString()); 你确信两个地方都改了吗?
现在报的错和以前的一样的?
把你改过的代码以及报的错再贴出来
string HtmlText = objReader.ReadToEnd();
string _HtmlText = "";
objReader.Close();
string file = @"F:\New120mmm\yaopinchaxun\{0}.html";
string file1 = "";
string info = "{0}个文件已生成!";
string info1 = "";
string total = "共生成{0}个文件";
int i = 0;
using (SqlConnection conn = new SqlConnection(connection))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//_HtmlText = String.Format(HtmlText, reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["title"].ToString(), reader["chu"], reader["content"].ToString(), reader["PreviewId"].ToString() + ". html", reader["PreviewTitle"].ToString(), reader["NextId"].ToString() + ".html", reader["NextTitle"].ToString());
_HtmlText = HtmlText.Replace("{0}", reader["title"].ToString());
_HtmlText = HtmlText.Replace("{1}", reader["title"].ToString());
_HtmlText = HtmlText.Replace("{2}", reader["title"].ToString());
_HtmlText = HtmlText.Replace("{3}", reader["title"].ToString());
_HtmlText = HtmlText.Replace("{4}", reader["chu"].ToString());
_HtmlText = HtmlText.Replace("{5}", reader["content"].ToString());
_HtmlText = HtmlText.Replace("{6}", reader["PreviewId"].ToString() + ".html");
_HtmlText = HtmlText.Replace("{7}", reader["PreviewTitle"].ToString());
_HtmlText = HtmlText.Replace("{8}", reader["NextId"].ToString() + ".html");
_HtmlText = HtmlText.Replace("{9}", reader["NextTitle"].ToString());
file1 = String.Format(file, reader[0].ToString());
info1 = String.Format(info, reader[0].ToString());
if (!File.Exists(file1))
{
FileStream fs = new FileStream(file1, FileMode.Create);
StreamWriter sw = new StreamWriter(fs, UnicodeEncoding.GetEncoding("GB2312"));
sw.Write(_HtmlText);
sw.Close();
sw.Dispose();
fs.Close();
}
Console.WriteLine(info1);
i++;
//Console.WriteLine(reader["title"].ToString());
}
reader.Close();
}
Console.WriteLine(string.Format(total, i));
Console.Read();
加注释的哪行 还是有错误
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta name="keywords" content="{0}" />
<meta name="description" content="{1}" />
<title>{2}- 药性、成分、功能、主治、用法、用量、使用说明|医浪网</title>
<link href="../css/blue_news.css" rel="stylesheet" type="text/css" />
<script src="../Js/CommonAjaxInterFace.js" type="text/javascript"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
function textPrint(){
textNews.focus();
var code = "<body onload=window.print()>";
code += "<div style=\"text-align: center; font-size:18pt; font-weight:bold\">";
code += news_title.innerHTML;
code += "<br><br></div><div align=right style=\"font-size:12px;\">【文档来源:医浪网(http://www.120mmm.com)】</div><hr><br><font style=\"font-family:宋体;font-size:10.5pt; line-height:22px;\">" + news_content.innerHTML + "</font></body>";
textNews.document.write(code);
textNews.document.close();
}
</script>
<style type="text/css">
.huisefengexian {
MARGIN-BOTTOM: 10px; PADDING-BOTTOM: 3px; COLOR: #666666; BORDER-BOTTOM: #d9d9d9 1px solid; TEXT-ALIGN: center
}
</style>
<!--页面文件开始 [头文件及尾部copyright信息,做成嵌套格式的]--><!--头部文件开始-->
<!--passport & choose color [未登陆用户]-->
<table cellpadding="0" cellspacing="0" id="passporttable">
<tr>
<td width="184"><img src="../images/BlueSkin/120mmmpassport.gif" alt="120mmm passport" width="169" height="26" /></td>
<td width="351"><a href="http://www.120mmm.com/bbs/login.aspx">登陆</a> <a href="http://www.120mmm.com/bbs/register.aspx?agree=yes">注册</a> <a href="http://www.120mmm.com/bbs/getpassword.aspx">取回密码</a></td>
<td width="242"><img src="../images/choosecolor.gif" alt="选择您喜欢的颜色方案" width="242" height="26" /></td>
</tr>
</table>
<!--LOGO和频道导航-->
<table cellpadding="0" cellspacing="0" id="daohang">
<tr>
<td width="227"><img src="../images/logo.gif" alt="logo" /></td>
<td width="55"><img src="../images/BlueSkin/top_home.jpg" width="43" height="43" class="top_link"/><a href="#">首页</a></td>
<td width="55"><a href="../News/"><img src="../images/BlueSkin/top_news_yello.jpg" width="43" height="43" class="top_link"/></a><a href="../News/">健康资讯</a></td>
<td width="55"><a href="../Baojian/"><img src="../images/BlueSkin/top_biaojian.jpg" width="43" height="43" class="top_link"/></a><a href="../Baojian/">保健</a></td>
<td width="55"><a href="../Disease/"><img src="../images/BlueSkin/top_bing.jpg" width="43" height="43" class="top_link"/></a><a href="../Disease/">疾病大全</a></td>
<td width="55"><a href="../Lady/"><img src="../images/BlueSkin/top_lady.jpg" width="43" height="43" class="top_link"/></a><a href="../Lady/">女人</a></td>
<td width="55"><a href="../Sex/"><img src="../images/BlueSkin/top_sex.jpg" width="43" height="43" class="top_link"/></a><a href="../Sex/">两性</a></td>
<td width="55">
<a href="../Liver/">
<img src="../images/BlueSkin/top_gang.jpg" width="43" height="43" class="top_link"/></a><a href="../Liver/">肝病</a></td>
<td width="55">
<a href="http://www.120md.com/">
<img src="../images/BlueSkin/top_cancer.jpg" width="43" height="43" class="top_link"/></a><a href="http://www.120md.com/">肿瘤</a></td>
<td width="55">
<a href="../Diagnose/">
<img src="../images/BlueSkin/top_self.jpg" width="43" height="43" class="top_link"/></a><a href="../Diagnose/">自诊</a></td>
<td width="55"><a href="../Xinli/"><img src="../images/BlueSkin/top_psyche.jpg" width="43" height="43" class="top_link"/></a><a href="../Xinli/">心理</a></td>
</tr>
</tr>
</table>
<!--健康咨询子栏目导航和新闻搜索-->
<table cellpadding="0" cellspacing="0" id="jkzx-search">
<tr>
<td width="30"><img src="../images/BlueSkin/jkzx-search-biao.gif" alt="新闻资讯" width="25" height="26"/></td>
<td width="66">新闻搜索</td>
<td width="141"><input type="text" class="jkzx-serch-shurukuang" /></td>
<td width="90"><img src="../images/BlueSkin/jkzx-search-bot.gif" alt="搜索" width="32" height="16" /></td>
<td width="450"> <script type="text/javascript"><!--
google_ad_client = "pub-4499924958165669";
/* 468x15, 创建于 08-9-24 */
google_ad_slot = "2161398051";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></td>
</tr>
</table>
<!--第一栏广告条 600*80 广告条位置和内容将随机或顺序显示-->
<table cellpadding="0" cellspacing="0" id="jkzx-banner1" class="table-bg-border">
<tr>
<td>
<script type="text/javascript"><!--
google_ad_client = "pub-4499924958165669";
/* 728x90, 创建于 08-9-23 */
google_ad_slot = "4171821859";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>
<!-- TOP 结束 --><!-- 首页内容模块 开始 -->
<!--第一行[热点图片新闻 | 头条新闻 | 最新专题]-->
<table cellpadding="0" cellspacing="0" id="jkzx-1">
<tr>
<td width="601" valign="top">
<!--热点图片新闻-->
<table cellpadding="0" cellspacing="0" id="newsshow">
<tr>
<td class="title">
<ul>
<li class="news_title" id="news_title"><nobr>{3}</nobr></li>
<li class="news_menu"><nobr>【字体:<span style="CURSOR: hand" onclick="javascript:news_content.style.fontSize = '15pt'">大</span>
<span style="CURSOR: hand" onclick="javascript:news_content.style.fontSize = '11pt'">中</span>
<span style="CURSOR: hand" onclick="javascript:news_content.style.fontSize = '9pt'">小</span>】 【<span style="cursor:hand" onclick="textPrint()">打印本稿</span>】 <a href="javascript:JsAddFavorite();">【收藏本文】</a> 【发布日期:2007-10-13】 【<a href="javascript:opener=null;window.close();">关闭本页</a>】</nobr></li>
</ul>
</td>
</tr>
<tr>
<td class="content">
<div>
<ul><li class="huisefengexian">{4}</li>
<li id="news_content" class="news_content">
{5}
</li>
<li style=" float:right">【文章来源:新浪网】【责任编辑:医浪admin】</li>
</ul>
<br /><br /><br /><br />
<div class="huisefengexian"></div>
上一条:
<a href="{6}">{7}</a>
<br />
下一条:
<a href="{8}">{9}</a>
<br /> </div>
</td>
</tr>
<tr><td></td></tr>
<tr><td>
</td></tr>
</table>
</td>
<td valign="top">
<!--新闻右边-->
<!--往期回顾-->
<table cellpadding="0" cellspacing="0" class="table-bg-border" id="jkzx-zthgLie">
<tr>
<td>
<script type="text/javascript"><!--
google_ad_client = "pub-4499924958165669";
/* 160x600, 创建于 08-10-6 */
google_ad_slot = "9860789022";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>
<!--往期回顾结束-->
</td>
</tr>
</table><table class="table-bg-border" id="LinkUs">
<tr>
<td><a href="../about/aboutus.aspx">关于医浪网</a> | <a href="../sitemap.htm">网站地图</a> | <a href="#">在线客服</a> | <a href="../about/ContectUs.aspx">联系我们</a> | <a href="#">合作伙伴</a> | <a href="#">广告服务</a> | <a href="#">业务合作</a> | <a href="#">法律顾问</a> | <a href="#">隐私保护</a></td>
</tr>
</table>
<br />
<br />
<div align="center">最后更改时间:<span style="color:#0000FF">
<!-- #BeginDate format:fcCh2a -->2008年12月30日 星期二 5:24 PM<!-- #EndDate -->
</span></div></body>
</html>