三层架构开发实战

Model:

代码

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

namespace Maticsoft.Model
{
    
/// <summary>
    
/// 实体类Product 。(属性说明自动提取数据库字段的描述信息)
    
/// </summary>
    public class Product
    {
        
public Product()
        { }
        
#region Model
        
private int _id;
        
private decimal? _vipprice;
        
private int? _cheapness;
        
private string _productid;
        
private string _categoryid;
        
private string _brandid;
        
private string _name;
        
private string _descn;
        
private string _image;
        
private string _imagesmall;
        
private decimal? _price;

        
/// <summary>
        
/// 流水号
        
/// </summary>
        public int ID
        {
            
set { _id = value; }
            
get { return _id; }
        }
        
/// <summary>
        
/// 会员价格
        
/// </summary>
        public decimal? VipPrice
        {
            
set { _vipprice = value; }
            
get { return _vipprice; }
        }
        
/// <summary>
        
/// 是否优惠
        
/// </summary>
        public int? Cheapness
        {
            
set { _cheapness = value; }
            
get { return _cheapness; }
        }
        
/// <summary>
        
/// 产品编号
        
/// </summary>
        public string ProductId
        {
            
set { _productid = value; }
            
get { return _productid; }
        }
        
/// <summary>
        
/// 类别编号
        
/// </summary>
        public string CategoryId
        {
            
set { _categoryid = value; }
            
get { return _categoryid; }
        }
        
/// <summary>
        
/// 品牌编号
        
/// </summary>
        public string BrandId
        {
            
set { _brandid = value; }
            
get { return _brandid; }
        }
        
/// <summary>
        
/// 产品名
        
/// </summary>
        public string Name
        {
            
set { _name = value; }
            
get { return _name; }
        }
        
/// <summary>
        
/// 产品描述
        
/// </summary>
        public string Descn
        {
            
set { _descn = value; }
            
get { return _descn; }
        }
        
/// <summary>
        
/// 产品图片
        
/// </summary>
        public string Image
        {
            
set { _image = value; }
            
get { return _image; }
        }
        
/// <summary>
        
/// 缩略图
        
/// </summary>
        public string ImageSmall
        {
            
set { _imagesmall = value; }
            
get { return _imagesmall; }
        }
        
/// <summary>
        
/// 产品价格
        
/// </summary>
        public decimal? Price
        {
            
set { _price = value; }
            
get { return _price; }
        }
        
#endregion Model

    }
}

 

