用asp.net写的论坛程序--上贴保存

  1. postmessage.aspx :- The page which saved data to the Database
1@ Import Namespace="System" 
1@ Assembly Name="System.Data" 
1@ Import Namespace="System.Data" 
1@ Import Namespace="System.Data.ADO" 
1@ Page Language="C#" Debug="true" 
  1<html>
  2<head>
  3<title>Thank You for Posting !</title>
  4<script language="C#" runat="server">   
  5//execute this script when the page loads   
  6void Page_Load(Object Src, EventArgs E)   
  7{   
  8//if the page is called from another page   
  9if (!Page.IsPostBack) {   
 10//Get all the Parameters from the Query string   
 11string name = Request.Params["name"] ;   
 12string email = Request.Params["email"] ;   
 13string subject = Request.Params["subject"] ;   
 14string ip = Request.Params["ip"] ;   
 15string date = Request.Params["date" ];   
 16string message = Request.Params["message"] ;   
 17bool newmess =true ;   
 18string previd ="1";   
 19//Check if the post is a New topic or a reply to a new topic   
 20if(Request.Params["newpost"].Equals("no"))   
 21{   
 22//if its a reply then get the postid called as previd here   
 23newmess =false ;   
 24previd = Request.Params["previd"] ;   
 25}   
 26//If the post is a new topic then follow the below routine   
 27if(newmess)   
 28{   
 29//The string for the path to the database , if your database is in some other   
 30directory then edit the path   
 31//of this variable   
 32string strConn=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=   
 33"+Server.MapPath(".\\\db\\\board.mdb") ;   
 34//Get a ADOConnection to the database   
 35ADOConnection myConn = new ADOConnection(strConn) ;   
 36//The SQL Select statement   
 37string strCom = "Select postid from newpost" ;   
 38//Create a ADOCommand since we want a ADODataReader later   
 39ADOCommand myCommand =new ADOCommand(strCom,myConn);   
 40//Open the connection   
 41myConn.Open();   
 42ADODataReader reader;   
 43//Execute the command and get the Data into "reader"   
 44myCommand.Execute(out reader) ;   
 45int i=1 ;   
 46//Get the current number of records present in the database.   
 47while(reader.Read())   
 48{   
 49i++ ;   
 50}   
 51reader.Close() ;   
 52//build the SQL statement to insert into the Database   
 53string insertStr =" INSERT INTO newpost VALUES ("   
 54+i +", '"   
 55+name+"', '"   
 56+email+"', '"   
 57+subject+"', '"   
 58+ip+"', '"   
 59+date+"', '"   
 60+message+"',0, 0)" ;   
 61myCommand.CommandText =insertStr ;   
 62//Since the SQL statement does not return any output use "ExecuteNonQuery() method   
 63myCommand.ExecuteNonQuery() ;   
 64//Close the connection   
 65myConn.Close() ;   
 66}   
 67else   
 68{   
 69//If the posted data is a reply to a topic then follow the below procedure   
 70//string for the path to the database, if your database is stored in some other directory then   
 71//edit the path here   
 72string strConn=@"Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source="+   
 73Server.MapPath(".\\\db\\\board.mdb") ;   
 74ADOConnection myConn = new ADOConnection(strConn) ;   
 75//SQL statement to select the replyid   
 76string strCom = "Select replyid from reply" ;   
 77//create a ADOCommand   
 78ADOCommand myCommand =new ADOCommand(strCom,myConn);   
 79//Open the Connection   
 80myConn.Open();   
 81ADODataReader reader;   
 82//Execute the command and get the Data into "reader"   
 83myCommand.Execute(out reader) ;   
 84int i=1 ;   
 85//Get the current number of records present in the database.   
 86while(reader.Read())   
 87{   
 88i++ ;   
 89}   
 90reader.Close() ;   
 91//Build a statement to insert the values into the reply table   
 92string insertStr =" INSERT INTO reply VALUES ("   
 93+i +", '"   
 94+name+"', '"   
 95+email+"', '"   
 96+subject+"', '"   
 97+ip+"', '"   
 98+date+"', '"   
 99+message+"', "   
100+previd+")";   
101myCommand.CommandText =insertStr ;   
102//ExecuteNonQuery - since the command does not return anything   
103myCommand.ExecuteNonQuery() ;   
104//string to get the replies column from the newpost table   
105string replyno = "SELECT replies FROM newpost WHERE postid ="+previd ;   
106myCommand.CommandText =replyno ;   
107//Execute command and get the reader   
108myCommand.Execute(out reader) ;   
109//read the first record (remember there can only be one record in the reader since postid is unique)   
110reader.Read();   
111//Get the "Int16" value of the number of replies from the replies column in the newpost table   
112int rep =reader.GetInt16(0) ;   
113reader.Close() ;   
114rep++ ;   
115//SQL statement to update the replies field in the newpost table   
116string updtStr ="UPDATE newpost SET replies = "+rep   
117+" WHERE (postid = "+previd+")" ;   
118myCommand.CommandText = updtStr;   
119//ExecuteNonQuerry why ?? I guess U should know by now !   
120myCommand.ExecuteNonQuery();   
121myConn.Close() ;   
122}   
123//get the different Parameters from the query string and store it   
124//to respective Labels   
125NameLabel.Text = name;   
126EmailLabel.Text= email ;   
127SubjectLabel.Text=subject;   
128MessageLabel.Text=message ;   
129}   
130else   
131{   
132//else display an error   
133errmess.Text="This Page Cannot be called directly. It has to be called from the Form posting page.<br>" ;   
134}   
135}   
136</script>
137<link href="mystyle.css" rel="stylesheet" type="text/css"/>
138</head>
139<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">
140<!-- #Include File="header.inc" -->
141<center>
142<asp:label id="errmess" runat="server" style="color:#FF0000" text=""></asp:label>
143<h2 class="fodark"><b>Thank You , for posting on the Message Board.</b></h2>
144<table align="center" border="0" cellpadding="1" cellspacing="2" width="60%">
145<tr class="fohead"><td colspan="2">The information You Posted!</td></tr>
146<tr class="folight">
147<td>Name :</td>
148<td><asp:label id="NameLabel" runat="server" text=""></asp:label></td>
149</tr>
150<tr class="folight">
151<td>E-Mail :</td>
152<td><asp:label id="EmailLabel" runat="server" text=""></asp:label></td>
153</tr>
154<tr class="folight">
155<td>Subject :</td>
156<td><asp:label id="SubjectLabel" runat="server" text=""></asp:label></td>
157</tr>
158<tr class="folight">
159<td>Message :</td>
160<td><asp:label id="MessageLabel" runat="server" text=""></asp:label></td>
161</tr>
162</table>
163<br/>
164<h4 class="fodark"><a href="forum.aspx">Click here </a> to go back to the Forum.<br/>   

-- A little work to show the link to return back to the page if, the post was a reply --

if(Request.Params["previd"]!=null)
{

1<a href='reply.aspx?postid=```
2=Request.Params["previd"] 
3```'> Click here </a>to go back   
4where you came from.   

}

1</h4>
2</center>
3<!-- #Include File="footer.inc" -->
4</body>
5</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus