ホームページ  >  記事  >  バックエンド開発  >  C#を使って簡単な掲示板を作成する(1)

C#を使って簡単な掲示板を作成する(1)

黄舟
黄舟オリジナル
2016-12-17 17:03:161417ブラウズ

まず初めに、bigeagle さんの協力に感謝します。これも彼女の bbs を参考にして作成しました。
掲示板は、メッセージ一覧、詳細コンテンツの表示、メッセージの投稿の 3 つのモジュールに分かれています。
名前空間 notpage
{
System を使用;
System.Data.SQL を使用;
System.Data を使用 ;
System.Collections を使用します ;


////////////////////////////////////////////// /// ////////////////////
//
// クラス名: メッセージボード
//
// 説明: メッセージボードオブジェクトを構築します
//
// 日付: 2000/06/06
//
// 作成者: God
/// //////////////////////////////////////////////// /// //////////////


/// string n_strTitle; //件名
プライベート string n_strAuthor; //講師
プライベート文字列; n_strContent; //メッセージ内容
private DateTime n_dateTime; //メッセージ時間

//属性

public int ID
{
get

{

return n_intID ;
}
set
{
n_intID = value;
}
}

文字列 タイトル
{
get
{
return n_strTitle ;
}
set
{
n_strTitle = value;
}
}

文字列 著者
{
get
{
return n_strAuthor ;
}
set
{
n_strAuthor = 値 ;
}
}
パブリック文字列 コンテンツ
{
get
{
return n_strContent ;
}
set
{
n_strContent = value ;
}
}
public DateTime adddate
{

get
{
return n_dateTime;
}
set
{
n_dateTime = value;
}
}

//Constructor

public notepage()
{
//
// TODO: 追加 コンストラクターロジックはこちら
//
this.n_intID = 0 ;
this.n_strTitle = "" ;
this.n_strAuthor = "" ;
this.n_strContent = "" ;
this.n_dateTime = System.DateTime.Now;

}

///


///
/// メッセージの内容を取得します
///
///

///
公開ノートページ GetTopic(int a_intID)
{
//

// TODO: ここにコンストラクター ロジックを追加します

//



//データベースを読み取ります
myconn myConn = new myconn();

SQLCommand myCommand = 新しい 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 = (文字列)myReader["タイトル"] ;
objNp.Author = (文字列)myReader["作成者"] ;
objNp.Content = (文字列)myReader["コンテンツ"];

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: ここにコンストラクター ロジックを追加します
//

//データベースを読み取ります
myconn myConn = new myconn();

SQLCommand myCommand = 新しい 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(例外 e)
{
throw(new Exception("取贴子失败:" + e.ToString())) ;
}
return true;

}


/// <概要>
/// 取られたトピック列表
///
///
/// 返される一个トピック数组
///

public 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 ;

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["著者"].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 までご連絡ください。