ASP.NET一种多商家网络商店的设计与实现

摘 要

21世纪是网络的世纪,电子商务随之将成为主流商业模式,多商家网络商店系统就是一个C2C型的电子商务系统。本文详细论述了采用ASP.NET 2005 和 SQL Server 2000等技术实现的一个多商家网络商店的过程。论文首先阐述了本设计题目的选题意义、背景,和网站的开发平台;其次根据网站需求对系统的数据库和功能模块做了详细设计,并在此基础上实现了用户管理模块、权限管理模块、购物订单模块,商家管理模块、商品管理模块、留言板和公告管理模块等功能。最后通过对网站进行的全面测试展现了网站界面简单、美观,网站功能丰富、操作方便等特点,完全符合多商家网络商店的需求,能够为用户网上购物提供一个很好的操作平台。

关键词 **:**电子商务;多商家;网络商店;网上购物;SQL Server 2000;C#

3.1 ASP.NET介绍

ASP.NET是一个已编译的、基于.NET的环境,把基于通用语言的程序在服务器上运行。将程序在服务器端首次运行时进行编译,比ASP即时解释程序速度上要快很多,而且是可以用任何与.NET兼容的语言创作应用程序。另外,任何ASP.NET应用程序都可以使用整个.NET Framework。开发人员可以方便地获得这些技术的优点,其中包括托管的公共语言运行库环境、类型安全、继承等等。

3.2 C#介绍

C#是一种面向对象的编程语言,它作为Visual Studio中的一部分推出。C#(发音为"C-Sharp")既保持了C++中熟悉的语法,并且还包含了大量的高效代码和面向对象特性。C#语言在保持C/C++灵活性的基础上为程序员带来更高效的RAD开发方式。它不仅能用于WEB服务程序的开发,并且还能开发强大的系统级程序。

3.3 SQL Server 2000介绍

Microsoft SQL Server 2000是Microsoft公司继SQL Server 6.5和SQL Server 7.0以后,在新千年推出的又一改进的新版关系型数据管理系统。它能使用户快捷地管理数据库和开发应用程序。SQL Server 2000使用了先进的数据库结构,与Windows DNA紧密集成,具有强大的Web功能,它可以利用高端硬件平台以及最新网络和存储技术,可以为最大的Web站点和企业应用提供优良的扩展性和可靠性,使用户能够在Internet领域快速建立服务系统,为占领市场赢得宝贵的时间。同时,SQL Server 2000还为用户提供了重要的安全性功能的增强,为用户的数据安全提供了可靠的保证。另外,SQL Server 2000在数据库服务器自动管理技术方面处于数据库领域的领先地位,它可以使用户免去繁琐复杂的工作量,从而有精力处理更重要的问题,使用系统在商业战略上占得先机。

4.2 系统功能分析

1.浏览功能,一般游客或注册用户都可以浏览及查询商品,商店。

2.用户注册及登录功能,用户需注册帐号后才能登录及进行其他相应操作。

3.用户修改资料、购买商品以及查询订单功能。

4.商家添加删除商品、管理商品、查询卖出商品、修改店铺资料及查看买家留言等功能。

5.管理员对用户、商家、商品的管理功能

6.管理员对商品分类的添加、删除功能。

7.管理员发布修改公告及推荐商品。

4.3 系统模块分析

4.3.1 后台管理模块分析

此模块只对系统管理员开放。管理员可以对用户、商家、商品,以及公告推荐商品进行管理。

4.3.2 前台管理模块分析

此模块对普通用户及卖家开放。又分为买家模块和买家模块:

  1. 买家模块。买家可以对注册资料进行修改对订单进行查看。
  2. 卖家模块。卖家在买家的基础上还可以对商品和商店进行管理,以及对买家下的订单和买家留言进行查看。

6.1 用户浏览及搜索商品

所有用户及游客最先进入的是网站的首页,它包括推荐商品、商店排行等内容,也包括了商品的搜索功能。首页界面如图6-1所示:

导航栏上面关于用户登录注册部分会根据用户登录与否显示不同的信息,其代码如下:

public string gettop()

{

string htmlstr;

if (Session["webusername"] == null)//跟踪用户名,判断是否登录

{ htmlstr = "<td width='200' align='center'><span class='top1'>你好,欢迎来到零点商城!</span></td> <td width='70'><a href='user/register.aspx' class='top2'>[免费注册]</a></td> <td width='45'><a href='user/login.aspx' class='top2'>[登录]</a></td>"; }

else

{ htmlstr = "<td width='245' align='right'><span class='top1'>欢迎您," + Session["webusername"] + "</span></td> <td width='70'align='center'><a href='login_out.aspx' class='top2'>[退出]</a></td>"; }

return htmlstr;//返回htmlstr的值

}

