一个简单的留言板的实现

1新建一个数据库BBS,新建一张表BBSItems.表设计如下:

 

 

 
2向APP_Code文件夹中添加一个BBSItem类,用于表示用户在BBS上发表的留言。
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
///
///BBSTtem 的摘要说明
///

public class BBSTtem
{
    public int BBSItemID = 0;  //主键
    private string _BBSContent = "";
    public string BBSContent   //留言内容
    {
        get { return _BBSContent ; }
        set
        {
            if (value.Length > 1000)
                _BBSContent = value.Substring(0, 1000); //诺留言超过固定长度1000,则只取前1000字符
            else
                _BBSContent = value;
        }
    }
    public DateTime SubmitTime = DateTime.Now;
    private string _NickName = "匿名";
    public string NickName
    {
        get { return _NickName; }
        set
        {
            if (value.Length > 50)
                _NickName = value.Substring(0, 50);
            else
                _NickName = value;
        }
    }
 public BBSTtem()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
       
 }
}
3 再向APP_Code中添加一个类BBSItemAccessObj类,负责从数据库中提取信息。
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.ComponentModel;
///
///BBSItemAccessObj 的摘要说明
///

public class BBSItemAccessObj
{
 public BBSItemAccessObj()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    //根据DataRow中存储的信息更新一个BBSItem对象
    private void LoadFromDataRow(BBSTtem obj, DataRow dr)
    {
        if ((obj == null) || (dr == null))
            return;
        obj.BBSItemID = dr.IsNull("BBSItemID") ? 0 : (int)dr["BBSItemID"];
        obj .BBSContent =dr.IsNull ("BBSContent")?"":dr["BBSContent"] as string ;
        obj.NickName = dr.IsNull("NickName") ? "" : dr["NickName"] as string;
        obj .SubmitTime =dr.IsNull ("SubmitTime")?DateTime .Now :(DateTime )dr["SubmitTime"];
    }
     
    //将一个BBSItem对象插入到数据库中
    public void SaveBBSItemToDB(BBSTtem obj)
    {
        if (obj == null) return;
        string strsql = "insert into BBSItems(BBSContent,NickName,SubmitTime) ";
        strsql += "values(@BBSContent,@NickName,@SubmitTime)";
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=.;Initial Catalog=BBS;Integrated Security=True";
        conn.Open();
        SqlCommand comm = new SqlCommand(strsql ,conn );
        //添加参数
        comm.Parameters.AddWithValue("@BBSContent",obj .BBSContent );
        comm.Parameters.AddWithValue("NickName", obj.NickName);
        comm.Parameters.AddWithValue("SubmitTime", obj.SubmitTime);
        comm.ExecuteNonQuery();
        conn.Close();
       
    }
}
4 Default.aspx中的代码如下
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>


    无标题页


   

   

   
   

         AutoGenerateColumns ="False"
      Width="270px"
        BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px"
        CellPadding="2" ForeColor="Black" GridLines="None"
    >
   
   
   
   

    发表于:

   
   

   

   

   

       
                    HorizontalAlign="Center" />
       
       
       
   

            ontextchanged="TextBox1_TextChanged">
   


                    ontextchanged="TextBox2_TextChanged">
   


            Text="Button" />
   


5 Default.aspx.cs中的代码如下
 using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BBSItemAccessObj accessor = new BBSItemAccessObj();
            databind();
        }
    }
  
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        BBSTtem item = new BBSTtem();
        if (txtNickName.Text.Trim().Length == 0)
            item.NickName = "匿名";
        else
            item.NickName = txtNickName.Text.Trim();
        item.BBSContent = txtContent.Text;
        item.SubmitTime = DateTime.Now;
        BBSItemAccessObj accessor = new BBSItemAccessObj();
        accessor.SaveBBSItemToDB(item);
        databind();
        txtNickName.Text = "";
        txtContent.Text = "";
    }
    void databind()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=.;Initial Catalog=BBS;Integrated Security=True";
        string strsql = "select * from BBSItems order by SubmitTime DESC";
        SqlDataAdapter ad = new SqlDataAdapter(strsql, con);
        DataSet set = new DataSet();
        con.Open();
        ad.Fill(set);
        this.GridView1.DataSource = set.Tables[0];
        this.GridView1.DataBind();
        con.Close();
    }
}
6 效果图

你可能感兴趣的:(一个简单的留言板的实现)