三层架构三种建站方案小结

三层架构三种建站方案小结
三层架构三种建站方案小结

三层架构,MVC,EasyUI三种建站方案小结

应用环境:vs2012 和SQLite两种软件

整个解决方案的目录:

删除实体类、数据访问层、业务逻辑层中的自动产生的Class1.cs,具体方法先选择文件再右键点击删除,或者直接用DEL键删除。

创建数据库和数据表,用SQLite软件完成:

下为CarClass的主要字段

下为Menu的主要字段和举例数据

以下为https://www.360docs.net/doc/523665841.html, WEB窗体建站方法

依次为实体类、数据访问层、业务逻辑层添加 类,名称为别为CarClass.cs、CarClassDAL.cs、CarClassBLL.cs CarClass.cs具体代码如下:

using System;

using System.Collections.Generic; using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace CarManageEntity

{

public class CarClass

{

private int carId;

public int CarId

{

get { return carId; }

set { carId = value; }

}

private string carName;

public string CarName

{

get { return carName; }

set { carName =

value; } }

private float carPrice;

public float CarPrice

{

get { return carPrice; }

set { carPrice = value; } }

}

while (rs.Read()) //用while循环读取rs记录集中的数据

{

CarClass Ca = new CarClass();

Ca.CarId = rs.GetInt32(0); //读取记录集的第一个字段,类型为整型

Ca.CarName = rs.GetString(1); //读取记录集的第二个字段,类型为字符型

Ca.CarPrice = rs.GetFloat(2); //读取记录集的第三个字段,类型为单精度型

CarList.Add(Ca); //将数据写入到刚才初始化的集合中,此句很重要,要不然等会数据不能显示

}

DbHelperSqlite.close(); //关闭数据表

return CarList; //返回记录集的值

}

}

CarClassBLL.cs using System;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using CarManageDAL;

using CarManageEntity;

namespace CarManageBLL

{

public class CarClassBLL

{

CarClassDAL Car = new CarClassDAL();

public IList GetCarList()

{

return Car.GetCarList();

}

public CarClass GetCar(int CarId)

{

return Car.GetCar(CarId);

}

public int AddCar(CarClass CarClass) //除了上方的查询,其他的操作方式都是用int

{

return Car.AddCar(CarClass);

}

public int UpdateCar(CarClass CarClass)

{

return Car.UpdateCar(CarClass);

}

public int DeleteCar(int CarId)

{

return Car.DeleteCar(CarId);

}

}

}

创建https://www.360docs.net/doc/523665841.html, WEB页面化内容

先引用CarManageEntity CarManageBLL两个文件,并将其用using写入到index.aspx.cs代码中或是default.aspx.cs 代码中

1 点CarManageWeb右键→添加→新建项→WEB窗体,输入文件名为index.aspx

2 双击打开CarManageWeb找到刚才新建的index.aspx,点index.aspx左侧的三角型按钮,找到index.aspx.cs

3 双击打开index.aspx.cs输入,代码同下。

第二种方法

直接用系统默认的default.aspx,找到它并点default.aspx左侧的三角型按钮,找到default.aspx.cs,双击打开它,并输入代码,代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using CarManageBLL;

using CarManageEntity;

namespace CarManageWeb

