ASP.NET体系及编程大作业
网上购书系统
姓名:刘涛
学号:2220111654
专业:网络工程(2)班
一、实验目的
1. 熟练掌握ASP.NET服务器控件、ASP.NET内置对象以及ADO.NET数据库访问技术及其应用;
2. 掌握使用ASP.NET开发Web应用程序的基本方法和基本技能。
二、实验内容
1. 实现一个网上书店。管理员将图书信息整理归类发布到网上,用户登录该网站后,首先要注册为会员才能购买图书。该系统的使用者主要分为3类:浏览者、注册用户和管理员。浏览者可以浏览网上书店中的图书信息;注册用户除了可以浏览网上书店中的图书信息外,还可以进行网上购书;管理员可以对网上书店中的图书信息进行管理、对客户订单进行处理等。主要包括以下功能模块:
前台:
(1)用户注册登录;
(2)书籍查询;
(3)书籍订购;
(4)订单查询;
(5)用户信息修改;
(6)书籍评论。
后台:
(1)书籍类别管理;
(2)书籍信息管理;
(3)订单管理;
(4)用户管理;
(5)评论管理。
2. 系统流程
3. 数据库设计
Admi表
Comment表
Book表
Orders表
成员表
4.主要模块的实现
主页面
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class home : System.Web.UI.Page
{
public SqlConnection Getconn()
{
string constr = ConfigurationManager.AppSettings["connstring"].ToString();
SqlConnection myconn = new SqlConnection(constr);
return myconn;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["mname"] == null)
{
cart.Visible = false;
}
else {
login.Visible = false;
signup.Visible = false;
cart.Visible = true;
userpage.Visible = true;
exit.Visible = true;
Label1.Text="欢?迎®-" + Session["mname"].ToString();
SqlConnection myconn = Getconn();
myconn.Open();
string strSQL = "select mid from members where mname='"
+ Session["mname"].ToString() + "'";
SqlCommand cmd = new SqlCommand(strSQL, myconn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
string mid = dr[0].ToString();
Session["mid"] = mid;
dr.Close();
myconn.Close();
}
}
protected void login_Click(object sender, EventArgs e)
{
Response.Redirect("login.aspx");
}
protected void signup_Click(object sender, EventArgs e)
{
Response.Redirect("regisiter.aspx");
}
protected void exit_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("home.aspx");
}
protected void userpage_Click(object sender, EventArgs e)
{
Response.Redirect("userpage.aspx");
}
protected void search_Click(object sender, EventArgs e)
{
Session.Timeout = 30;
Session["search"] = sBox.Text.ToString();
Server.Transfer("search.aspx");
}
protected void cart_Click(object sender, EventArgs e)
{
Response.Redirect("cart.aspx");
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("detail.aspx?bid=11");
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("detail.aspx?bid=12");
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("detail.aspx?bid=15");
}
}
登录
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class login : System.Web.UI.Page
{
public SqlConnection Getconn()
{
string constr = ConfigurationManager.AppSettings["connstring"].ToString();
SqlConnection myconn = new SqlConnection(constr);
return myconn;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void tz_Click(object sender, EventArgs e)
{
Response.Redirect("regisiter.aspx");
}
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection myconn = Getconn();
myconn.Open();
string strSQL = "select count(*) as xcount from members where mname='"
+ mname.Text+"'"
+" and mpwd='"+mpwd.Text.Trim() +"'";
SqlCommand cmd = new SqlCommand(strSQL,myconn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
string Count = dr["xCount"].ToString();
dr.Close();
myconn.Close();
if (Count != "0")
{
myconn.Open();
string strSQL1 = "select * from members where mname='"
+ mname.Text + "'"
+ " and mpwd='" + mpwd.Text.Trim() + "'";
SqlCommand cmd1 = new SqlCommand(strSQL1, myconn);
SqlDataReader dr1 = cmd1.ExecuteReader();
dr1.Read();
string mid = dr1["mid"].ToString();
dr1.Close();
myconn.Close();
Session.Timeout = 60;
Session["mid"] = mid;
Session["mname"] = mname.Text.ToString();
Server.Transfer("home.aspx");
}
else {
Response.Write("<script language='javascript'>alert('用户名密码错误');</script>");
mname.Text = "";
mpwd.Text = "";
}
}
protected void search_Click(object sender, EventArgs e)
{
Session.Timeout = 30;
Session["search"] = sBox.Text.ToString();
Server.Transfer("search.aspx");
}
}
注册
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class regisiter : System.Web.UI.Page
{ public SqlConnection Getconn(){
string constr = ConfigurationManager.AppSettings["connstring"].ToString();
SqlConnection myconn= new SqlConnection(constr);
return myconn;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void search_Click(object sender, EventArgs e)
{
Session.Timeout = 30;
Session["search"] = sBox.Text.ToString();
Server.Transfer("search.aspx");
}
protected void sign_Click(object sender, EventArgs e)
{
SqlConnection myconn = Getconn();
myconn.Open();
string strSQL="insert into members (mname,mpwd,email,mobile) values('";
strSQL+= mname.Text.ToString() + "','";
strSQL+= mpwd.Text.ToString()+"','";
strSQL+= email.Text.ToString()+"',' ";
strSQL+= mobile.Text.ToString()+" ')";
SqlCommand cmd = new SqlCommand(strSQL, myconn);
if (cmd.ExecuteNonQuery() > 0)
{
Session.Timeout = 60;
Session["mname"] = mname.Text.ToString();
Response.Write("<script>alert('添加成功')</script>");
mname.Text = "";
mpwd.Text = "";
mpwdconfirm.Text = "";
email.Text = "";
mobile.Text = "";
Server.Transfer("home.aspx");
}
else {
Response.Write("添加失败");
}
myconn.Close();
}
protected void tz_Click(object sender, EventArgs e)
{
Response.Redirect("login.aspx");
}
}
购物车
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class cart : System.Web.UI.Page
{
public SqlConnection Getconn()
{
string constr = ConfigurationManager.AppSettings["connstring"].ToString();
SqlConnection myconn = new SqlConnection(constr);
return myconn;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["mname"] == null) {
Response.Redirect("login.aspx") ;
}
else
{
exit.Visible = true;
Label1.Text = "欢迎" + Session["mname"].ToString();
}
SqlConnection myconn = Getconn();
myconn.Open();
string mid = Session["mid"].ToString();
string strSQL = "select bprice from cart where mid='" + mid + "'";
SqlCommand cmd = new SqlCommand(strSQL, myconn);
SqlDataReader dr = cmd.ExecuteReader();
float aaa = 0;
while (dr.Read())
{
string bprice = dr["bprice"].ToString();
aaa += float.Parse(bprice);
}
myconn.Close();
L2.Text = "您选购图书的总价为:" + aaa.ToString() + "<br/>";
}
protected void search_Click(object sender, EventArgs e)
{
Session.Timeout = 30;
Session["search"] = sBox.Text.ToString();
Server.Transfer("search.aspx");
}
protected void cart1_Click(object sender, EventArgs e)
{
Response.Redirect("cart.aspx");
}
protected void home_Click(object sender, EventArgs e)
{
Response.Redirect("home.aspx");
}
protected void exit_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("home.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("order.aspx");
}
}
管理员
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class admin : System.Web.UI.Page
{
public SqlConnection Getconn()
{
string constr = ConfigurationManager.AppSettings["connstring"].ToString();
SqlConnection myconn = new SqlConnection(constr);
return myconn;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LoginButton_Click(object sender, EventArgs e)
{
SqlConnection myconn = Getconn();
myconn.Open();
string strSQL = "select count(*) as xcount from admin where aname='"
+ aname.Text + "'"
+ " and apwd='" + apwd.Text.Trim() + "'";
SqlCommand cmd = new SqlCommand(strSQL, myconn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
string Count = dr["xCount"].ToString();
dr.Close();
myconn.Close();
if (Count != "0")
{
myconn.Open();
string strSQL1 = "select * from admin where aname='"
+ aname.Text + "'"
+ " and apwd='" + apwd.Text.Trim() + "'";
SqlCommand cmd1 = new SqlCommand(strSQL1, myconn);
SqlDataReader dr1 = cmd1.ExecuteReader();
dr1.Read();
string aid = dr1["aid"].ToString();
dr1.Close();
myconn.Close();
Session.Timeout = 60;
Session["aid"] = aid;
Session["aname"] = aname.Text.ToString();
Server.Transfer("bookmanage.aspx");
}
else
{
Response.Write("<script language='javascript'>alert('用户名或密码错误');</script>");
aname.Text = "";
apwd.Text = "";
}
}
}
书籍管理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class admin_admincomment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["aname"] == null)
{
Response.Redirect("admin.aspx");
}
else
{
exit.Visible = true;
Label1.Text = "欢迎管理员" + Session["aname"].ToString();
}
}
protected void exit_Click(object sender, EventArgs e)
{
Session.Abandon();
Response.Redirect("admin.aspx");
}
}
另外还有用户管理,订单管理,和评论管理等。
三.实验体会
1.通过一学期的学习,掌握了许多ASP的编程方法。
2.刚开始看到大作业时完全不知道该怎么做,后来经过看书和看课件慢慢有了思路,但是在构建数据库的时候出了一些问题,问了几个同学以后得到了解决。
3.在连接数据库的时候总是出错,后来把SQLserver换成了2008版本的就好了。至于这是为什么到现在还不知道。
4.再后来编写代码的时候总是出小问题,例如在用GridView的时候数源的配置有问题,再用DetailsView的时候数据库里面的表没有设置主键,就不成功。诸如此类的小问题简直是层出不穷。
5.老师检查的时候说界面太简单,由于不会做界面设计所以找了同学帮忙。