DAL:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Maticsoft.DAL
{
    
/// <summary>
    
/// 数据访问类Product。
    
/// </summary>
    public class Product
    {
        
public Product()
        { }
        
#region  成员方法

        
/// <summary>
        
/// 增加一条数据
        
/// </summary>
        public int Add(Maticsoft.Model.Product model)
        {
            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"insert into P_Product(");
            strSql.Append(
"VipPrice,Cheapness,ProductId,CategoryId,BrandId,Name,Descn,Image,ImageSmall,Price)");
            strSql.Append(
" values (");
            strSql.Append(
"@VipPrice,@Cheapness,@ProductId,@CategoryId,@BrandId,@Name,@Descn,@Image,@ImageSmall,@Price)");
            strSql.Append(
";select @@IDENTITY");
            SqlParameter[] parameters 
= {
                    
new SqlParameter("@VipPrice", SqlDbType.Decimal,9),
                    
new SqlParameter("@Cheapness", SqlDbType.Int,4),
                    
new SqlParameter("@ProductId", SqlDbType.VarChar,20),
                    
new SqlParameter("@CategoryId", SqlDbType.VarChar,20),
                    
new SqlParameter("@BrandId", SqlDbType.VarChar,20),
                    
new SqlParameter("@Name", SqlDbType.VarChar,80),
                    
new SqlParameter("@Descn", SqlDbType.Text),
                    
new SqlParameter("@Image", SqlDbType.VarChar,80),
                    
new SqlParameter("@ImageSmall", SqlDbType.VarChar,80),
                    
new SqlParameter("@Price", SqlDbType.Decimal,9)};
            parameters[
0].Value = model.VipPrice;
            parameters[
1].Value = model.Cheapness;
            parameters[
2].Value = model.ProductId;
            parameters[
3].Value = model.CategoryId;
            parameters[
4].Value = model.BrandId;
            parameters[
5].Value = model.Name;
            parameters[
6].Value = model.Descn;
            parameters[
7].Value = model.Image;
            parameters[
8].Value = model.ImageSmall;
            parameters[
9].Value = model.Price;

            
object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            
if (obj == null)
            {
                
return 1;
            }
            
else
            {
                
return Convert.ToInt32(obj);
            }
        }
        
/// <summary>
        
/// 更新一条数据
        
/// </summary>
        public void Update(Maticsoft.Model.Product model)
        {
            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"update P_Product set ");
            strSql.Append(
"VipPrice=@VipPrice,");
            strSql.Append(
"Cheapness=@Cheapness,");
            strSql.Append(
"CategoryId=@CategoryId,");
            strSql.Append(
"BrandId=@BrandId,");
            strSql.Append(
"Name=@Name,");
            strSql.Append(
"Descn=@Descn,");
            strSql.Append(
"Image=@Image,");
            strSql.Append(
"ImageSmall=@ImageSmall,");
            strSql.Append(
"Price=@Price");
            strSql.Append(
" where ID=@ID and ProductId=@ProductId ");
            SqlParameter[] parameters 
= {
                    
new SqlParameter("@ID", SqlDbType.Int,4),
                    
new SqlParameter("@VipPrice", SqlDbType.Decimal,9),
                    
new SqlParameter("@Cheapness", SqlDbType.Int,4),
                    
new SqlParameter("@ProductId", SqlDbType.VarChar,20),
                    
new SqlParameter("@CategoryId", SqlDbType.VarChar,20),
                    
new SqlParameter("@BrandId", SqlDbType.VarChar,20),
                    
new SqlParameter("@Name", SqlDbType.VarChar,80),
                    
new SqlParameter("@Descn", SqlDbType.Text),
                    
new SqlParameter("@Image", SqlDbType.VarChar,80),
                    
new SqlParameter("@ImageSmall", SqlDbType.VarChar,80),
                    
new SqlParameter("@Price", SqlDbType.Decimal,9)};
            parameters[
0].Value = model.ID;
            parameters[
1].Value = model.VipPrice;
            parameters[
2].Value = model.Cheapness;
            parameters[
3].Value = model.ProductId;
            parameters[
4].Value = model.CategoryId;
            parameters[
5].Value = model.BrandId;
            parameters[
6].Value = model.Name;
            parameters[
7].Value = model.Descn;
            parameters[
8].Value = model.Image;
            parameters[
9].Value = model.ImageSmall;
            parameters[
10].Value = model.Price;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        
//从web.config中获取另一个数据库的连接字符串
        string connectionString2 = ConfigurationManager.AppSettings["ConnectionString2"];  
        
/// <summary>
        
/// 更新数据
        
/// </summary>
        public void Update2(Maticsoft.Model.Product model)
        {
            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"update P_Product set ");            
            strSql.Append(
"Name=@Name,");
            strSql.Append(
"Descn=@Descn,");            
            strSql.Append(
"Price=@Price");
            strSql.Append(
" where ProductId=@ProductId ");
            SqlParameter[] parameters 
= {                    
                    
new SqlParameter("@ProductId", SqlDbType.VarChar,20),                    
                    
new SqlParameter("@Name", SqlDbType.VarChar,80),
                    
new SqlParameter("@Descn", SqlDbType.Text),                    
                    
new SqlParameter("@Price", SqlDbType.Decimal,9)};
            parameters[
3].Value = model.ProductId;            
            parameters[
6].Value = model.Name;
            parameters[
7].Value = model.Descn;            
            parameters[
10].Value = model.Price;
            DbHelperSQL.ExecuteSql(connectionString2,strSql.ToString(), parameters);
        }

        
