News system core code for generating static files (. NET C #) (ii)

Source: Internet
Author: User
Tags datetime httpcontext tostring
static | News System

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>
Summary description of the 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 "]) +"; ";/ /connection character channeling//place user code here to initialize page
if (request.params["id"]!=null&&request.params["class1id"]!=null&&request.params["Class2id"]!= Null
{
Initialpages ();

}//Place user code here to initialize the page
}

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>
Read and write the same category, the previous article, and the next article
</summary>
<param name= "inputID" > The article id</param>
<param name= "Class2id" > The article belongs to the classification 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 + "' D id< "+id+" ORDER by id DESC ";
String strnextsql = "SELECT top 1 id,newstitle,newsdate from news where class2id= '" + class2id + "' D 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>
Read the article information out of the library and write the HTML file path to be generated in storage
</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); Read the next and previous information.


OleDbConnection mycon = new OleDbConnection (Strcon); Open a database connection
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); Writes the generated file path to the library for easy use in the build category page
mycommand. ExecuteNonQuery ();
Mycon. Close ();

}


<summary>
Generate target directories and files, used primarily to generate catalogs of different categories
</summary>
<param name= "Inputstr" ></param>
<returns></returns>
public string Makecatalogname (string class1,string class2)//Generate destination directory file
{
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);
}
Create destination Folder
return class2str;
}

<summary>
Generate file names based on article classification and ID
</summary>
<param name= "Class1id" ></param>
<param name= "Class2id" ></param>
<param name= "Nid" ></param>
<returns> return file name </returns>
public string Makefilename (string class1,string class2,string ID)//Generate file name to generate context
{
string myclass2id = Class2;
string myclass1id = Class1;
string s = DateTime.Now.Year.ToString ()
+datetime.now.month.tostring ()
+datetime.now.day.tostring ()
+"_"
+myclass1id
+"_"
+MYCLASS2ID//Parent class ID
+"_"
+id; News ID
return s;
}

<summary>
Generate HTML file
</summary>
<param name= "Nid" > Article ID number </param>
public void Makehtmlfile (string nid,string cla1id,string cla2id)//Makehtmlfile (String nid,string C La1id,string cla2id,string filetemp) used to differentiate between different touch classes
{
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"; Generate a file connection name for the context based on category and ID
String nexturl = Makefilename (Cla1id,cla2id,nextid) + ". htm"; Next article
System.Text.Encoding code = System.Text.Encoding.GetEncoding ("gb2312");

StreamReader SRD = null; Read
StreamWriter SWR = null; Write

string strfile = null; String

Try
{
SRD = new StreamReader (File_path, code);
strfile = SRD. ReadToEnd (); Reading files
}
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 ();
   }
  }

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.