一份静态新闻的核心代码

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace makehtmlfile
{
 /// <summary>
 /// makeallfiles 的摘要说明。
 /// </summary>
 public class makeallfiles : System.Web.UI.Page
 {
 public string strcon;
 public OleDbConnection conn;
 public string strSQL;

 private void Page_Load(object sender, System.EventArgs e)
 {
 
 InitialPages();// 在此处放置用户代码以初始化页面
 }

 public void InitialPages()
 {
 strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+"; ";//连接字符窜// 在此处放置用户代码以初始化页面
 strSQL = "select id,class1id,class2id from news order by id desc";
 MakeAreaForShow();
 ReadNewsForWriteFileUserDataReader(); //同过DataReader来读取数据,
 //ReadNewsForWriteFileUserDataSet(); //将数据直接挂入DataSet中来读取,
 }
 /// <summary>
 /// 用来产生循环显示页面的区域,装载生成HTML页的ASPX页面的区域
 /// </summary>
 public void MakeAreaForShow()
 {
 Response.Write("<span id=showImport></span>");
 Response.Write("<IE:Download ID='oDownload' STYLE='behavior:url(#default#download)'/>");
 }

 /// <summary>
 /// 通过DATAREADER来读取数据
 /// </summary>
 public void ReadNewsForWriteFileUserDataReader()
 {
 int num = 0 ;
 string newsid = null;
 string class1id = null;
 string class2id = null;
 OleDbDataReader dr = null;
 OleDbConnection conn = new OleDbConnection(strcon);
 conn.Open();
 OleDbCommand mycommand = new OleDbCommand(strSQL,conn);
 dr = mycommand.ExecuteReader();
 while(dr.Read())
 {
 newsid = dr["id"].ToString();
 class1id = dr["class1id"].ToString();
 class2id = dr["class2id"].ToString();
 WriteJScript(newsid,class1id,class2id);
 num++;
 }
 dr.Close();
 conn.Close();
 Response.Write(num.ToString());

 }

 /// <summary>
 /// 通过DATASET来读取数据
 /// </summary>
 public void ReadNewsForWriteFileUserDataSet()
 {
 DataSet ds = new DataSet();
 int num = 0 ;
 string newsid = null;
 string class1id = null;
 string class2id = null;

 OleDbConnection conn = new OleDbConnection(strcon);
 conn.Open();
 OleDbDataAdapter da = new OleDbDataAdapter(strSQL,conn);
 da.Fill(ds,"news");
 conn.Close();
 num = ds.Tables["news"].Rows.Count;
 foreach(DataRow dr in ds.Tables["news"].Rows)
 {
 newsid = dr["id"].ToString();
 class1id = dr["class1id"].ToString();
 class2id = dr["class2id"].ToString();
 WriteJScript(newsid,class1id,class2id);
 }
 ds = null;
 Response.Write(num.ToString());
 
 }

 public void WriteJScript(string newsid,string class1id,string class2id)
 {
 Response.Write("<script>");
 Response.Write("function onDownloadDone(downDate)");
 Response.Write("{");
 Response.Write("showImport.innerHTML=downDate");
 Response.Write("}");
 Response.Write("oDownload.startDownload('makefile2.aspx?id=");
 Response.Write(newsid);
 Response.Write("&class1id=");
 Response.Write(class1id);
 Response.Write("&class2id=");
 Response.Write(class2id);
 Response.Write("',onDownloadDone)");
 Response.Write("</script>");
 }

 

 #region Web 窗体设计器生成的代码
 override protected void OnInit(EventArgs e)
 {
 //
 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 //
 InitializeComponent();
 base.OnInit(e);
 }
 
 /// <summary>
 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 /// 此方法的内容。
 /// </summary>
 private void InitializeComponent()
 {
 this.Load += new System.EventHandler(this.Page_Load);

 }
 #endregion
 }
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace makehtmlfile
{
 /// <summary>
 /// makefile2 的摘要说明。
 /// </summary>
 public class makefile2 : System.Web.UI.Page
 {
 public string strcon;
 public OleDbConnection conn;
 
 public string class1id;
 public string class2id;
 //***********************************
 public string previd;
 public string prevtitle;
 public string nextid;
 public string nexttitle;
 //***********************************
 public string newstitle;
 public string newscontent;
 public string newsdate;
 public string newsip;
 public string newsid;
 //************************************

 private void Page_Load(object sender, System.EventArgs e)
 {
 strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+"; ";//连接字符窜// 在此处放置用户代码以初始化页面
 if(Request.Params["id"]!=null&&Request.Params["class1id"]!=null&&Request.Params["class2id"]!=null)
 {
 InitialPages();

 }// 在此处放置用户代码以初始化页面
 }

 public void InitialPages()
 {
 strcon = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";
 
 if(Request.Params["id"]!=null)
 {
 newsid = Request.Params["id"].ToString();
 }
 if(Request.Params["class1id"]!=null)
 {
 class1id = Request.Params["class1id"].ToString();
 }
 if(Request.Params["class2id"]!=null)
 {
 class2id = Request.Params["class2id"].ToString();
 }
 ReadDataBase(newsid,class2id);
 MakeHtmlFile(newsid,class1id,class2id);
 }

 /// <summary>
 /// 读写同一分类中,上一篇,和下一篇文章
 /// </summary>
 /// <param name="inputid"> 该文章ID</param>
 /// <param name="class2id">该文章所属分类ID</param>
 public void ReadPrevAndNext(string inputid,string class2id)
 {
 int id = int.Parse(inputid);
 string strPrevSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id<"+id+" order by id desc";
 string strNextSQL = "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id>"+id+" order by id asc";
 OleDbDataReader datar = null;
 OleDbConnection con = new OleDbConnection(strcon);
 con.Open();
 OleDbCommand newcommand = new OleDbCommand(strPrevSQL,con);
 datar = newcommand.ExecuteReader();
 while(datar.Read())
 {
 previd = datar["id"].ToString();
 prevtitle = datar["newstitle"].ToString();
 }
 datar.Close();
 newcommand.CommandText = strNextSQL ;
 datar = newcommand.ExecuteReader();
 while(datar.Read())
 {
 nextid = datar["id"].ToString();
 nexttitle = datar["newstitle"].ToString();
 }
 con.Close();

 }

 /// <summary>
 /// 将文章信息从库中读出,并将准备生成的HTML文件路径写入库中
 /// </summary>
 /// <param name="inputid"></param>
 /// <param name="class2id"></param>
 public void ReadDataBase(string inputid,string class2id)
 {
 string filename_w = MakeFileName(class1id,class2id,newsid)+".htm";
 
 ReadPrevAndNext(inputid,class2id); //读取下一篇和上一篇的信息。

 OleDbConnection mycon = new OleDbConnection(strcon); //打开数据库连接
 mycon.Open();

 int id = int.Parse(inputid);
 string strSQL = "select * from news where id="+id;
 OleDbDataReader dr = null;
 OleDbCommand mycommand = new OleDbCommand(strSQL,mycon);
 dr = mycommand.ExecuteReader();
 while(dr.Read())
 {
 newstitle = dr["newstitle"].ToString();
 newscontent = dr["newscontent"].ToString();
 newsdate = dr["newsdate"].ToString();
 newsip = dr["newsip"].ToString();
 }
 dr.Close();
 mycommand.CommandText = "update news set url='"+ filename_w +"' where id="+int.Parse(inputid); //将生成的文件路径写入库中,以遍在生成分类页中方便使用
 mycommand.ExecuteNonQuery();
 mycon.Close();
 
 }

 /// <summary>
 /// 生成目标目录和文件,主要用来生成不同分类的目录
 /// </summary>
 /// <param name="inputstr"></param>
 /// <returns></returns>
 public string MakeCatalogName(string class1,string class2) //生成目标目录文件
 {
 string namestr = "Article";
 string rootstr = Server.MapPath(".").ToString();
 string class1str = rootstr + "//" + namestr + "_" + class1 + "//";
 string class2str = rootstr + "//" + namestr + "_" + class1 + "//" + namestr + "_" + class2 + "//";
 if(!Directory.Exists(class1str))
 {
 Directory.CreateDirectory(class1str);
 }
 if(!Directory.Exists(class2str))
 {
 Directory.CreateDirectory(class2str);
 }
 //创建目标文件夹
 return class2str;
 }

 /// <summary>
 /// 根据文章分类和ID生成文件名
 /// </summary>
 /// <param name="class1id"></param>
 /// <param name="class2id"></param>
 /// <param name="nid"></param>
 /// <returns>返回文件名</returns>
 public string MakeFileName(string class1,string class2,string id) //生成文件名,能够生成上下篇
 {
 string myclass2id = class2;
 string myclass1id = class1;
 string s = DateTime.Now.Year.ToString()
 +DateTime.Now.Month.ToString()
 +DateTime.Now.Day.ToString()
 +"_"
 +myclass1id
 +"_"
 +myclass2id //父类ID
 +"_"
 +id; //新闻ID
 return s;
 }

 /// <summary>
 /// 生成HTML文件
 /// </summary>
 /// <param name="nid">文章ID号</param>
 public void MakeHtmlFile(string nid,string cla1id,string cla2id) // MakeHtmlFile(string nid,string cla1id,string cla2id,string filetemp) 用于区分不同的摸班
 {
 string file_path = Server.MapPath ("template/news_mb.htm");
 string desfilename = MakeFileName(cla1id,cla2id,nid)+".htm";
 string desfile = MakeCatalogName(cla1id,cla2id)+MakeFileName(cla1id,cla2id,nid)+".htm";
 string prevurl = MakeFileName(cla1id,cla2id,previd)+".htm"; //根据分类和ID生成上下篇的文件连接名
 string nexturl = MakeFileName(cla1id,cla2id,nextid)+".htm"; //下篇
 System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
 
 StreamReader srd = null; //读
 StreamWriter swr = null; //写
 
 string strFile = null ; //字符串
 
 try
 {
 srd = new StreamReader(file_path, code);
 strFile = srd.ReadToEnd(); // 读取文件
 }
 catch(Exception exp)
 {
 HttpContext.Current.Response.Write(exp.Message);
 HttpContext.Current.Response.End();
 srd.Close();
 }
 
 strFile = strFile.Replace("$title$",newstitle);
 strFile = strFile.Replace("$content$",newscontent);
 strFile = strFile.Replace("$date$",newsdate);
 strFile = strFile.Replace("$ip$",newsip);
 strFile = strFile.Replace("$prev$",prevtitle);
 strFile = strFile.Replace("$next$",nexttitle);
 strFile = strFile.Replace("$prevurl$",prevurl);
 strFile = strFile.Replace("$nexturl$",nexturl);
 
 try
 {
 swr = new StreamWriter(desfile,false,code);
 swr.Write(strFile);
 swr.Flush();
 }
 catch(Exception ex)
 {
 HttpContext.Current.Response.Write(ex.Message);
 HttpContext.Current.Response.End();
 }
 finally
 {
 swr.Close();
 }
 if(srd!=null)
 {
 srd.Close();
 }
 Response.Write(desfilename);

 }

 public void Open()
 {
 if(conn==null)
 {
 conn = new OleDbConnection(strcon);
 conn.Open();
 }
 }
 public void Close()
 {
 if(conn!=null)
 {
 conn.Close();
 }
 }

 #region Web 窗体设计器生成的代码
 override protected void OnInit(EventArgs e)
 {
 //
 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
 //
 InitializeComponent();
 base.OnInit(e);
 }
 
 /// <summary>
 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 /// 此方法的内容。
 /// </summary>
 private void InitializeComponent()
 {
 this.Load += new System.EventHandler(this.Page_Load);
 }
 #endregion
 }
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一份简单易懂的 C++ 教程: ## C++ 简介 C++ 是一种通用编程语言,支持面向对象、过程性和泛型编程风格。它是一种静态类型、编译式语言,被广泛用于系统/应用程序开发、游戏开发、嵌入式系统等领域。 C++ 语言最初由 Bjarne Stroustrup 在 1983 年设计,C++ 的初始版本是 C 的一个扩展,它添加了类、虚函数、重载运算符、多重继承等特性。自那时以来,C++ 已经经历了多次更新和改进,现在最新的版本是 C++20。 ## 开始学习 C++ ### 环境设置 要开始学习 C++,您需要首先设置编程环境。您需要一个 C++ 编译器和一个 IDE(集成开发环境)。以下是一些常用的 C++ 编译器和 IDE: - GCC:GNU Compiler Collection,是一个免费的、开源的编译器系统,支持多种编程语言,包括 C++。 - Clang:Clang是一个C++编译器,由苹果公司开发,它也是一个免费的、开源的编译器系统。 - Visual Studio:Microsoft开发的一个IDE,支持多种编程语言,包括 C++。 - Code::Blocks:一个免费、开源的跨平台 C++ IDE。 ### 编写你的第一个 C++ 程序 让我们从一个简单的“Hello, World!”程序开始。在你的 IDE 中创建一个新项目,并添加一个新的 C++ 源文件。将以下代码粘贴到文件中: ```c++ #include <iostream> int main() { std::cout << "Hello, World!" << std::endl; return 0; } ``` 这个程序使用了iostream库,用于在控制台输出一条消息。main()函数是程序的入口点,当程序运行时,它将被执行。 ### C++ 基础语法 #### 变量和数据类型 在 C++ 中,变量是用于存储数据值的存储区域。在声明变量时,必须指定数据类型,例如: ```c++ int age = 20; double salary = 1000.50; char grade = 'A'; ``` 上面的代码声明了三个变量:age、salary 和 grade,分别存储整数、浮点数和字符类型的数据。C++ 中支持的数据类型包括整数、浮点数、字符、布尔值、指针等。 #### 运算符 C++ 支持各种运算符,包括算术运算符、关系运算符、逻辑运算符等。例如: ```c++ int a = 10, b = 20; int sum = a + b; // 算术运算符 bool result = (a > b); // 关系运算符 bool flag = (a && b); // 逻辑运算符 ``` #### 控制语句 C++ 中的控制语句包括条件语句和循环语句。条件语句用于根据条件执行不同的代码块,例如: ```c++ if (a > b) { std::cout << "a is greater than b" << std::endl; } else { std::cout << "b is greater than a" << std::endl; } ``` 循环语句用于重复执行一段代码,例如: ```c++ for (int i = 0; i < 10; i++) { std::cout << i << std::endl; } while (a > 0) { // do something } ``` ### C++ 面向对象编程 C++ 支持面向对象编程(OOP),它是一种编程范式,用于组织代码和数据,使程序易于理解和维护。 在 C++ 中,类是一种用户定义的数据类型,它包含数据成员和成员函数。以下是一个简单的类的例子: ```c++ class Person { public: std::string name; int age; void sayHello() { std::cout << "Hello, my name is " << name << std::endl; } }; ``` 上面的代码定义了一个 Person 类,它有两个数据成员:name 和 age,和一个成员函数 sayHello(),用于打印一条问候消息。 要创建一个 Person 对象,可以使用以下代码: ```c++ Person person; person.name = "Tom"; person.age = 20; ``` 要调用成员函数,可以使用以下代码: ```c++ person.sayHello(); ``` ### C++ 标准库 C++ 标准库是一组用于处理输入/输出、字符串、日期和时间、文件等的函数和类。它包含两个主要部分: - 核心语言支持:包括数据类型、变量、运算符等。 - 标准库:包括容器、算法、迭代器、字符串、流等。 例如,要使用字符串类,可以包含头文件<string>,然后使用以下代码: ```c++ #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; std::cout << str << std::endl; return 0; } ``` 这个程序输出字符串"Hello, World!"。 ## 总结 这只是一个简单的 C++ 教程,但它应该能让您开始编写简单的程序。如果您想深入了解 C++,还有许多其他主题需要学习,例如内存管理、多线程编程、异常处理等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值