1、0简单三层结构操作说明第一节 入门 1第二节 数据列表显示 12第三节 数据详细显示 23第四节 数据增加 29第五节 数据修改 36第六节 数据删除 45第一节 入门1. 创建项目框架Model实体模型层 DAL数据访问层 BLL业务逻辑成 Web表示层2. 添加项目之间引用Dal 引用 model ;bll引用model 、dal;web引用model、bll准备数据访问类DbHelperSQL 弹出框类MessageBox3. 编写model层以admin为例。创建Admin类using System;namespace Model / / Admin:实体类(属性说明自动提取数据库字段
2、的描述信息) / Serializable public partial class Admin public Admin() #region Model private int _adminid; private string _loginid; private string _loginpwd; private string _adminname; private bool _sex; / / / public int adminID set _adminid=value; getreturn _adminid; / / / public string LoginID set _login
3、id=value; getreturn _loginid; / / / public string LoginPWD set _loginpwd=value; getreturn _loginpwd; / / / public string AdminName set _adminname=value; getreturn _adminname; / / / public bool sex set _sex=value; getreturn _sex; #endregion Model 4. 编写数据访问层 DAL创建AdminDAL类, 编写数据库访问代码。准备好 DbHelperSQLus
4、ing System;using System.Data;using System.Text;using System.Data.SqlClient;using Model;namespace DAL / / 数据访问类:Admin / public class AdminDAL public AdminDAL() / / 查询一个对象实体 / public Admin GetModel(string name, string pwd) StringBuilder strSql=new StringBuilder(); strSql.Append(select top 1 ); strSql.
5、Append( adminID,LoginID,LoginPWD,AdminName,sex ); strSql.Append( from Admin ); strSql.Append( where LoginID= + name + and LoginPWD= + pwd + ); Admin model=new Admin(); DataSet ds=DbHelperSQL.Query(strSql.ToString(); if(ds.Tables0.Rows.Count0) return DataRowToModel(ds.Tables0.Rows0); else return null
6、; / / 得到一个对象实体 / public Admin DataRowToModel(DataRow row) Admin model = new Admin(); if (row != null) if (rowadminID != null & rowadminID.ToString() != ) model.adminID = int.Parse(rowadminID.ToString(); if (rowLoginID != null) model.LoginID = rowLoginID.ToString(); if (rowLoginPWD != null) model.Log
7、inPWD = rowLoginPWD.ToString(); if (rowAdminName != null) model.AdminName = rowAdminName.ToString(); if (rowsex != null & rowsex.ToString() != ) if (rowsex.ToString() = 1) | (rowsex.ToString().ToLower() = true) model.sex = true; else model.sex = false; return model; 5. 编写业务逻辑层 BLL创建 AdminBLL类using S
8、ystem;using System.Data;using System.Collections.Generic;using DAL;using Model;namespace BLL / / Admin / public class AdminBLL private readonly AdminDAL dal=new AdminDAL(); public AdminBLL() / / 得到一个对象实体 / public Admin GetModel(string name, string pwd) return dal.GetModel(name, pwd); 6. 编写表示层WEB创建lo
9、gin.aspx页面.进行登录验证(0)web.config 添加连接字符串 (1)创建页面。 . 使用本地帐户登录。 用户名 密码 类型 管理员 学生 (2)编写代码using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using BLL;using Model;namespace Web public partial class Login : System.Web.UI.Page
10、 protected void Page_Load(object sender, EventArgs e) protected void submit_Click(object sender, EventArgs e) string usertype = DropDownList1.SelectedValue; string name = UserName.Text.Trim(); string pwd = Password.Text.Trim(); if (usertype = 1)/管理员 AdminBLL bll=new AdminBLL(); Admin model = bll.Get
11、Model(name,pwd); if (model != null) Response.Redirect(welcome.aspx); else FailureText.Text = Invalid username or password.; ErrorMessage.Visible = true; else if (usertype = 2)/学生 /* studentBLL bll = new studentBLL(); student model = bll.GetModel(name, pwd); if (model != null) Response.Redirect(welco
12、me.aspx); else FailureText.Text = Invalid username or password.; ErrorMessage.Visible = true; */ 第二节 数据列表显示1.dal层 / / 获得数据列表 / public DataSet GetList(string strWhere) StringBuilder strSql = new StringBuilder(); strSql.Append(select adminID,LoginID,LoginPWD,AdminName,sex ); strSql.Append( FROM Admin
13、); if (strWhere.Trim() != ) strSql.Append( where + strWhere); return DbHelperSQL.Query(strSql.ToString(); / / 获得前几行数据 / public DataSet GetList(int Top, string strWhere, string filedOrder) StringBuilder strSql = new StringBuilder(); strSql.Append(select ); if (Top 0) strSql.Append( top + Top.ToString
14、(); strSql.Append( adminID,LoginID,LoginPWD,AdminName,sex ); strSql.Append( FROM Admin ); if (strWhere.Trim() != ) strSql.Append( where + strWhere); strSql.Append( order by + filedOrder); return DbHelperSQL.Query(strSql.ToString(); / / 批量删除数据 / public bool DeleteList(string adminIDlist) StringBuilde
15、r strSql = new StringBuilder(); strSql.Append(delete from Admin ); strSql.Append( where adminID in ( + adminIDlist + ) ); int rows = DbHelperSQL.ExecuteSql(strSql.ToString(); if (rows 0) return true; else return false; 2.bll层 / / 获得数据列表 / public DataSet GetList(string strWhere) return dal.GetList(strWhere); / / 获得前几行数据 / public DataSet GetList(int Top, string strWhere, string filedOrder) return dal.GetList(Top, strWhere, filedOrder); / / 删除一条数据 / public bool DeleteList(string adminIDlist) return dal.DeleteList(adminIDlist); 3.web层(1)页面新建页面 List.aspx 关键字: 增加 /asp:Te
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2