资源简介
开发环境为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
评论
共有 条评论