1、建立网站,以班级 + 序号 + 姓名为名字(如 " 软件工程 1 班 1 好张三 ")来建立网站。建立数据库 test, 其中包含 stu_info 表和 goods_info 表,stu_info 表结构:(Done)
stu_name varchar(20)
stu_pwd varchar(20)
在 stu_info 表中插入一条记录("zhang","zhang")
goods_info 表结构:
goods_name varchar(50)
goods_price float
goods_date varchar(20)
goods_images varchar(100)
2、新建 login.aspx 页面,要求在此页面中用户输入用户名和密码,当用户单击 " 登录 " 按钮时,判断用户的输入与 stu_info 表中的数据是否符合,如果不符合,弹出对话框提示 " 用户名或密码错误 ", 如果用户输入正确,将用户的用户名保存到 Session, 并跳转到 goodsadmin.aspx 页 面。(Done)
[csharp]
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginBtn_Click(object sender, EventArgs e)
{
// 连接字符串
string connStr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=D:\\My Documents\\Test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
// 获取用户的输入
string username = tbUserName.Text;
string password = tbPassword.Text;
//sql 语句,用来查询
string sqlStr = "select * from stu_info where stu_name='" + username +"'";
using(SqlConnection conn = new SqlConnection(connStr))
{
conn.Open(); // 打开数据库连接
using(SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sqlStr;
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
DataTable table = ds.Tables[0];
if(table.Rows.Count <= 0)
{
Response.Write(@"<script>alert(' 用户名不存在!');</script>");
return;
}
else
{
DataRow rows = table.Rows[0];
string dbpassword =(string)rows["stu_pwd"];
if(dbpassword == password)
{
// 将用户名保存到 Session
Session["username"] =(string)rows["stu_name"];
Response.Redirect("~/goodsAdmin.aspx");
}
else
{
Response.Write(@"<script>alert(' 密码错误!');</script>");
return;
}
}
}
}
}
}
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}