如登录后界面如图6-2所示,未登录界面如图6-3所示。

显示最新商品代码如下:

public static string getnewgoods()

{

string sql = "select top 4 * from web_goods where goods_audit=1 order by goods_id desc";//查找出的结果按商品ID的降序排列,即最后添加的最先显示。

IDataReader drnew = common.GetDataReader(sql); //从数据库读出最新添加的商品

string ret = "";

int i = 1;

string goodsname = "";

while (drnew.Read())

{

if (drnew ["goods_name"].ToString().Length< 8) //判断商品名称长度,若小于8则直接将值赋给proname,若不是则赋给前八个字

{

goodsname = drnew ["goods_name"].ToString();

}

else

{

goodsname = drnew ["goods_name"].ToString().Substring(0, 8);

}

if (i != 3)

{

ret += "<td ><table height='150' border='0' align='center' cellpadding='0' cellspacing='0'><tr><td height='130' align='center'><a href='goods.aspx?id=" + drnew["goods_id"].ToString() + "' class='top1' target='_blank'><img src='upload/" + drnew["goods_img"].ToString() + "' width='120' height='120' border='0'></a></td></tr><tr><td height='20' align='center'><a href='goods.aspx?id=" + drnew["goods_id"].ToString() + "' class='top1' target='_blank'>" + goodsname + "</a></td> </tr> <tr> <td align='center'><span class='top2'>¥" + WebCommon.common.strFormatmoney(drnew["goods_price"].ToString()) + "</span></td> </tr> </table></td>";

}

else

{

ret += "</tr><tr><td ><table height='150' border='0' align='center' cellpadding='0' cellspacing='0'><tr><td height='130' align='center'><a href='goods.aspx?id=" + drnew["goods_id"].ToString() + "' class='top1' target='_blank'><img src='upload/" + drnew["goods_img"].ToString() + "' width='120' height='120' border='0'></a></td></tr><tr><td height='20' align='center'><a href='goods.aspx?id=" + drnew["goods_id"].ToString() + "' class='top1' target='_blank'>" + goodsname + "</a></td> </tr> <tr> <td align='center'><span class='top2'>¥" + WebCommon.common.strFormatmoney(drnew["goods_price"].ToString()) + "</span></td> </tr> </table></td>";

}

i++;

} //从数据库读出数据并显示于页面

drnew.Close();//关闭数据库

return ret;

}

其中:

public static string strFormatmoney(string str)

{

str = str.Replace(".0000", ".00");

return str;

} //格式化货币

public static IDataReader GetDataReader(string sql)

{

Database db = DatabaseFactory.CreateDatabase();

IDataReader dr = db.ExecuteReader(CommandType.Text, sql);

return dr;

}//返回DataReader

6.2 用户管理页面

在用户管理的首页,我们可以看到用户的详细资料,界面如图6-4所示(此图为有商家权限用户的界面):

代码如下:

public string main()

{

string sql = "select * from web_user where user_name='" + Session["webusername"] + "'";

IDataReader dr = common.GetDataReader(sql);

dr.Read();

Label1.Text = dr["user_name"].ToString();

Label2.Text = dr["user_email"].ToString();

Label3.Text = dr["user_truename"].ToString();

Label4.Text = dr["user_idcard"].ToString();

Label5.Text = dr["user_mobile"].ToString();

Label6.Text = dr["user_tel"].ToString();

Label7.Text = dr["area_class1"].ToString();

Label8.Text = dr["area_class2"].ToString();

Label9.Text = dr["user_address"].ToString();

Label10.Text = dr["user_postalcode"].ToString();

Label11.Text = dr["user_date"].ToString();

int s = int.Parse(dr["shop_id"].ToString());

string str;

if (s == 0)

{

str = "<tr><td height='30' class='top1'>您还未注册为卖家,想要卖商品请点击左边的<span class='top2'>成为卖家</span></td></tr>";

}

else {

str = "<tr><td height='30' class='top1'>您已经是卖家了哦,想要卖商品请点击左边的<span class='top2'>添加商品</span></td></tr>";

}

return str;

dr.Close();

}

卖家添加商品首先选择商品分类,其界面如图6-5所示:

代码如下:

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

if (Session["webusername"] == null) //判断Session值是否为空,如果为空则跳到登录页面

{

Response.Redirect("user/login.aspx");

}

init2();

ListBox2.Enabled = true;// 实例化并启动一个新的工作线程,稍后将添加工作线程本身的功能

ListBox3.Enabled = false;//终止该工作线程

Button1.Enabled = false;

}

}

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)//通过ListBox1的动作显示ListBox2的内容