{

public partial class_Default : Page

{

protected void Page_Load(object sender, EventArgs e)

{

CarClassBLL CBLL = new CarClassBLL();

IList CarList = CBLL.GetCarList();

// FindCarId.Text = "";

FindCarId.Focus();

//DataGridView.DataSource = rs.Tables["ck"].DefaultView;

//DataGridView.DataBind();

// DataGridView.AutoGenerateColumns.ToString();

Table1.Width = 400;

Table1.Caption = "车型列表";

Table1.BorderWidth = 0;

Table1.GridLines = GridLines.Both; //设置单元格的框线

Table1.HorizontalAlign = HorizontalAlign.Center;//设置表格相对页面居中

Table1.CellPadding = 0; //设置单元格内间距

Table1.CellSpacing = 0; //设置单元格之间的距离

int irows = CarList.Count();

int icells = 4;

for (int i = 0; i < irows + 1; i++)

{

TableRow myRow = new TableRow();

for (int j = 0; j < icells; j++)

TableCell myCell = new TableCell();

if (i == 0)

{

if (j == 0)

{

myCell.Text = "小车编号";

}

if (j == 1)

{

myCell.Text = "小车品牌";

}

if (j == 2)

{

myCell.Text = "小车价格";

}

if (j == 3)

{

myCell.Text = "操作";

}

}

else if (i > 0)

{

if (j == 0)

{

myCell.Text = CarList[i - 1].CarId.ToString();

}

if (j == 1)

{

myCell.Text = CarList[i - 1].CarName;

}

if (j == 2)

{

myCell.Text = CarList[i - 1].CarPrice.ToString();

}

if (j == 3)

{

HyperLink lyp = new HyperLink();

lyp.Text = "修改";

lyp.NavigateUrl = "UpdateCar.aspx?id=" + CarList[i - 1].CarId;

HyperLink lyp2 = new HyperLink();

lyp2.Text = "删除";

lyp2.NavigateUrl = "DeleteCar.aspx?id=" + CarList[i - 1].CarId;

myCell.Controls.Add(lyp);

myCell.Controls.Add(lyp2);

}

myRow.Cells.Add(myCell);

}

Table1.Rows.Add(myRow);

}

}

protected void FindOne(object sender, EventArgs e)

{

Table2.Width = 400;

Table2.Caption = "单个对象查找结果";

Table2.BorderWidth = 1;

Table2.GridLines = GridLines.Both; //设置单元格的框线

Table2.HorizontalAlign = HorizontalAlign.Center;//设置表格相对页面居中

Table2.CellPadding = 0; //设置单元格内间距

Table2.CellSpacing = 0; //设置单元格之间的距离

String id = FindCarId.Text;

if (!string.IsNullOrEmpty(id))

{

CarClassBLL CBLL = new CarClassBLL();

IList CarList = new List();

CarClass ca = CBLL.GetCar(int.Parse(id));

CarList.Add(ca);//将查找到的数据存储起来,要不然就不能显示了

int irows = CarList.Count();

int icells = 4;

for (int i = 0; i < irows + 1; i++)

{

TableRow myRow = new TableRow(); //定义表格的行数

for (int j = 0; j < icells; j++)

{

TableCell myCell = new TableCell(); ////定义表格的列数

if (i == 0)

{

if (j == 0)

{

myCell.Text = "小车编号";

}

if (j == 1)

{

}

if (j == 2)

{

myCell.Text = "小车价格";

}

if (j == 3)

{

myCell.Text = "操作";

}

}

else if (i > 0)

{

if (j == 0)

{

myCell.Text = CarList[i - 1].CarId.ToString();

}

if (j == 1)

{

myCell.Text = CarList[i - 1].CarName;

}

if (j == 2)

{

myCell.Text = CarList[i - 1].CarPrice.ToString();

}

if (j == 3)

{

//myCell.Text = "");

FindCarId.Text = "";

FindCarId.Focus();

}

}

}

}

另外还有对应的AddCarClass.aspx,DeleteCar.aspx,UpdateCar.aspx请到实例中查看

以下为https://www.360docs.net/doc/523665841.html, MVC4 WEB窗体建站方法

要理解如何用控件器Controllers去控制views里的显示内容

双击打开CarManageMVC→引用→右键→引用CarManageEntity和CarManageBLL两个文件

双击打开CarManageMVC→Controllers→打开HomeController.cs(这个控件的主要作用是控制views→home→中index.schtml的数据读取。)

homeController.cs主要代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using CarManageBLL;

using CarManageEntity;

namespace CarManageMVC.Controllers

{

public class HomeController : Controller

{

CarClassBLL cbll=new CarClassBLL(); //定义一个CarClassBLL类型的对象cbll。

}

return RedirectToAction("index");

}

public ActionResult Update() 本方法对应Update.cshtml,用来完成数据的修改。{

int id = int.Parse(Request["CarId"]);

return View(cbll.GetCar(id));

[HttpPost]

public ActionResult Update(CarClass Car)

{

int id = cbll.UpdateCar(Car);

return RedirectToAction("index");

}

[HttpGet]

public ActionResult Delete() //用来完成数据的删除,此方法不用视图就可以完成了。

{

int id = int.Parse(Request["CarId"]);

int ca = cbll.DeleteCar(id);

return RedirectToAction("Index");

}

public ActionResult Select() //对应数据的查找显示

{

return RedirectToAction("index");

}

}

}

Index.cshtml代码如下:

@model IEnumerable

@{

ViewBag.Title = "主页";

}

@section featured {

var str = '@ViewBag.result'

function del(CarId)

{

var id = CarId;

if (confirm("你确定删除?"))

{

window.location.href = "/home/Delete?CarId=" + id;

}

}

function sel()

{

var id = $("#CarId").val();

window.location.href = "/home/index?CarId="+id;

}

}

添加

  • 小车编号
  • 小车名称
  • 小车价格
  • 操作方式

@foreach (var a in Model)

{

}

AddCar.cshtml代码如下:

@model CarManageEntity.CarClass

ViewBag.Title = "AddCar";

}

添加车辆信息

@using (Html.BeginForm())

{

@Html.ValidationSummary(true)

小车编号

@Html.TextBoxFor(model=>model.CarId)

小车名称

@Html.TextBoxFor(model=>model.CarName)

小车价格

@Html.TextBoxFor(model=>model.CarPrice)

Select.cshtml代码如下:

@model CarManageEntity.CarClass

@{

ViewBag.Title = "Select";

}

小车信息查找

小车编号:@Html.DisplayFor(model => model.CarId)

小车名称:@Html.DisplayFor(model => model.CarName)

小车价格:@Html.DisplayFor(model => model.CarPrice)

返回

Update.cshtml代码如下:

@model CarManageEntity.CarClass

@{

ViewBag.Title = "Update";

}

Update

@using (Html.BeginForm())

{

@Html.Hidden("CarId", Model.CarId);

@Html.ValidationSummary(true)

小车编号

@Html.DisplayFor(model=>model.CarId)
小车名称

小车价格

@Html.TextBoxFor(model=>model.CarPrice)

}

用CarManageEasyui创建后台的方法

要“引用”CarManageEntity和CarManageBLL两个文件

先将事先准备好的jquery-easyui文件夹拷贝一份,双击打开CarManageEasyui找到此目录下的Scripts文件夹,右击Scripts点粘贴将jquery-easyui放在它的下方。

在CarManageEasyui的目录下,找到views 目录下的Shared文件夹并双击打开,找到_Layout.cshtml这个共用模版双击打开它,将如下5个文件放到这个共用模版中:

这个共用模版的最终代码为:

完成CarManageEntity下Menu.cs类的创建,方法是点CarManageEntity右键添加类名为Menu.cs

主要代码如下:

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading.Tasks;

namespace CarManageEntity

{

public class Menu

{

private string id; //此句为我们自己输入,下几句用ctrl+r和ctrl+e自动产生的,注意字段的第一个字母用小写public string Id

{

get { return id; }

set { id = value; }

}

private string name; //具体说明同上

public string Name

{

get { return name; }

set { name = value; }

}

private string parentId;

public string ParentId

{

get { return parentId; }

set { parentId = value; }

}

private string url;

public string Url

{

get { return url; }

set { url = value; }

}

private string iconic;

public string Iconic

{

get { return iconic; }

set { iconic = value; }

}

}

}

完成CarManageDAL下MenuDAL.cs类的创建,方法是点CarManageDAL右键添加类名为MenuDAL.cs 具体代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using Common;

using CarManageEntity;

using System.Data.SQLite;

namespace CarManageDAL

{

public class MenuDAL

{

#region IMenuDAL 成员//主要作用是为将以下所以代码折叠起来

DbHelperSqlite help = new DbHelperSqlite();

public IList GetMenuList()

{

IList

mList = new List();

string sql = "select * from Menu";

SQLiteDataReader rs = DbHelperSqlite.selete(sql);

while (rs.Read())

{

Menu Menu = new Menu();

Menu.Id = rs.GetString(0);

https://www.360docs.net/doc/523665841.html, = rs.GetString(1);

Menu.ParentId = rs.GetString(2);

Menu.Url = rs.GetString(3);

Menu.Iconic = rs.GetString(4);

mList.Add(Menu);

}

DbHelperSqlite.close();

return mList;

}

public CarManageEntity.Menu GetMenu(String Id)

{

string sql = "select * from Menu where Id=" + Id;

SQLiteDataReader dr = DbHelperSqlite.selete(sql);

Menu menu = null;

while (dr.Read())

{

menu = new Menu();

menu.Id = dr.GetString(0);

https://www.360docs.net/doc/523665841.html, = dr.GetString(1);

menu.ParentId = dr.GetString(2);

menu.Iconic = dr.GetString(4);

}

DbHelperSqlite.close();

return menu;

}

public int UpdateMenu(CarManageEntity.Menu Menu)

{

string sql = "update Menu set Name='" + https://www.360docs.net/doc/523665841.html, + "',Url='" + Menu.Url + "',Iconic='" + Menu.Iconic + "'where Id=" + Menu.Id;

int a = DbHelperSqlite.update(sql);

DbHelperSqlite.close();

return a;

}

public int AddMenu(CarManageEntity.Menu Menu)

{

string sql = "insert into Menu(Id,Name,ParentId,Url,Iconic) values(" + Menu.Id + ",'" + https://www.360docs.net/doc/523665841.html, + "','" + Menu.ParentId + "','" + Menu.Url + "','" + Menu.Iconic + "')";

int a = DbHelperSqlite.update(sql);

DbHelperSqlite.close();

return a;

}

public int DeleteMenu(string Id)

{

string sql = "delete from Menu where MenuId=" + Id;

int a = DbHelperSqlite.update(sql);

DbHelperSqlite.close();

return a;

}

#endregion

}

}

完成CarManageBLL下MenuBLL.cs类的创建,方法是点CarManageBLL右键添加类名为MenuBLL.cs 主要代码如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Threading.Tasks;

using CarManageDAL;

using CarManageEntity;

namespace CarManageBLL

{

public class MenuBLL

{

MenuDAL mnu = new MenuDAL();

///

///查询所有菜单

///

///

public IList

GetMenuList()

{

return mnu.GetMenuList();

}

///

///查询单个菜单

///

///要查询的小车编号

///

public Menu GetMenu(string Id)

{

return mnu.GetMenu(Id);

}

///

///更新小车

///

///要更新的小车

///

public int UpdateMenu(Menu Menu)

{

return mnu.UpdateMenu(Menu);

}

///

///添加小车

///

///要添加的小车

public int AddMenu(Menu Menu)

{

return mnu.AddMenu(Menu);

}

///

///删除小车

///要删除的小车编号

///

public int DeleteMenu(string Id)

{

return mnu.DeleteMenu(Id);

}

}

}

找到home文件夹双击打开index.cshtml删除原来里面的所有代码,将事先下载好的模版代码拷入,此例的模版代码如下:

汽车管理信息系统