c#

位置:IT落伍者 >> c# >> 浏览文章

利用C#制作简单的留言板的方法


发布日期:2019年10月08日
 
利用C#制作简单的留言板的方法
留言板分三个模块列出留言列表显示详细内容发表留言

notepagecs

namespace notpage

{

using System;

using SystemDataSQL ;

using SystemData ;

using SystemCollections ;

////////////////////////////////////////////////////////////////////

//

// Class Name : 留言板

//

// Description: 构造一个留言板对象

//

// date: //

//

// 作者 天啦

/// ////////////////////////////////////////////////////////////////

///

/// Summary description for notepage

///

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

{

&nbs

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

//

thisn_intID = ;

thisn_strTitle = ;

thisn_strAuthor = ;

thisn_strContent = ;

thisn_dateTime = SystemDateTimeNow;

}

///

///

/// 取得留言的内容

///

///

///

public notepage GetTopic(int a_intID)

{

//

// TODO: Add Constructor Logic here

//

//读取数据库

myconn myConn = new myconn()

SQLCommand myCommand = new SQLCommand() ;

myCommandActiveConnection = myConn ;

myCommandCommandText = n_GetTopicInfo ; //调用

存储过程

myCommandCommandType = CommandTypeStoredProcedure ;

myCommandParametersAdd(new SQLParameter(@a_intTopicID SQLDataTypeInt)) ;

myCommandParameters[@a_intTopicID]Value = a_intID ;

notepage objNp = new notepage()

try

{

myConnOpen() ;

SQLDataReader myReader ;

myCommandExecute(out myReader) ;

if (myReaderRead())

{

objNpID = (int)myReader[ID] ;

objNpTitle = (string)myReader[Title] ;

objNpAuthor = (string)myReader[Author] ;

objNpContent = (string)myReader[Content];

objNpadddate = (DateTime)myReader[adddate];

}

//清场

myReaderClose()

myConnClose() ;

}

catch(Exception e)

{

throw(new Exception(取贴子失败 + eToString())) ;

}

return objNp;

}

///

///

/// 目的将留言的内容入库

///

/// 利用构造函数来传递信息

///

///

///

public bool AddTopic(notepage n_Topic)

{

//

// TODO: Add Constructor Logic here

//

//读取数据库

myconn myConn = new myconn()

SQLCommand myCommand = new SQLCommand() ;

myCommandActiveConnection = myConn ;

myCommand

CommandText = n_addTopic ; //调用存储过程

myCommandCommandType = CommandTypeStoredProcedure ;

myCommandParametersAdd(new SQLParameter(@a_strTitle SQLDataTypeVarChar)) ;

myCommandParameters[@a_strTitle]Value = n_TopicTitle ;

myCommandParametersAdd(new SQLParameter(@a_strAuthor SQLDataTypeVarChar)) ;

myCommandParameters[@a_strAuthor]Value = n_TopicAuthor ;

myCommandParametersAdd(new SQLParameter(@a_strContent SQLDataTypeVarChar)) ;

myCommandParameters[@a_strContent]Value = n_TopicContent ;

try

{

myConnOpen() ;

myCommandExecuteNonQuery() ;

//清场

myConnClose() ;

}

catch(Exception e)

{

throw(new Exception(取贴子失败 + eToString())) ;

}

return true;

}

///

/// 取的贴子列表

///

///

/// 返回一个Topic数组

///

public ArrayList GetTopicList()

{

//定义一个forum数组做为返回值

ArrayList arrForumList =new ArrayList() ;

//从数据库中读取留言列表

myconn myConn = new myconn()

SQLCommand myCommand = new SQLCommand() ;

myCommandActiveConnection = myConn ;

myCommandCommandText = n_GetTopicList ; //调用存储过程

myCommandCommandType = CommandTypeStoredProcedure ;

try

{

 

; myConnOpen() ;

SQLDataReader myReader ;

myCommandExecute(out myReader) ;

for (int i = ; myReaderRead() ; i++)

{

notepage objItem = new notepage() ;

objItemID = myReader[ID]ToString()ToInt() ;

objItemTitle = myReader[Title]ToString() ;

objItemAuthor = myReader[Author]ToString() ;

objItemadddate = myReader[adddate]ToString()ToDateTime()

objItemContent = myReader[Content]ToString()

arrForumListAdd(objItem) ;

}

//清场

myReaderClose()

myConnClose() ;

}

catch(SQLException e)

{

throw(new Exception(数据库出错 + eToString())) ;

//return null ;

}

return arrForumList ;

}

}

}

myconncs

namespace notpage

{

using System;

using SystemDataSQL ;

///

/// Summary description for myconn

///

public class myconn:SystemDataSQLSQLConnection

{

private void InitializeComponent ()

{

}

public myconn()

{

//

// TODO: Add Constructor Logic here

//

thisDatabase = back ;

thisDataSource = LUOCHANG ;

thisUserID = sa ;

thisPas

sword = ;

}

}

}

添加留言addTopicaspx

<%@ Page language=c# Codebehind=AddTopiccs AutoEventWireup=false Inherits=notpageAddTopic %>

<html><head>

<meta content=Microsoft Visual Studio name=GENERATOR>