{

string sql = "select * from web_goods_class2 where goods_class1='" + ListBox1.SelectedValue + "'";

ListBox2.Enabled = true;

ListBox3.Enabled = false;

Button1.Enabled = false;

ListBox2.DataSource = common.GetDataReader(sql);

ListBox2.DataTextField = "goods_class2";

ListBox2.DataValueField = "goods_class2";

ListBox2.DataBind();

initlabel();//显示Label1中的内容,即所选择的添加商品的分类

}

public void init2()//初始化ListBox1

{

string sql = "select * from web_goods_class1 order by goods_class1_id";

ListBox1.DataSource = common.GetDataReader(sql);

ListBox1.DataTextField = "goods_class1";

ListBox1.DataValueField = "goods_class1";

ListBox1.DataBind();

}

protected void ListBox2_SelectedIndexChanged(object sender, EventArgs e)

{

string sql = "select * from web_goods_class3 where goods_class2='" + ListBox2.SelectedValue + "'";

ListBox3.Enabled = true;

Button1.Enabled = false;

ListBox3.DataSource = common.GetDataReader(sql);

ListBox3.DataTextField = "goods_class3";

ListBox3.DataValueField = "goods_class3";

ListBox3.DataBind();

initlabel();

}

protected void ListBox3_SelectedIndexChanged(object sender, EventArgs e)

{

Button1.Enabled = true;

initlabel();

}

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("addgoods.aspx?type=" + ListBox3.SelectedValue);//进入商品详细资料添加页面

}

public void initlabel()

{

string strlistbox1 = "";

string strlistbox2 = "";

string strlistbox3 = "";

if (ListBox2.SelectedValue != "")

{

strlistbox2 = " > " + ListBox2.SelectedValue;

}

if (ListBox3.SelectedValue != "")

{

strlistbox3 = " > " + ListBox3.SelectedValue;

}

Label1.Text = ListBox1.SelectedValue + strlistbox2 + strlistbox3;

}

商品详细资料添加界面如图6-6所示:

代码如下:

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

if (Session["webusername"] == null)

{

Response.Redirect("user/login.aspx");//返回错误信息

}

else if (Request["type"] == null)

{

Response.Redirect("sell.aspx");

}

Init2();//初始化界面

}

}

protected void BtnSave_Click(object sender, EventArgs e)

{

if (checkRequest() == false)

{

return;

}

try

{

string nam = FileUpload1.PostedFile.FileName;

string upload3 = "img1.jpg";

if (FileUpload1.HasFile == true)

{

int i = nam.LastIndexOf(".");//取得文件名(包括路径)里最后一个"."的索引

string newext = nam.Substring(i).ToLower();//取得文件扩展名

if (newext == ".gif" || newext == ".jpg" || newext == ".jpeg" || newext == ".bmp")

{

string savePath1 = Server.MapPath("~/upload/");

string savePathsmall = Server.MapPath("~/smallupload/");//生成缩略图

savePath1 += FileUpload1.PostedFile.ContentLength.ToString() + newext;

savePathsmall += FileUpload1.PostedFile.ContentLength.ToString() + newext;

FileUpload1.SaveAs(savePathsmall);

WebCommon.common.MakeSLT(savePathsmall, savePath1);

upload3 = FileUpload1.PostedFile.ContentLength.ToString() + newext;

}

else

{

Response.Write("<script>alert('你上传的图片格式不对,正确的图片格式为gif,jpg,jpeg,bmp!');</script>");

return;

}

}

string image = upload3;//图片

string goodsdes = WebCommon.common.strFormat(TextBox1.Text);//产品详细信息

string username = Session["webusername"].ToString();//商家登陆名

string goodsdate = DateTime.Now.ToShortDateString();

string sql = "insert into web_goods(goods_name,goods_price,goods_tranprice,goods_img,goods_description,goods_startdate,goods_enddate,goods_date,goods_class3,user_name,goods_brandtype,goods_audit,goods_vip)values('" + Txtgoodsname.Text + "','" + Txtgoodsprice.Text + "','" + Txtgoodstranprice.Text + "','" + image + "','" + goodsdes + "','" + Txtgoodsstartdate.SelectedValue + "','" + Txtgoodsenddate.SelectedValue + "','" + goodsdate + "','" + Label22.Text + "','" + username + "','" + TxtBand.Text + "','0','0')";

if (common.ExecuteSql(sql) == "1")

{

Response.Write("<script>alert('你添加的信息已经成功提交。');</script>");

TextBox1.Text = "";

Txtgoodsname.Text = "";

string sql2 = "update web_shop set shop_goods_number=shop_goods_number+1 where user_name=" + username;

common.ExecuteSql(sql2);

}

else

{

Response.Write("<script>alert('抱歉,添加信息错误,请检查信息是否正确。');</script>");

}

}

catch

{

Response.Write("<script>alert('抱歉,添加信息错误,请检查信息是否正确。');</script>");

}

}

