資源簡介
開發環境為VS2010+SQL2008,一個簡易的留言板程序,管理員可對留言用戶進行增刪改查。留言板利用Session對象記錄登錄時的用戶名,可以顯示在留言板中。

代碼片段和文件信息
using?System;
using?System.Data;
using?System.Configuration;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?System.Data.SqlClient;
using?System.Configuration;
public?partial?class?_Default?:?System.Web.UI.Page?
{
????protected?void?Page_Load(object?sender?EventArgs?e)
????{
????????this.title?=?“簡易留言板——用戶登錄“;
????????
????}
????protected?void?Login_Click(object?sender?EventArgs?e)
????{
????????if?(Username.Text?==?““?||?UserPwd.Text?==?““)
????????{
????????????Response.Write(“ript?language=javascript>alert(‘用戶名或密碼不得為空!‘); ript>“);
????????????return;
????????}
????????//設置conn對象的連接字符串
????????string?ConnSql?=?ConfigurationManager.ConnectionStrings[“ConnString“].ToString();
????????
????????//聲明conn為一個SQL?Server連接對象
????????SqlConnection?Conn?=?new?SqlConnection(ConnSql);
????????
????????Conn.Open(); //打開連接
????????//使用MD5算法加密用戶口令
????????string?SecPwd?=?FormsAuthentication.HashPasswordForStoringInConfigFile(UserPwd.Text?“MD5“);
????????string?StrinSQL?=?“select?*?from?userinfo?where?u_name=‘“?+?Username.Text?+?“‘?and?u_password=‘“?+?SecPwd?+?“‘“;
????????//聲明一個Command對象com,該對象使用conn指定的連接,執行strSQL指定的SQL語句
????????SqlCommand?SelectCom?=?new?SqlCommand(StrinSQL?Conn);
????????//執行查詢,返回一個DataReader對象
????????SqlDataReader?dr?=?SelectCom.ExecuteReader();
????????if?(!dr.Read())
????????{
????????????Response.Write(“ript?language=javascript>alert(‘用戶名或密碼錯!‘); ript>“);
????????}
????????else
????????{
????????????Session[“Pass“]?=?dr[“u_name“]; //將用戶名字段值保存到Session對象pass中
????????????Response.Redirect(“msg.aspx“);
????????}
????????dr.Close(); //關閉Reader對象
????????Conn.Close(); //關閉連接
????}
????protected?void?Register_Click(object?sender?EventArgs?e)
????{???????
????????Response.Redirect(“register.aspx“);
????}
????protected?void?Repassword_Click(object?sender?EventArgs?e)
????{
????????if?(Username.Text?==?““)
????????{
????????????Response.Write(“ript?language=javascript>alert(‘請輸入用戶名!‘); ript>“);
????????}
????????else
????????{
????????????Session[“username“]?=?Username.Text;?????//將用戶名通過Session對象username傳遞到recover.aspx
????????????Response.Redirect(“recover.aspx“);
????????}
????}
????protected?void?Memanger_Click(object?sender?EventArgs?e)
????{
????????if?(Username.Text?!=?“admin“)
????????{
????????????Response.Write(“ript?language=javascript>alert(‘請使用管理員身份登錄!‘); ript>“);
????????????return;
????????}
????????
????????string?ConnSql?=?System.Configuration.ConfigurationManager.ConnectionStrings[“ConnString“].ConnectionString;??????
????????SqlConnection?Conn?=?new?SqlConnection(ConnSql);
????????Conn.Open();
????????string?SecPwd?=?FormsAuthentication.HashPasswordForStoringInConfigFile(UserPwd.Text?“MD5“);
????????string?StrinSQL?=?“select?*?from?userinfo?where?u_name=‘admin‘?and?u_password=‘
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2009-10-14?12:15??Chapter14\
?????文件?????4194304??2009-10-14?16:11??Chapter14\guestbook.mdf
?????文件?????1048576??2009-10-14?16:11??Chapter14\guestbook_log.ldf
?????目錄???????????0??2009-10-14?12:36??Chapter14\綜合實訓\
?????目錄???????????0??2019-06-15?21:18??Chapter14\綜合實訓\App_Data\
?????文件????????2185??2009-10-14?12:22??Chapter14\綜合實訓\Default.aspx
?????文件????????3793??2009-10-14?12:33??Chapter14\綜合實訓\Default.aspx.cs
?????文件????????3361??2009-09-28?10:39??Chapter14\綜合實訓\msg.aspx
?????文件????????3111??2009-09-28?12:45??Chapter14\綜合實訓\msg.aspx.cs
?????文件????????3292??2009-09-28?18:13??Chapter14\綜合實訓\recover.aspx
?????文件????????3815??2009-09-28?13:40??Chapter14\綜合實訓\recover.aspx.cs
?????文件????????2950??2009-09-28?18:22??Chapter14\綜合實訓\register.aspx
?????文件????????4221??2009-09-28?17:59??Chapter14\綜合實訓\register.aspx.cs
?????文件????????3139??2009-09-28?14:50??Chapter14\綜合實訓\users.aspx
?????文件????????3586??2009-09-28?13:22??Chapter14\綜合實訓\users.aspx.cs
?????文件????????8417??2009-10-14?12:36??Chapter14\綜合實訓\web.config
評論
共有 條評論