汗。一上午才写完这一段。效率太低了。
由于听说输出DLL可以增加程序运行速度,所以把这段查看程序拆出来写了。
写到中途的时候又考虑到一点,如果我们事先不知道输出格式的话,那么程序员如何与美工很好地配合呢?
首先考虑到我们可以允许的查询有四种,依次是:
Code
public pageload(int _year,int _month)
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page where year(Page_Date)=" + _year + " and " + "month(Page_Date)=" + _month;
pageresponse();
}
public pageload(int _cath)
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page where Page_Class=" + _cath;
pageresponse();
}
public pageload(string _search)
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page where " + _search + "link Page_Text";
pageresponse();
}
public pageload()
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page";
pageresponse();
}
考虑到只需要年份以及月份查询BLOG最近更新,如果使用String 作为参数的话,重写会与_search冲突。所以就把它分拆开来用两个Int参数来代替(用if else又显得太麻烦)。
如果没有参数的话,那当然是返回所有的值;
由于重载四个创建方法考虑到在使用类时可以直接获取输出值,所以很傻地写了一个pageresponse()程序来输出值,并且在类创建时引用。
到最后才忽然想起。如果创建对象时有返回值的话,那么这个返回值怎么提取呢。汗!经验不足啊。
然后是输出函数,主要的功能是创建数据连接和输出HTML格式。
Code
public void pageresponse()
{
string strConnect = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnect += @"Data Source=" + Server.MapPath("Blog.mdb");
OleDbConnection myconn = new OleDbConnection(strConnect);
OleDbCommand mycomm = new OleDbCommand(Sql, myconn);
myconn.Open();
objDataReader = mycomm.ExecuteReader();
while (objDataReader.Read())
{
Rutext += "<div class=\"Article\">";
Rutext += "<h3 class=\"A_Title\">";
Rutext += objDataReader["Page_Title"];
Rutext += "</h3>";
Rutext += "<div class=\"A_Article\">";
Rutext += objDataReader["Page_TextAD"];
Rutext += "</div>";
Rutext += "<h4 class=\"A_Count\">";
Rutext += "Date=" + objDataReader["Page_Date"] + " | cath=" + objDataReader["Page_Class"] + "Comments=" + objDataReader["Page_Comm"];
Rutext += "</h4>";
Rutext += "</div>";
}
}
最后在引用类页面中才想起刚刚说的那一点,不得不又加了一个属性来提取值。
Code
namespace pagerespon
{
public class pageload : System.Web.UI.Page
{
private string Sql;
private OleDbDataReader objDataReader = null;
private string Rutext;
public pageload(int _year,int _month)
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page where year(Page_Date)=" + _year + " and " + "month(Page_Date)=" + _month;
pageresponse();
}
public pageload(int _cath)
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page where Page_Class=" + _cath;
pageresponse();
}
public pageload(string _search)
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page where " + _search + "link Page_Text";
pageresponse();
}
public pageload()
{
Sql = "select Page_Id,Page_Title,Page_TextAD,Page_Date,Page_Class,Page_Comm from Page";
pageresponse();
}
public void pageresponse()
{
string strConnect = "Provider=Microsoft.Jet.OleDb.4.0;";
strConnect += @"Data Source=" + Server.MapPath("Blog.mdb");
OleDbConnection myconn = new OleDbConnection(strConnect);
OleDbCommand mycomm = new OleDbCommand(Sql, myconn);
myconn.Open();
objDataReader = mycomm.ExecuteReader();
while (objDataReader.Read())
{
Rutext += "<div class=\"Article\">";
Rutext += "<h3 class=\"A_Title\">";
Rutext += objDataReader["Page_Title"];
Rutext += "</h3>";
Rutext += "<div class=\"A_Article\">";
Rutext += objDataReader["Page_TextAD"];
Rutext += "</div>";
Rutext += "<h4 class=\"A_Count\">";
Rutext += "Date=" + objDataReader["Page_Date"] + " | cath=" + objDataReader["Page_Class"] + "Comments=" + objDataReader["Page_Comm"];
Rutext += "</h4>";
Rutext += "</div>";
}
}
public string Text
{
get
{
return Rutext;
}
}
}
}
现在发现首页的代码实在太长了,而且由于在创建各个类的时候,没有考虑到整合,最终导致数据复用性太大了,页面越来越长了,还有代码没有实现很好地分离,导致日历类需要创建的过程太长了。。
最后感谢一位朋友教我的SQL提取Year及Month方法。现在网络上怎么没有一本完整的说明书呢。