private void Init2()

{

//商品类型初始化

Label22.Text = Request["type"].ToString();

//商品出售开始时间初始化

//商品出售结束时间初始化

DateTime t = DateTime.Now;

Txtgoodsenddate.Items.Add(t.AddDays(40).ToShortDateString());

for (int i = 0; i < 40; i++)

{

Txtgoodsstartdate.Items.Add(t.AddDays(i).ToShortDateString());

Txtgoodsenddate.Items.Add(t.AddDays(i).ToShortDateString());

}

}

protected void Button1_Click(object sender, EventArgs e)

{

Response.Redirect("sell.aspx");//跳转到修改商品分类页面

}

public bool checkRequest()

{

if (Txtgoodsname.Text == "")

{

Response.Write("<script>alert('请输入商品名称');</script>");

return false;

}

else if (Txtgoodsprice.Text == "")

{

Response.Write("<script>alert('请输入商品价格');</script>");

return false;

}

else if (Txtgoodstranprice.Text == "")

{

Response.Write("<script>alert('请输入商品运费');</script>");

return false;

}

else if (TextBox1.Text == "")

{

Response.Write("<script>alert('请输入产品描述');</script>");

return false;

}

else

{

return true;

}

}

订单查询页面(以下是商家部分,买家类似),界面如图6-7所示:

代码如下:

protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)

{

DataGrid1.CurrentPageIndex = e.NewPageIndex;

string sql = "select * from web_list where sell='" + Session["webusername"].ToString() + "' order by list_date desc";

DataGrid1.DataSource = common.GetDataSet(sql);

DataGrid1.DataKeyField = "list_id";

DataGrid1.DataBind();

}

其中:

public static DataSet GetDataSet(string sql)

{

Database db = DatabaseFactory.CreateDatabase();

DataSet ds= db.ExecuteDataSet(CommandType.Text, sql);

return ds;

}

6.3 购买商品

购买商品页面部分代码如下:

protected void Button1_Click(object sender, EventArgs e)

{

string sql="select * from web_goods where goods_id='"+Request["id"]+"'";

IDataReader dr = common.GetDataReader(sql);

dr.Read();

int gnum = int.Parse(dr["goods_number"].ToString());//将字符串转换成int型数据,方便下面的比较

int bnum=int.Parse(this.Txtgoodsnumber.Text);

if (bnum < 0 || bnum > gnum)//判断商品购买数量是否超出范围

{

Response.Write("<script>alert('您输入的商品数量不正确');</script>");

}

else {

string strdate = DateTime.Now.ToShortDateString();

string str = "等待发货";

string sql = "insert into web_list(buy,sell,goods_id,goods_name,goods_number,list_price,list_date,list_condition)values('" + Session["webusername"].ToString + "','" + dr["user_name"].ToString + "','" + dr["goods_id"].ToString() + "','" + dr["goods_name"].ToString() + "','" + this.Txtgoodsnumber.Text + "'," + this.Txtallprice.Text + ",'" + strdate + "','" + str + "')";

common.ExecuteSql(sql);//将数据存入数据库

Response.Write("<script>alert('您已订购成功,请等待卖家联系'); document.location='Default.aspx';</script>");//返回商品订购成功信息,并跳转到首页

}

dr.close();

}

购买商品界面如图6-8所示:

相关推荐
追逐时光者6 小时前
推荐 12 款开源美观、简单易用的 WPF UI 控件库,让 WPF 应用界面焕然一新!
后端·.net
Jagger_6 小时前
敏捷开发流程-精简版
前端·后端
苏打水com6 小时前
数据库进阶实战:从性能优化到分布式架构的核心突破
数据库·后端
间彧7 小时前
Spring Cloud Gateway与Kong或Nginx等API网关相比有哪些优劣势?
后端
间彧7 小时前
如何基于Spring Cloud Gateway实现灰度发布的具体配置示例?
后端
间彧7 小时前
在实际项目中如何设计一个高可用的Spring Cloud Gateway集群?
后端
间彧7 小时前
如何为Spring Cloud Gateway配置具体的负载均衡策略?
后端
间彧8 小时前
Spring Cloud Gateway详解与应用实战
后端
EnCi Zheng9 小时前
SpringBoot 配置文件完全指南-从入门到精通
java·spring boot·后端
烙印6019 小时前
Spring容器的心脏:深度解析refresh()方法(上)
java·后端·spring