首頁  >  文章  >  後端開發  >  利用c#製作簡單的留言板(1)

利用c#製作簡單的留言板(1)

黄舟
黄舟原創
2016-12-17 17:03:161417瀏覽

首先要感謝bigeagle的幫助,這也是參考她的bbs做成的
留言板分三個模組:列出留言清單、顯示詳細內容、發表留言
notepage.cs
namespace notpage
{
using System;
using System.Data.SQL ;
using System.Data ;
using System.Collections ;


////////////////////////////////////////////////// ///////////////////
//
// Class Name : 留言板
//
// Description: 建構一個留言板物件
//
// date: 2000/06/06
//
// 作者: 天啦
/// ////////////////////////////////////////////////// //////////////


///


/// Summary description for notepage.
///

public class notepage
{
//私有變數

PRivate int nprivate 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: Add Constructor Logic here
//
this.n_intID = 0 ;
this.n_strTitle = "" ;
this.n_strAuthor = "" ;
this.n_strContent = "" ;
this.n_dateTime = System.DateTime.Now;

}

///

///
/// 取得留言的內容
///
///


///
public notepage GetTopic(int a_intID)
{
//
// TODO: Add Constructor Logic here
//

//讀取資料庫
myconn myConn = new myconn();

SQLCommand myCommand = new SQLCommand() ;

myCommand.ActiveConnection = myConn ;
myCommand.CommandText = "n_GetTopicInfo" ; //呼叫預存程序
myCommand.CommandType = CommandType.StoredProcedure ;
myCommand.Parameters.Add(new SQLParameter("@a_intTopicID" , SQLDataType.Int)) ;
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.Content = (string)myReader["Content"];
objNp.adddate = (DateTime)myReader["adddate"];
}


//清場
myReader.Close();
myConn.Close() ;

}
catch(Exception e)
{
throw(new Exception("取貼失敗:" + e.ToString())) ;
}
return objNp;

}

///

///
/// 目的:將留言的內容入庫
///
/// 利用建構子來傳遞訊息
///
///


///
public bool AddTopic(notepage n_Topic)
{
//
// TODO: Add Constructor Logic here
//

//讀取資料庫

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(新 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 ;

試試
{

myConn.Open() ;
myCommand.ExecuteNonQuery() ;

//清場

myConn.Close() ;

}
catch(異常e)
{
拋出(新 Exception("取貼失敗:" + e.ToString())) ;
}
return true;

}


///
/ // 取的貼文清單
/// 摘要>
///
/// 回傳一個主題備份
/// 備註>
公共ArrayList GetTopicList()
{
//定義一個論壇批次做為回傳值
ArrayList arrForumList =new ArrayList() ;

//從資料庫中讀取留言清單
myconn myConn = new myconn();
SQLCommand myCommand = new SQLCommand() ;
myCommand.ActiveConnection = myConn ;
myCommand.CommandText = “n_GetTopicList”; //呼叫預存程序
myCommand.CommandType = CommandType.StoredProcedure ;

嘗試
{
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())) ;
//返回null ;
}

return arrForumList ;
}

}
}

 以上就是利用c#製作簡單的留言板(1)的內容,更多相關文章請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn