怎样把生成的静态页面的文件名称保存在数据库中?
程序的代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //添加引用
using System.Text;
using System.IO;
using System.Collections;
namespace staticeindex
{
/// <summary>
/// media 的摘要说明。
/// </summary>
public class media : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnshengcheng;
protected System.Web.UI.WebControls.ListBox ListBox1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnshengcheng.Click += new System.EventHandler(this.btnshengcheng_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnshengcheng_Click(object sender, System.EventArgs e)
{
string ConStr = "Server=(local);DataBase=ad_class;Uid=sa;Pwd=";
string prostr="select ID from Province_manager ";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand Cmd1 = new SqlCommand(prostr,Con);
SqlDataReader dr1 = Cmd1.ExecuteReader();
Hashtable hs = new Hashtable();
if(dr1.HasRows)
{
while(dr1.Read())
{
hs.Add(dr1["ID"].ToString(),dr1["ID"].ToString());
}
}
dr1.Close();
Cmd1.Dispose();
foreach(DictionaryEntry de in hs)
{
string strsql="select * from Province_manager join media_info on Province_manager.ID= media_info.Parent_province_id where Province_manager.ID= '"+de.Key.ToString()+"'";
SqlCommand Cmd = new SqlCommand(strsql,Con);
DataSet ds=new DataSet();
SqlDataReader dr = Cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while(dr.Read())
{
sb.Append("<a href=#>"+dr["Key_index"].ToString()+"</a>"+" ");
sb.Append(" ");
}
WriteFile(sb.ToString());
dr.Close();
}
Response.Write("<script>alert('静态页面生成成功!');location='media.aspx'</script>"); }
public bool WriteFile(string Key_index)
{
string OutPutPath = HttpContext.Current.Server.MapPath("province/city/media/");
Encoding encoding = Encoding.GetEncoding("gb2312");
// 读取模板文件
string ModelTemp = HttpContext.Current.Server.MapPath("ModelHTML.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(ModelTemp, encoding);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
} Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
string HtmlFilename = rd.Next().ToString()+".html";
// 替换内容
str = str.Replace("ArticleContent", Key_index);
// 写文件
try
{
sw = new StreamWriter(OutPutPath+HtmlFilename, false, encoding);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
}
}
还请各位大侠帮帮忙,该怎样来写?
程序的代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; //添加引用
using System.Text;
using System.IO;
using System.Collections;
namespace staticeindex
{
/// <summary>
/// media 的摘要说明。
/// </summary>
public class media : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnshengcheng;
protected System.Web.UI.WebControls.ListBox ListBox1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnshengcheng.Click += new System.EventHandler(this.btnshengcheng_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnshengcheng_Click(object sender, System.EventArgs e)
{
string ConStr = "Server=(local);DataBase=ad_class;Uid=sa;Pwd=";
string prostr="select ID from Province_manager ";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand Cmd1 = new SqlCommand(prostr,Con);
SqlDataReader dr1 = Cmd1.ExecuteReader();
Hashtable hs = new Hashtable();
if(dr1.HasRows)
{
while(dr1.Read())
{
hs.Add(dr1["ID"].ToString(),dr1["ID"].ToString());
}
}
dr1.Close();
Cmd1.Dispose();
foreach(DictionaryEntry de in hs)
{
string strsql="select * from Province_manager join media_info on Province_manager.ID= media_info.Parent_province_id where Province_manager.ID= '"+de.Key.ToString()+"'";
SqlCommand Cmd = new SqlCommand(strsql,Con);
DataSet ds=new DataSet();
SqlDataReader dr = Cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while(dr.Read())
{
sb.Append("<a href=#>"+dr["Key_index"].ToString()+"</a>"+" ");
sb.Append(" ");
}
WriteFile(sb.ToString());
dr.Close();
}
Response.Write("<script>alert('静态页面生成成功!');location='media.aspx'</script>"); }
public bool WriteFile(string Key_index)
{
string OutPutPath = HttpContext.Current.Server.MapPath("province/city/media/");
Encoding encoding = Encoding.GetEncoding("gb2312");
// 读取模板文件
string ModelTemp = HttpContext.Current.Server.MapPath("ModelHTML.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(ModelTemp, encoding);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
} Random rd = new Random(unchecked((int)DateTime.Now.Ticks));
string HtmlFilename = rd.Next().ToString()+".html";
// 替换内容
str = str.Replace("ArticleContent", Key_index);
// 写文件
try
{
sw = new StreamWriter(OutPutPath+HtmlFilename, false, encoding);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
}
}
还请各位大侠帮帮忙,该怎样来写?
解决方案 »
- Microsoft Visual Studio 11 Beta 和 Microsoft SQL Server 2012
- 官网下载的discuz!NT3.5为什么只有readme和upload_files两个文件夹??该怎么用啊?
- string[i]转int
- 使用面向对象的技术创建高级 Web 应用程序
- 这样设置cookies有错吗?
- 这个问题该如何解决??????????
- 》NET程序员群。。。。。。。为了提高质量,超级懒惰的新手别加,先去搜搜资料入入门先
- VS.net WEB程序中调整网页布局问题
- 50分哦 大型网站的更多页是如何生成静态页的?首页呢?
- 怎样在Web网页中实现打印以及打印预览?
- window2003+sql2000为别的电脑不能联接到主机的数据库!!!
- net是用c写的吗?
你不是已经生成文件了吗,直接写一个sql: insert into table value ......
然后ExecuteNoQuery不久完了??
各位写下,看看该写到哪儿合适,大家仔细点看。没想象的那么简单,有点繁杂!
----------------------------------------------------------------------------
生成完一个文件后直接写入数据库!