留言板分三个模块:列出留言列表、显示详细内容、发表留言 notepage.cs using System; using System.Data; using System.Collections; using System.Data.SqlClient; using System.Data.Sql; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Web.Configuration; /// <summary> /// notepage 的摘要说明 /// </summary> public class notepage { private int n_intID; //ID编号 private string n_strTitle; //主题 private string n_strAuthor; //留言人 private string n_strContent; //留言内容 private DateTime n_dateTime; //留言时间 //属性 public int ID { get { return n_intID; } set { n_intID = value; } } public string Title { get { return n_strTitle; } set { n_strTitle = value; } } public string Author { get { return n_strAuthor; } set { n_strAuthor = value; } } public string Content { get { return n_strContent; } set { n_strContent = value; } } public DateTime adddate { get { return n_dateTime; } set { n_dateTime = value; } } public notepage() { // // TODO: 在此处添加构造函数逻辑 // this.n_intID = 0; this.n_strTitle = ""; this.n_strAuthor = ""; this.n_strContent = ""; this.n_dateTime = System.DateTime.Today; } /// <summary> /// /// 取得留言的内容 /// /// </summary> /// <param name="a_intID"> </param> public notepage GetTopic(int n_intID) { // // TODO: Add Constructor Logic here // //读取数据库 string connectionString = ConfigurationManager.ConnectionStrings["back"].ConnectionString; SqlConnection myConn = new SqlConnection(connectionString); SqlCommand myCommand = new SqlCommand("n_GetTopicInfo", myConn);//调用存储过程 myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add(new SqlParameter("@ID",SqlDbType.Int)); myCommand.Parameters["@ID"].Value = n_intID; notepage objNp = new notepage(); try { myConn.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); if (myReader.Read()) { objNp.ID = Convert.ToInt32(myReader["ID"].ToString()); objNp.Title = myReader["Title"].ToString(); objNp.Author = myReader["Author"].ToString(); objNp.Content = myReader["Content"].ToString(); objNp.adddate = (DateTime)myReader["adddate"]; } //清场 myReader.Close(); myConn.Close(); } catch (Exception e) { throw (new Exception("取贴子失败:" + e.ToString())); } return objNp; } /// <summary> /// /// 目的:将留言的内容入库 /// /// 利用构造函数来传递信息 /// /// </summary> /// <param name="n_Topic"> </param> public bool AddTopic(notepage n_Topic) { // // TODO: Add Constructor Logic here // //读取数据库 string connectionString = ConfigurationManager.ConnectionStrings["back"].ConnectionString; SqlConnection myConn = new SqlConnection(connectionString); SqlCommand myCommand = new SqlCommand("n_addTopic", myConn);//调用存储过程 myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add(new SqlParameter("@Title", SqlDbType.VarChar, 100)); myCommand.Parameters["@Title"].Value = n_Topic.Title; myCommand.Parameters.Add(new SqlParameter("@Author", SqlDbType.VarChar, 50)); myCommand.Parameters["@Author"].Value = n_Topic.Author; myCommand.Parameters.Add(new SqlParameter("@Content", SqlDbType.VarChar, 2000)); myCommand.Parameters["@Content"].Value = n_Topic.Content; myCommand.Parameters.Add(new SqlParameter("@adddate", SqlDbType.DateTime, 8)); myCommand.Parameters["@adddate"].Value = n_Topic.adddate; try { myConn.Open(); myCommand.ExecuteNonQuery(); //清场 myConn.Close(); } catch (Exception e) { throw (new Exception("取贴子失败:" + e.ToString())); } return true; } /// <summary> /// 取的贴子列表 /// </summary> /// <remarks> /// 返回一个Topic数组 /// </remarks> public ArrayList GetTopicList() { //定义一个forum数组做为返回值 ArrayList arrForumList = new ArrayList(); //从数据库中读取留言列表 string connectionString = ConfigurationManager.ConnectionStrings["back"].ConnectionString; SqlConnection myConn = new SqlConnection(connectionString); SqlCommand myCommand = new SqlCommand("n_GetTopicList", myConn);//调用存储过程 myCommand.CommandType = CommandType.StoredProcedure; try { myConn.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); for (int i = 0; myReader.Read(); i++) { notepage objItem = new notepage(); objItem.ID = Convert.ToInt32(myReader["ID"].ToString()); objItem.Title = myReader["Title"].ToString(); objItem.Author = myReader["Author"].ToString(); objItem.adddate = (DateTime)myReader["adddate"]; arrForumList.Add(objItem); } //清场 myReader.Close(); myConn.Close(); } catch (SqlException e) { throw (new Exception("数据库出错:" + e.ToString())); //return null ; } return arrForumList; } } 添加留言addTopic.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddTopic.aspx.cs" Inherits="AddTopic" %> <!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 runat="server"> <meta content="Microsoft Visual Studio 7.0" name="GENERATOR" /> <meta content="C#" name="CODE_LANGUAGE" /> <title>留言发表</title> </head> <body> <form id="Form1" method="post" runat="server"> <table cellspacing="1" cellpadding="1" width="88%" border="0"> <tr> <td>主题:</td> <td style="width: 574px"><asp:textbox id="txtTitle" runat="server" maxlength="80" columns="65"></asp:textbox></td></tr> <tr> <td>姓名:</td> <td style="width: 574px"><asp:textbox id="txtAuthor" runat="server" maxlength="40" columns="20"></asp:textbox></td></tr> <tr> <td>内容</td> <td style="width: 574px"><asp:textbox id="txtContent" runat="server" maxlength="2000" columns="50" rows="20" TextMode="MultiLine"></asp:textbox><asp:button id="btnSubmit" runat="Server" text="确认" ></asp:button></td></tr></table></form> </body> </html> 对应的cs 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.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class AddTopic : System.Web.UI.Page { public AddTopic() { Page.Init += new System.EventHandler(Page_Init); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // // Evals true first time browser hits the page // } } protected void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP+ Windows Form Designer. // InitializeComponent(); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { btnSubmit.Click += new System.EventHandler (this.OnSubmit); this.Load += new System.EventHandler (this.Page_Load); } public void OnSubmit(Object sender, EventArgs e) { if (Page.IsValid) { //数据入库 try { notepage objNp = new notepage(); objNp.Title = txtTitle.Text; objNp.Author = txtAuthor.Text; objNp.Content = txtContent.Text; objNp.adddate = System.DateTime.Now; notepage objNp1 = new notepage(); //if (objNp1.AddTopic(objNp)) if (txtTitle.Text == "" || txtAuthor.Text == "" || txtContent.Text == "") { if (txtTitle.Text == "") { //MessageBox.Show("标题不能为空", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); Response.Write("<p align=center class=cn>标题不能为空!</p>"); txtTitle.Focus(); return; } if (txtAuthor.Text == "") { //MessageBox.Show("作者不能为空", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); Response.Write("<p align=center class=cn>作者不能为空!</p>"); txtAuthor.Focus(); return; } if (txtContent.Text == "") { //MessageBox.Show("内容不能为空", "警告提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error); Response.Write("<p align=center class=cn>内容不能为空!</p>"); txtContent.Focus(); return; } } else if (objNp1.AddTopic(objNp)) { txtTitle.Text = ""; txtAuthor.Text = ""; txtContent.Text = ""; Response.Write("<p align=center class=cn>成功留言,点击<a href = list.aspx>此处</a>查看留言列表!。</p>"); } } catch (Exception exp) { #if DEBUG Response.Write ("出现异常:" + exp.Message) ; return ; #endif//DEBUG } } } } 显示列表list.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="List.aspx.cs" Inherits="List" %> <!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 runat="server"> <title>留言列表</title> </head> <body> <form id="Form1" method="post" runat="server"></form><a href = "AddTopic.aspx">发表留言</a><br /> <table width="90%" border="0" cellspacing="2" cellpadding="2" id="liuyan"> <tr> <td style="height: 22px">主题</td> <td style="height: 22px">留言人</td> <td style="height: 22px">留言时间</td> </tr> <asp:label id="n_tdtitle" runat="Server"></asp:label></table> </body> </html> using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class List : System.Web.UI.Page { public List() { Page.Init += new System.EventHandler(Page_Init); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // // Evals true first time browser hits the page // } } protected void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP+ Windows Form Designer. // InitializeComponent(); Init_tdtitle(); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } protected void Init_tdtitle() { InitializeComponent(); // // CODEGEN: This call is required by the ASP+ Windows Form Designer. // try { notepage np = new notepage(); ArrayList arrTopic = np.GetTopicList(); for (int i = 0; i < arrTopic.Count; i++) { notepage objTopic = (notepage)arrTopic[i]; string str = "<tr><td><a href = showTopic.aspx?id=" + objTopic.ID.ToString() + ">" + objTopic.Title.ToString() + "</a></td>"; str = str + "<td>" + objTopic.Author.ToString() + "</td>"; str = str + "<td>" + objTopic.adddate.ToString() + "</td></tr>"; n_tdtitle.Text = str + n_tdtitle.Text; } } catch (Exception e) { throw (new Exception("取得贴子列表出错:" + e.ToString())); } } } 查看留言内容showtopic.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="showtopic.aspx.cs" Inherits="showtopic" %> <!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 runat="server"> <title>无标题页</title> </head> <body> <form id="Form1" method="post" runat="server"> <p style="text-align:center" mce_style="text-align:center"><font color="red"><b>察看留言</b></font></p><br /> <p style="text-align:left" mce_style="text-align:left"><font color="blue">留言主题:<asp:label id="n_tdtitle" runat="Server" forecolor="Black"></asp:label> <br />留言时间:<asp:label id="n_tdAdddate" runat="Server" forecolor="Black"></asp:label><br /></font><font color="blue">留言人: <asp:label id="n_tdAuthor" runat="server" forecolor="Black"></asp:label><br />留言内容:<asp:label id="n_tdContent" runat="Server" forecolor="Black"></asp:label></font></p></form> </body> </html> 对应的cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class showtopic : System.Web.UI.Page { protected System.Web.UI.WebControls.Label td; protected System.Web.UI.WebControls.Label n_ttitle; public showtopic() { Page.Init += new System.EventHandler(Page_Init); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // // Evals true first time browser hits the page // } } protected void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP+ Windows Form Designer. // int int_ID; int_ID = Convert.ToInt32(Request.QueryString["ID"]); notepage np = new notepage(); notepage objNp = np.GetTopic(int_ID); n_tdtitle.Text = objNp.Title.ToString(); n_tdContent.Text = objNp.Content.ToString(); n_tdAuthor.Text = objNp.Author.ToString(); n_tdAdddate.Text = objNp.adddate.ToString(); InitializeComponent(); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } } 数据库 一、 创建一个数据库 打开SQL SERVER查询分析器,创建一个data数据库,该数据库的主数据文件的逻辑名称是data,操作系统文件是data.mdf,大小是15MB,最大是30MB,以20%的速度增加;该数据库的日志文件的逻辑名称是data_log,操作系统文件是data.ldf,大小是3MB,最大是10MB,以1MB的速度增加。 Create Database data --创建数据库data On Primary ( Name=data, Filename='d:/data.mdf', --数据库操作系统文件的目录和名称 Size=15MB, Maxsize=30MB, Filegrowth=20%) Log On (Name=data_log, Filename='d:/data.ldf', Size=3MB, Maxsize=10MB, FileGrowth=1MB) USE data --打开数据库 二、 创建一个表 Drop Table back --如果已经有此表将其删除,第一次创建,不用这句! GO Create Table back --创建表back ( ID int Primary Key Identity (1, 1) Not Null, --字段ID ,主关键字,自动累加,初值为1,自动加1,不能为空 Title varchar(100) Not Null, --字段Title 留言标题,类型varchar 大小100,不能为空 Author varchar(50) Not Null, –留言作者 Content varchar(2000) Not Null, --字段Content --留言内容 adddate DateTime ) 三、创建三个存储过程 CREATE Procedure n_addTopic @Title varChar(100),@Author varChar(50),@Content varchar(2000),@adddate datetime--定义参数变量 AS Insert into back (Title,Author,Content,adddate) values(@Title,@Author,@Content,@adddate) --执行语句 GO CREATE Procedure n_GetTopicInfo @ID int AS select * from back where @ID = ID GO CREATE Procedure n_GetTopicList AS select * from back GO Web.config中添加的代码段。用于数据库的连接 <connectionStrings> <add name="back" connectionString="Server=SERVER;user id=sa;password=; Integrated Security=True;Database=data;Persist Security Info=True" providerName="System.Data.SqlClient" /> </connectionStrings>