First of all, I would like to thank bigeagle for his help. This is also made with reference to her bbs.
The message board is divided into three modules: list message list, display detailed content, and post message
notepage.cs
namespace
notpage
{
using System;
using System.Data.SQL;
using System.Data
;
using System.Collections ;
//////////////////////////////////////////////////// ////////////////////
//
//
Class Name: Message Board
//
// Description:
Construct a message board object
//
// date: 2000/06/06
//
// Author: God
///
///////////////////////////////////////////////////// //////////////
///
private string n_strAuthor; //Lecturer
private string
n_strContent; //Message content
//Attributes
public int ID
{
{
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;
}
//Constructor
public notepage()
{
//
// TODO: Add
Constructor Logic here
//
this.n_intID = 0 ;
this.n_strTitle = ""
;
this.n_strAuthor = "" ;
this.n_strContent = "" ;
this.n_dateTime =
System.DateTime.Now;
}
///
///
/// Get the content of the message
///
///
///
public notepage GetTopic(int a_intID)
{
// TODO:
Add Constructor Logic here
//
//Read database
myconn myConn = new
myconn();
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection =
myConn ;
myCommand.CommandText = "n_GetTopicInfo" ; //Call the stored procedure
myCommand.CommandType =
CommandType.StoredProcedure;
myCommand.Parameters["@a_intTopicID"].Value = a_intID ;
notepage objNp = new notepage();
try
{
myConn.Open() ;
SQLDataReader myReader ;
myCommand.Execute(out
myReader) ;
if (myReader.Read())
{
objNp.ID = (int)myReader["ID"]
;
objNp.Title = (string)myReader["Title"] ;
objNp.Author =
(string)myReader["Author"] ;
objNp.adddate =
(DateTime)myReader["adddate"];
}
//Clear up
myReader.Close();
myConn.Close() ;
}
catch(Exception e)
{
throw(new Exception("Failed to fetch post:" + e.ToString()))
;
}
return objNp;
}
///
///
/// Purpose: to store the content of the message into the database
///
/// Use constructors to pass information
///
///
///
{
//
// TODO: Add Constructor Logic here
//
//Read database
myconn myConn = new myconn();
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection =
myConn ;
myCommand.CommandText = "n_addTopic" ;
//调用存储过程
myCommand.CommandType = CommandType.StoredProcedure
;
myCommand.Parameters.Add(new SQLParameter("@a_strTitle" ,
SQLDataType.VarChar,100)) ;
myCommand.Parameters["@a_strTitle"].Value =
n_Topic.Title ;
myCommand.Parameters.Add(new
SQLParameter("@a_strAuthor" , SQLDataType.VarChar,50))
;
myCommand.Parameters["@a_strAuthor"].Value = n_Topic.Author
;
myCommand.Parameters.Add(new SQLParameter("@a_strContent" ,
SQLDataType.VarChar,2000)) ;
myCommand.Parameters["@a_strContent"].Value =
n_Topic.Content ;
try
{
myConn.Open() ;
myCommand.ExecuteNonQuery() ;
//清场
myConn.Close() ;
}
catch(Exception e)
{
throw(new
Exception("取贴子失败:" + e.ToString())) ;
}
return true;
}
///
///
取的贴子列表
///
///
/// 返回一个Topic数组
///
public ArrayList
GetTopicList()
{
//定义一个forum数组做为返回值
ArrayList arrForumList =new
ArrayList() ;
//从数据库中读取留言列表
myconn myConn = new
myconn();
SQLCommand myCommand = new SQLCommand()
;
myCommand.ActiveConnection = myConn ;
myCommand.CommandText =
"n_GetTopicList" ; //调用存储过程
myCommand.CommandType =
CommandType.StoredProcedure ;
try
{
myConn.Open()
;
SQLDataReader myReader ;
myCommand.Execute(out myReader)
;
for (int i = 0 ; myReader.Read() ; i++)
{
notepage
objItem = new notepage() ;
objItem.ID = myReader["ID"].ToString().ToInt32()
;
objItem.Title = myReader["Title"].ToString() ;
objItem.Author =
myReader["Author"].ToString() ;
objItem.adddate =
myReader["adddate"].ToString().ToDateTime();
objItem.Content =
myReader["Content"].ToString();
arrForumList.Add(objItem) ;
}
//清场
myReader.Close();
myConn.Close() ;
}
catch(SQLException e)
{
throw(new Exception("数据库出错:" + e.ToString()))
;
//return null ;
}
return arrForumList ;
}
}
}
以上就是利用c#制作简单的留言板(1)的内容,更多相关文章请关注PHP中文网(www.php.cn)!