/// <summary>
        
/// 删除一条数据
        
/// </summary>
        public void Delete(int ID, string ProductId)
        {

            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"delete P_Product ");
            strSql.Append(
" where ID=@ID and ProductId=@ProductId ");
            SqlParameter[] parameters 
= {
                    
new SqlParameter("@ID", SqlDbType.Int,4),
                    
new SqlParameter("@ProductId", SqlDbType.VarChar,50)};
            parameters[
0].Value = ID;
            parameters[
1].Value = ProductId;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        
/// <summary>
        
/// 得到一个对象实体
        
/// </summary>
        public Maticsoft.Model.Product GetModel(string ProductId)
        {
            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"select  top 1 ID,VipPrice,Cheapness,ProductId,CategoryId,BrandId,Name,Descn,Image,ImageSmall,Price from P_Product ");
            strSql.Append(
" where ProductId=@ProductId ");
            SqlParameter[] parameters 
= {                    
                    
new SqlParameter("@ProductId", SqlDbType.VarChar,50)};           
            parameters[
0].Value = ProductId;

            Maticsoft.Model.Product model 
= new Maticsoft.Model.Product();
            DataSet ds 
= DbHelperSQL.Query(strSql.ToString(), parameters);
            
if (ds.Tables[0].Rows.Count > 0)
            {
                
if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID 
= int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                
if (ds.Tables[0].Rows[0]["VipPrice"].ToString() != "")
                {
                    model.VipPrice 
= decimal.Parse(ds.Tables[0].Rows[0]["VipPrice"].ToString());
                }
                
if (ds.Tables[0].Rows[0]["Cheapness"].ToString() != "")
                {
                    model.Cheapness 
= int.Parse(ds.Tables[0].Rows[0]["Cheapness"].ToString());
                }
                model.ProductId 
= ds.Tables[0].Rows[0]["ProductId"].ToString();
                model.CategoryId 
= ds.Tables[0].Rows[0]["CategoryId"].ToString();
                model.BrandId 
= ds.Tables[0].Rows[0]["BrandId"].ToString();
                model.Name 
= ds.Tables[0].Rows[0]["Name"].ToString();
                model.Descn 
= ds.Tables[0].Rows[0]["Descn"].ToString();
                model.Image 
= ds.Tables[0].Rows[0]["Image"].ToString();
                model.ImageSmall 
= ds.Tables[0].Rows[0]["ImageSmall"].ToString();
                
if (ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price 
= decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                
return model;
            }
            
else
            {
                
return null;
            }
        }

        
/// <summary>
        
/// 获得数据列表
        
/// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql 
= new StringBuilder();
            strSql.Append(
"select ID,VipPrice,Cheapness,ProductId,CategoryId,BrandId,Name,Descn,Image,ImageSmall,Price ");
            strSql.Append(
" FROM P_Product ");
            
if (strWhere.Trim() != "")
            {
                strSql.Append(
" where " + strWhere);
            }
            
return DbHelperSQL.Query(strSql.ToString());
        }           

        
#endregion  成员方法
    }
}

 

 