<meta content=C# name=CODE_LANGUAGE></head>

<body>

<form method=post runat=server>

<table cellSpacing= cellPadding= width=% border=>

<tr>

<td>留言主题</TD>

<td><asp:textbox id=txtTitle runat=server maxlength= columns=></asp:textbox></TD></TR>

<tr>

<td>姓名</TD>

<td><asp:textbox id=txtAuthor runat=server maxlength= columns=></asp:textbox></TD></TR>

<tr>

<td>留言内容</TD>

<td><asp:textbox id=txtContent runat=server maxlength= columns= rows= TextMode=MultiLine></asp:textbox><asp:button

id=btnSubmit runat=Server

text=确认></asp:button></TD></TR></TABLE></FORM>

</body></html></P><P>对应的cs

namespace notpage

{

using System;

using SystemCollections;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemWeb;

using SystemWebSessionState;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemWebUIHtmlControls;

///

/// Summary description for AddTopic

///

public class AddTopic : SystemWebUIPage

{

protected SystemWebUIWebControlsTextBox txtContent;

protected SystemWebUIWebControlsTextBox txtAuthor;

protected SystemWebUIWebControlsTextBox txtTitle;

protected SystemWebUIWebControlsButton btnSubmit;

public AddTopic()

{

PageInit += new SystemEventHandler(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()

}

///

/// Required method for Designer support do not modify

/// the contents of this method with the code editor

///

private void InitializeComponent()

{

btnSubmitClick += new SystemEventHandler (thisOnSubmit)

thisLoad += new SystemEventHandler (thisPage_Load)

}

public void OnSubmit(Object sender EventArgs e)

{

if (PageIsValid)

{

//数据入库

try

{

notepage objNp = new notepage()

objNpTitle = txtTitleText;

objNpAuthor = txtAuthorText;

objNpContent = txtContentText;

objNpadddate = SystemDateTimeNow;

notepage objNp = new notepage()

if(objNpAddTopic(objNp))

{

ResponseWrite (

成功留言点击此处查看留言列表!

) ;

}

}

catch(Exception exp)

{

#if DEBUG

ResponseWrite (出现异常 + expMessage) ;

return ;

#endif//DEBUG

}

}

}

}

}

显示列表listaspx

<%@ Page language=c# Codebehind=listcs AutoEventWireup=false Inherits=notpagelist %>

<html><head>

<meta name=GENERATOR Content=Microsoft Visual Studio >

<meta name=CODE_LANGUAGE Content=C#></head>

&nbs

p; <body>

<form method=post runat=server></P><P> </form><a href = addTopicaspx>发表留言</a><br>

<TABLE WIDTH=% HEIGHT= BORDER= CELLSPACING= CELLPADDING= id=liuyan>

<TR>

<TD>主题</TD>

<TD>留言人</TD>

<TD>留言时间</TD>

</TR>

<asp:label id=n_tdtitle runat=Server></asp:label>

</TABLE></P><P> </body></html>

namespace notpage

{

using System;

using SystemCollections;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemWeb;

using SystemWebSessionState;

using SystemWebUI;

using SystemWebUIWebControls;

using SystemWebUIHtmlControls;

///

/// Summary description for list

///

public class list : SystemWebUIPage

{

protected SystemWebUIWebControlsLabel n_tdtitle;

public list()

{

PageInit += new SystemEventHandler(Page_Init)

}

protected void Page_Load(object sender EventArgs e)

{

if (!IsPostBack)

{

//

// Evals true first time browser hits the page

//

&n

bsp; }

}

protected void Page_Init(object sender EventArgs e)

{

//

// CODEGEN: This call is required by the ASP+ Windows Form Designer

//

InitializeComponent()

Init_tdtitle()

}

///

/// Required method for Designer support do not modify

/// the contents of this method with the code editor

///

private void InitializeComponent()

{

thisLoad += new SystemEventHandler (thisPage_Load)

}

protected void Init_tdtitle()

{

InitializeComponent()

//

// CODEGEN: This call is required by the ASP+ Windows Form Designer

//

try

{

notepage np = new notepage()

ArrayList arrTopic = npGetTopicList()

for ( int i = ; i < arrTopicCount ; i ++)

{

notepage objTopic = (notepage)arrTopic[i] ;

string str = + objTopicTitleToString() + ;

str = str + + objTopicAuthorToString()+;

str = str + + objTopicadddateToString()+;

n_tdtitleText = str + n_tdtitleText;

}

}

catch(Exception e)

{

throw(new Exception(取得贴子列表出错 + eToString())) ;

}

}

}

}

查看留言内容showtopicaspx

<%@ Page language=c# Codebehind=showTopiccs AutoEventWireup=false Inherits=notpageshowTopic %>

<html><head>

<meta content=Microsoft Visual Studio name=GENERATOR>

<meta content=C# name=CODE_LANGUAGE></head>

<body>

<form method=post runat=server>

<p align=center><font color=red><b>察看留言</b></font></p><br>

<p 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

namespace notpage

{

using System;

using SystemCollections;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemWeb;

using SystemWebSessionState;

using SystemWebUI;

&nbs

               

上一篇:c#通过网线读取三菱PLC数据

下一篇:C#中IList与List区别