logo资料库

web程序设计代码_学生管理系统.doc

第1页 / 共33页
第2页 / 共33页
第3页 / 共33页
第4页 / 共33页
第5页 / 共33页
第6页 / 共33页
第7页 / 共33页
第8页 / 共33页
资料共33页,剩余部分请下载后查看
房屋销售管理系统 一、HouseManagerDAL 数据访问层中设计三个类: CustomerService.cs 和 DBhelper.cs 和 houseserver.cs (1)在 CustomerService.cs 中 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using HouseManager.Models; namespace HouseManager.DAL {public static class CustomerService {/// 根据提供的登录账号查询用户信息 public static Customer GetCustomerByLoginName(string name) { string sql = string.Format("select * from Customers where LoginName='{0}'", name); return GetCustomerBySQL(sql); } /// 根据用户ID查询用户信息 public static Customer GetCustomerById(int id)
{ string sql = string.Format("select * from Customers where CustomerId={0}", id); return GetCustomerBySQL(sql); } /// 私有方法,提供公共方法查询用户信息使用 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)sdr["CustomerId"]; c.LoginName = sdr["LoginName"].ToString();
c.Password = sdr["Password"].ToString(); } } catch (Exception ex) { } Console.WriteLine(ex.Message); finally { } conn.Close(); return c; } } } } (2)在DBHelper中 using System; using System.Collections.Generic; using System.Text; namespace HouseManager.DAL {
public static class DBHelper { public static readonly string connectString = "server=.;database=HouseDB;uid=sa;pwd=123456"; } } (3)在HouseService中 using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using HouseManager.Models; namespace HouseManager.DAL { public static class HouseService { /// 获取所有发布的房屋信息 public static IList GetAllHouse() { List houses = new List(); using (SqlConnection conn = new
SqlConnection(DBHelper.connectString)) { try { conn.Open(); SqlCommand cmd = new SqlCommand("select * from Houses", conn); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { House h = new House(); h.Id = (int)sdr["HouseId"]; h.TypeName = sdr["HouseTypeName"].ToString(); h.Area = (int)sdr["Area"]; h.Price = Convert.ToDouble(sdr["Price"]); h.Address = sdr["Address"].ToString();//外键对象的处理 h.Customer = CustomerService.GetCustomerById((int)sdr["CustomerId"]); houses.Add(h); } } catch (Exception ex) {
Console.WriteLine(ex.Message); } finally { } conn.Close(); } return houses; } /// 根据房屋信息主键ID删除发布的房屋信息 /// 受影响的行数 public static int DeleteHouseById(int houserId) { int count = 0; using (SqlConnection conn = new SqlConnection(DBHelper.connectString)) { try { string sql = string.Format("delete from Houses where HouseId={0}", houserId);
conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); count = cmd.ExecuteNonQuery(); } catch (Exception ex) { } Console.WriteLine(ex.Message); finally { } conn.Close(); } return count; } /// 增加发布的房屋信息 /// 受影响的行数 public static int AddHouse(House house) { string sql = string.Format("insert into dbo.Houses"+ " (HouseTypeName,Area,Price,Address,CustomerId)"
+ " values ('{0}',{1},{2},'{3}',{4})", house.TypeName, house.Area, house.Price, house.Address, house.Customer.Id); int count = 0; using (SqlConnection conn = new SqlConnection(DBHelper.connectString)) { try { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); count = cmd.ExecuteNonQuery(); } catch (Exception ex) { } Console.WriteLine(ex.Message); return 0; finally {
分享到:
收藏