ASP.Net课堂

2013 年 6 月 4 日3580

欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入

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)
{
}

[1]

【责编:peter】

0 0