BLL:

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using Maticsoft.Model;
namespace Maticsoft.BLL
{
    
/// <summary>
    
/// 业务逻辑类Product 的摘要说明。
    
/// </summary>
    public class Product
    {
        
private readonly Maticsoft.DAL.Product dal = new Maticsoft.DAL.Product();
        
public Product()
        { }

        
#region  成员方法
        
/// <summary>
        
/// 增加一条数据
        
/// </summary>
        public int Add(Maticsoft.Model.Product model)
        {
            
return dal.Add(model);
        }

        
/// <summary>
        
/// 更新一条数据
        
/// </summary>
        public void Update(Maticsoft.Model.Product model)
        {
            dal.Update(model);
        }

        
/// <summary>
        
/// 删除一条数据
        
/// </summary>
        public void Delete(int ID, string ProductId)
        {

            dal.Delete(ID, ProductId);
        }

        
/// <summary>
        
/// 得到一个对象实体
        
/// </summary>
        public Maticsoft.Model.Product GetModel(string ProductId)
        {

            
return dal.GetModel( ProductId);
        }

        
/// <summary>
        
/// 获得数据列表
        
/// </summary>
        public DataSet GetList(string strWhere)
        {
            
return dal.GetList(strWhere);
        }
        
/// <summary>
        
/// 获得数据列表
        
/// </summary>
        public List<Maticsoft.Model.Product> GetModelList(string strWhere)
        {
            DataSet ds 
= dal.GetList(strWhere);
            List
<Maticsoft.Model.Product> modelList = new List<Maticsoft.Model.Product>();
            
int rowsCount = ds.Tables[0].Rows.Count;
            
if (rowsCount > 0)
            {
                Maticsoft.Model.Product model;
                
for (int n = 0; n < rowsCount; n++)
                {
                    model 
= new Maticsoft.Model.Product();
                    
if (ds.Tables[0].Rows[n]["ID"].ToString() != "")
                    {
                        model.ID 
= int.Parse(ds.Tables[0].Rows[n]["ID"].ToString());
                    }
                    
if (ds.Tables[0].Rows[n]["VipPrice"].ToString() != "")
                    {
                        model.VipPrice 
= decimal.Parse(ds.Tables[0].Rows[n]["VipPrice"].ToString());
                    }
                    
if (ds.Tables[0].Rows[n]["Cheapness"].ToString() != "")
                    {
                        model.Cheapness 
= int.Parse(ds.Tables[0].Rows[n]["Cheapness"].ToString());
                    }
                    model.ProductId 
= ds.Tables[0].Rows[n]["ProductId"].ToString();
                    model.CategoryId 
= ds.Tables[0].Rows[n]["CategoryId"].ToString();
                    model.BrandId 
= ds.Tables[0].Rows[n]["BrandId"].ToString();
                    model.Name 
= ds.Tables[0].Rows[n]["Name"].ToString();
                    model.Descn 
= ds.Tables[0].Rows[n]["Descn"].ToString();
                    model.Image 
= ds.Tables[0].Rows[n]["Image"].ToString();
                    model.ImageSmall 
= ds.Tables[0].Rows[n]["ImageSmall"].ToString();
                    
if (ds.Tables[0].Rows[n]["Price"].ToString() != "")
                    {
                        model.Price 
= decimal.Parse(ds.Tables[0].Rows[n]["Price"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            
return modelList;
        }

        
/// <summary>
        
/// 获得数据列表
        
/// </summary>
        public DataSet GetAllList()
        {
            
return GetList("");
        }     

        
#endregion  成员方法
    }
}

 

WEB:

 

代码
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
decimal VipPrice = decimal.Parse(this.txtVipPrice.Text);
            
int Cheapness = int.Parse(this.txtCheapness.Text);
            
string CategoryId = this.txtCategoryId.Text;
            
string BrandId = this.txtBrandId.Text;
            
string Name = this.txtName.Text;
            
string Descn = this.txtDescn.Text;
            
string Image = this.txtImage.Text;
            
string ImageSmall = this.txtImageSmall.Text;
            
decimal Price = decimal.Parse(this.txtPrice.Text);

            Maticsoft.Model.Product model 
= new Maticsoft.Model.Product();
            model.ProductId 
= txtProductId.Text.Trim();
            model.VipPrice 
= VipPrice;
            model.Cheapness 
= Cheapness;
            model.CategoryId 
= CategoryId;
            model.BrandId 
= BrandId;
            model.Name 
= Name;
            model.Descn 
= Descn;
            model.Image 
= Image;
            model.ImageSmall 
= ImageSmall;
            model.Price 
= Price;

            Maticsoft.BLL.Product bll 
= new Maticsoft.BLL.Product();
            bll.Add(model);
        }

 

 

posted @ 2010-03-23 23:28  qinyi  阅读(242)  评论(0编辑  收藏  举报