1、 private static Customer GetCustomerBySQL(string sql) using (SqlConnection conn = new SqlConnection(DBHelper.connectString) Customer c = null; try conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader sdr = cmd.ExecuteReader(); if (sdr.Read() c = new Customer(); c.Id = (int)sdrCusto
2、merId; c.LoginName = sdrLoginName.ToString(); c.Password = sdrPassword catch (Exception ex) Console.WriteLine(ex.Message); finally conn.Close(); return c;(2)在DBHelper中 public static class DBHelper public static readonly string connectString = server=.;database=HouseDB;uid=sa;pwd=123456;(3)在HouseServ
3、ice中 public static class HouseService / 获取所有发布的房屋信息 public static IList GetAllHouse() List houses = new List();SqlCommand cmd = new SqlCommand(select * from Houses, conn); while (sdr.Read() House h = new House(); h.Id = (int)sdrHouseId h.TypeName = sdrHouseTypeName h.Area = (int)sdrArea h.Price = Co
4、nvert.ToDouble(sdrPrice); h.Address = sdrAddress/外键对象的处理 h.Customer = CustomerService.GetCustomerById(int)sdr houses.Add(h); return houses; / 根据房屋信息主键ID删除发布的房屋信息 / 受影响的行数 public static int DeleteHouseById(int houserId) int count = 0;using (SqlConnection conn = new SqlConnection(DBHelper.connectStrin
5、g)delete from Houses where HouseId=0, houserId); count = cmd.ExecuteNonQuery(); return count; / 增加发布的房屋信息 public static int AddHouse(House house)insert into dbo.Houses+ (HouseTypeName,Area,Price,Address,CustomerId) + values (,1,2,3,4), house.TypeName, house.Area, house.Price, house.Address, house.Cu
6、stomer.Id); return 0; return 1;二、在HouseManagerModels模型层中设计两个类:Customer.cs和house.cs (1)在Customer.cs中namespace Serializable public class Customer private int id; public int Id get return id; set id = value; private string loginName; / 登录账号 public string LoginName get return loginName; set loginName =
7、value; private string password; / 登录密码 public string Password get return password; set password = value;(2)在house.cs中namespace HouseManager.Models public class House private string typeName; / 房屋类型名称 public string TypeName get return typeName; set typeName = value; private int area; / 面积 public int
8、Area get return area; set area = value; private double price; / 价格 public double Price get return price; set price = value; private string address; / 地址 public string Address get return address; set address = value; private Customer customer; / 发布人 public Customer Customer get return customer; set c
9、ustomer = value;三、在HouseManagerBLL逻辑业务层中设计两个类:HouseManager.cs和LoginManager.cs(1)在HouseManager.cs中:using HouseManager.DAL;namespace HouseManager.BLL public static class HouseManager / 查询所有发布的房屋信息 return HouseService.GetAllHouse(); / 删除已发布的房屋信息删除是否成功 public static bool DeleteHouse(int houseId) return
10、HouseService.DeleteHouseById(houseId) != 0; / 发布房屋信息添加是否成功 public static bool AddHouse(House house) return HouseService.AddHouse(house) !(2)在LoginManager.cs中 public static class LoginManager / 用户登录判断是否登录成功 public static bool Login(string name, string password, out Customer customer) customer = null;
11、 Customer c = CustomerService.GetCustomerByLoginName(name); if (c = null) return false; if (c.Password.Equals(password) customer = c; return true;四、在表示层中建立一个空网站:其中包括四个网页窗体:about.aspx和default.aspx和LoginPage.aspx和ReleaseHouseInformationPage.aspx(1)在LoginPage.aspx中代码如下:using System.Configuration;using
12、System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using HouseManager.BLL;public partial class LoginPage : System.Web.UI.Page protected void Page_Load(object sende
13、r, EventArgs e) if (!this.IsPostBack) /已登录直接跳转到查看页面 if (SessionUser != null) this.Response.Redirect(/default.aspx); protected void btnLogin_Click(object sender, EventArgs e) Customer cus = null; /验证登录信息是否正确 if (LoginManager.Login(this.txtLoginName.Text.Trim(), this.txtPassword.Text.Trim(), out cus)
14、/跳转到查看页面 Session = cus; else /提示错误信息 this.ClientScript.RegisterStartupScript(this.GetType(), Warnning alert(用户信息不正确!)(2)在ReleaseHouseInformationPage.aspx中:public partial class ReleaseHouseInformationPage : /没有登录的话,跳转到登录页面 = null)/LoginPage.aspx protected void btnSubmit_Click(object sender, EventArgs
15、 e) /从界面获取用户输入的信息 House house = new House(); house.TypeName = this.ddlType.SelectedValue; house.Area = int.Parse(this.txtArea.Text); house.Price = double.Parse(this.txtPrice.Text); house.Address = this.txtAddress.Text; Customer customer = Session as Customer; house.Customer = customer; /判断保存信息是否成功 i
16、f (HouseManager.BLL.HouseManager.AddHouse(house) /提示成功信息并跳转到查看页面Alert房屋信息增加成功!window.location.href=default.aspx房屋信息增加失败!(3)在default.aspx中:具体就是:设置GridView,设置数据源等操作。(4)在about.aspx中:自动带的。学生管理系统一、StudentDAL数据访问层中设计三个类:AdminDAL.cs和DBHelper.cs和studentDAL.cs (1)在AdminDAL.cs中using System.Linq;using StudentM
17、odel;namespace StudentDAL public class AdminDAL public static Admin GetAdminByLoginName(string name)select * from admin where UserId= return GetAdminBySQL(sql); private static Admin GetAdminBySQL(string sql) Admin c = null; c = new Admin(); c.UserId = sdrUserId.ToString().Trim(); c.UserPwd = sdrUserPwd
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1