实验: 用户登录
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

protected void Button1_Click(object sender, EventArgs e)//在注册中
{
Response.Redirect("userreg.aspx");//提示进入注册的界面
}
protected void Button2_Click(object sender, EventArgs e)//在登录中
{
string sqlx = @"server=.;database=xiao;UID =sa;PWD=123456";//连接数据库字串
SqlConnection conn = new SqlConnection(sqlx);//通过数据库字串连接数据库
conn.Open();//打开连接
string sqlinscmd = @"select * from xi_user where username='" + TextBox1.Text + " '";//sql命令
SqlCommand sqlcmd = new SqlCommand(sqlinscmd, conn);//让sql命令通过conn连接区执行
SqlDataReader dr = sqlcmd.ExecuteReader();//执行sql命令
if (dr.Read())//用dr Read()执行读取
{
if (dr["userpwd"].ToString() == TextBox2.Text)//当读取的用户密码等于textbox2中的内容时
{
if (dr["shibie"].ToString() == "0")//当读取的识别等于0时
{
Response.Redirect("putong.aspx");//提示进入普通用户界面
}
else if (dr["shibie"].ToString() == "1")//当读取的识别等于1时
{
Session["admin"] = "asdfghjkl";//给管理员定的固定值
Response.Redirect("guanli.aspx");//提示进入管理员界面
}
}
else//
{
Response.Write("<script>alert('密码错误')</script>");//提示
}
}
else//
{
Response.Write("<script>alert('用户不存在')</script>");//提示
}
conn.Close();//关闭数据库
}
实验: 用户注册
protected void Button1_Click(object sender, EventArgs e)//在用户注册中
{
if (TextBox1.Text != "")//
{
if (TextBox2.Text == TextBox3.Text)//
{
string sqlx = @"server=.;database=xiao;UID =sa;PWD=123456";//连接数据库字串
SqlConnection conn = new SqlConnection(sqlx);//通过数据库字串连接数据库
SqlConnection conn1 = new SqlConnection(sqlx);//通过数据库字串连接数据库
conn.Open();//打开连接
string sqlinscmd = @"select * from xi_user where username='" + TextBox1.Text + " '";
SqlCommand sqlcmd = new SqlCommand(sqlinscmd, conn);//让sql命令通过coon连接区执行
SqlDataReader dr = sqlcmd.ExecuteReader();//执行sql命令
if (dr.Read())//用dr.Read执行读取
{
conn.Close();//关闭数据库连接
Response.Write("<script>alert('用户存在')</script>");//提示
}
else//
{
conn.Close();//关闭数据库连接
conn.Open();//打开连接
string sqlinscmd1 = @"insert into xi_user values('" + TextBox1.Text + "','" + TextBox2.Text + "',0)";//sql命令
SqlCommand sqlcmd1 = new SqlCommand(sqlinscmd1, conn);//让sql命令通过coon连接区执行
sqlcmd1.ExecuteNonQuery();//执行sql命令
conn1.Close();//关闭数据库连接
Response.Write("<script>alert('用户添加成功')</script>");//提示
}
}
else//
{
Response.Write("<script>alert('两次密码不相同')</script>");//提示
}
}
else//
{
Response.Write("<script>alert('请您输入用户名')</script>");//提示
}
}
protected void Button2_Click(object sender, EventArgs e)//在检查用户中
{
string sqlx = @"server=.;database=xiao;UID =sa;PWD=123456";//连接数据库字串
SqlConnection conn = new SqlConnection(sqlx);//通过数据库字串连接数据库
conn.Open();//打开连接
string sqlinscmd = @"select * from xi_user where username='"+TextBox1.Text +" '";//sql命令
SqlCommand sqlcmd = new SqlCommand(sqlinscmd, conn);//让sql命令通过coon连接区执行
SqlDataReader dr = sqlcmd.ExecuteReader();//执行sql命令
if (dr.Read())//用dr.Read执行读取
{
Response.Write("<script>alert('用户存在')</script>");//提示
}
else//
{
Response.Write("<script>alert('用户不存在')</script>");//提示
}
conn.Close();//关闭数据库连接
}
实验: 普通用户
protected void Button1_Click(object sender, EventArgs e)//在注销中
{
//Session.Abandon();//注销 清空当前页面的信息
Response.Redirect("Default.aspx");//提示进入原来界面(登录时)
}
(按F5后出来的结果)
实验: 管理员用户
protected void Page_Load(object sender, EventArgs e)//
{
if ( Session ["admin"] == null)//当存储的信息为空时
{
Response.Redirect("Default.aspx");//提示
}
else if (Session["admin"].ToString() != "asdfghjkl")//当存储的信息不是管理员定的值时
{
Response.Redirect("Default.aspx");//提示
}
}
protected void Button1_Click(object sender, EventArgs e)//在注销中
{
//Session.Abandon();//注销清空当前页面的信息
Response.Redirect("Default.aspx");//提示进入原来界面(登录时)
}
(按F5后出来的结果)
详细的信息 请下载下面的文档
转载于:https://blog.51cto.com/wangshiming/339041