ITPub博客

首页 > Linux操作系统 > Linux操作系统 > asp.net标准三层架构--工厂模式

asp.net标准三层架构--工厂模式

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-02-18 16:33:02 0 删除 编辑

下面我先把类给大家传上来, 大家先看一下: 

表:AnserTable

 

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Yeah.IDAL;
using Maticsoft.DBUtility;//请先添加引用
namespace Yeah.SQLServerDAL
{
 ///


 /// 数据访问类AnserTable。
 ///

 public class AnserTable:IAnserTable
 {
  public AnserTable()
  {}
  #region  成员方法
  ///
  /// 是否存在该记录
  ///

  public bool Exists(int TbID)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select count(1) from AnserTable");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4)};
   parameters[0].Value = TbID;

   return DbHelperSQL.Exists(strSql.ToString(),parameters);
  }


  ///


  /// 增加一条数据
  ///

  public int Add(Yeah.Model.AnserTable model)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("insert into AnserTable(");
   strSql.Append("ParentID,AnserTitle,AnserContent,UserID,AddTime)");
   strSql.Append(" values (");
   strSql.Append("@ParentID,@AnserTitle,@AnserContent,@UserID,@AddTime)");
   strSql.Append(";select @@IDENTITY");
   SqlParameter[] parameters = {
     new SqlParameter("@ParentID", SqlDbType.VarChar,50),
     new SqlParameter("@AnserTitle", SqlDbType.VarChar,100),
     new SqlParameter("@AnserContent", SqlDbType.NText),
     new SqlParameter("@UserID", SqlDbType.VarChar,50),
     new SqlParameter("@AddTime", SqlDbType.DateTime)};
   parameters[0].Value = model.ParentID;
   parameters[1].Value = model.AnserTitle;
   parameters[2].Value = model.AnserContent;
   parameters[3].Value = model.UserID;
   parameters[4].Value = model.AddTime;

   object bj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
   if (obj == null)
   {
    return 1;
   }
   else
   {
    return Convert.ToInt32(obj);
   }
  }
  ///


  /// 更新一条数据
  ///

  public void Update(Yeah.Model.AnserTable model)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("update AnserTable set ");
   strSql.Append("ParentID=@ParentID,");
   strSql.Append("AnserTitle=@AnserTitle,");
   strSql.Append("AnserContent=@AnserContent,");
   strSql.Append("UserID=@UserID,");
   strSql.Append("AddTime=@AddTime");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4),
     new SqlParameter("@ParentID", SqlDbType.VarChar,50),
     new SqlParameter("@AnserTitle", SqlDbType.VarChar,100),
     new SqlParameter("@AnserContent", SqlDbType.NText),
     new SqlParameter("@UserID", SqlDbType.VarChar,50),
     new SqlParameter("@AddTime", SqlDbType.DateTime)};
   parameters[0].Value = model.TbID;
   parameters[1].Value = model.ParentID;
   parameters[2].Value = model.AnserTitle;
   parameters[3].Value = model.AnserContent;
   parameters[4].Value = model.UserID;
   parameters[5].Value = model.AddTime;

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

  ///


  /// 删除一条数据
  ///

  public void Delete(int TbID)
  {
   
   StringBuilder strSql=new StringBuilder();
   strSql.Append("delete AnserTable ");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4)};
   parameters[0].Value = TbID;

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


  ///


  /// 得到一个对象实体
  ///

  public Yeah.Model.AnserTable GetModel(int TbID)
  {
   
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select  top 1 TbID,ParentID,AnserTitle,AnserContent,UserID,AddTime from AnserTable ");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4)};
   parameters[0].Value = TbID;

   Yeah.Model.AnserTable model=new Yeah.Model.AnserTable();
   DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
   if(ds.Tables[0].Rows.Count>0)
   {
    if(ds.Tables[0].Rows[0]["TbID"].ToString()!="")
    {
     model.TbID=int.Parse(ds.Tables[0].Rows[0]["TbID"].ToString());
    }
    model.ParentID=ds.Tables[0].Rows[0]["ParentID"].ToString();
    model.AnserTitle=ds.Tables[0].Rows[0]["AnserTitle"].ToString();
    model.AnserContent=ds.Tables[0].Rows[0]["AnserContent"].ToString();
    model.UserID=ds.Tables[0].Rows[0]["UserID"].ToString();
    if(ds.Tables[0].Rows[0]["AddTime"].ToString()!="")
    {
     model.AddTime=DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
    }
    return model;
   }
   else
   {
    return null;
   }
  }

  ///


  /// 获得数据列表
  ///

  public DataSet GetList(string strWhere)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select TbID,ParentID,AnserTitle,AnserContent,UserID,AddTime ");
   strSql.Append(" FROM AnserTable ");
   if(strWhere.Trim()!="")
   {
    strSql.Append(" where "+strWhere);
   }
   return DbHelperSQL.Query(strSql.ToString());
  }

  /*
  ///


  /// 分页获取数据列表
  ///

  public DataSet GetList(int PageSize,int PageIndex,string strWhere)
  {
   SqlParameter[] parameters = {
     new SqlParameter("@tblName", SqlDbType.VarChar, 255),
     new SqlParameter("@fldName", SqlDbType.VarChar, 255),
     new SqlParameter("@PageSize", SqlDbType.Int),
     new SqlParameter("@PageIndex", SqlDbType.Int),
     new SqlParameter("@IsReCount", SqlDbType.Bit),
     new SqlParameter("@OrderType", SqlDbType.Bit),
     new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
     };
   parameters[0].Value = "AnserTable";
   parameters[1].Value = "ID";
   parameters[2].Value = PageSize;
   parameters[3].Value = PageIndex;
   parameters[4].Value = 0;
   parameters[5].Value = 0;
   parameters[6].Value = strWhere; 
   return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
  }*/

  #endregion  成员方法
 }
}

 表: MessageTable

 

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Yeah.IDAL;
using Maticsoft.DBUtility;//请先添加引用
namespace Yeah.SQLServerDAL
{
 ///


 /// 数据访问类MessageTable。
 ///

 public class MessageTable:IMessageTable
 {
  public MessageTable()
  {}
  #region  成员方法
  ///
  /// 是否存在该记录
  ///

  public bool Exists(int TbID)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select count(1) from MessageTable");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4)};
   parameters[0].Value = TbID;

   return DbHelperSQL.Exists(strSql.ToString(),parameters);
  }


  ///


  /// 增加一条数据
  ///

  public int Add(Yeah.Model.MessageTable model)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("insert into MessageTable(");
   strSql.Append("MessageID,UserID,MessageContent,AddTime,Special,Better)");
   strSql.Append(" values (");
   strSql.Append("@MessageID,@UserID,@MessageContent,@AddTime,@Special,@Better)");
   strSql.Append(";select @@IDENTITY");
   SqlParameter[] parameters = {
     new SqlParameter("@MessageID", SqlDbType.VarChar,50),
     new SqlParameter("@UserID", SqlDbType.VarChar,50),
     new SqlParameter("@MessageContent", SqlDbType.NText),
     new SqlParameter("@AddTime", SqlDbType.DateTime),
     new SqlParameter("@Special", SqlDbType.VarChar,2),
     new SqlParameter("@Better", SqlDbType.VarChar,2)};
   parameters[0].Value = model.MessageID;
   parameters[1].Value = model.UserID;
   parameters[2].Value = model.MessageContent;
   parameters[3].Value = model.AddTime;
   parameters[4].Value = model.Special;
   parameters[5].Value = model.Better;

   object bj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
   if (obj == null)
   {
    return 1;
   }
   else
   {
    return Convert.ToInt32(obj);
   }
  }
  ///


  /// 更新一条数据
  ///

  public void Update(Yeah.Model.MessageTable model)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("update MessageTable set ");
   strSql.Append("MessageID=@MessageID,");
   strSql.Append("UserID=@UserID,");
   strSql.Append("MessageContent=@MessageContent,");
   strSql.Append("AddTime=@AddTime,");
   strSql.Append("Special=@Special,");
   strSql.Append("Better=@Better");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4),
     new SqlParameter("@MessageID", SqlDbType.VarChar,50),
     new SqlParameter("@UserID", SqlDbType.VarChar,50),
     new SqlParameter("@MessageContent", SqlDbType.NText),
     new SqlParameter("@AddTime", SqlDbType.DateTime),
     new SqlParameter("@Special", SqlDbType.VarChar,2),
     new SqlParameter("@Better", SqlDbType.VarChar,2)};
   parameters[0].Value = model.TbID;
   parameters[1].Value = model.MessageID;
   parameters[2].Value = model.UserID;
   parameters[3].Value = model.MessageContent;
   parameters[4].Value = model.AddTime;
   parameters[5].Value = model.Special;
   parameters[6].Value = model.Better;

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

  ///


  /// 删除一条数据
  ///

  public void Delete(int TbID)
  {
   
   StringBuilder strSql=new StringBuilder();
   strSql.Append("delete MessageTable ");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4)};
   parameters[0].Value = TbID;

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


  ///


  /// 得到一个对象实体
  ///

  public Yeah.Model.MessageTable GetModel(int TbID)
  {
   
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select  top 1 TbID,MessageID,UserID,MessageContent,AddTime,Special,Better from MessageTable ");
   strSql.Append(" where TbID=@TbID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4)};
   parameters[0].Value = TbID;

   Yeah.Model.MessageTable model=new Yeah.Model.MessageTable();
   DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
   if(ds.Tables[0].Rows.Count>0)
   {
    if(ds.Tables[0].Rows[0]["TbID"].ToString()!="")
    {
     model.TbID=int.Parse(ds.Tables[0].Rows[0]["TbID"].ToString());
    }
    model.MessageID=ds.Tables[0].Rows[0]["MessageID"].ToString();
    model.UserID=ds.Tables[0].Rows[0]["UserID"].ToString();
    model.MessageContent=ds.Tables[0].Rows[0]["MessageContent"].ToString();
    if(ds.Tables[0].Rows[0]["AddTime"].ToString()!="")
    {
     model.AddTime=DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
    }
    model.Special=ds.Tables[0].Rows[0]["Special"].ToString();
    model.Better=ds.Tables[0].Rows[0]["Better"].ToString();
    return model;
   }
   else
   {
    return null;
   }
  }

  ///


  /// 获得数据列表
  ///

  public DataSet GetList(string strWhere)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select TbID,MessageID,UserID,MessageContent,AddTime,Special,Better ");
   strSql.Append(" FROM MessageTable ");
   if(strWhere.Trim()!="")
   {
    strSql.Append(" where "+strWhere);
   }
   return DbHelperSQL.Query(strSql.ToString());
  }

  /*
  ///


  /// 分页获取数据列表
  ///

  public DataSet GetList(int PageSize,int PageIndex,string strWhere)
  {
   SqlParameter[] parameters = {
     new SqlParameter("@tblName", SqlDbType.VarChar, 255),
     new SqlParameter("@fldName", SqlDbType.VarChar, 255),
     new SqlParameter("@PageSize", SqlDbType.Int),
     new SqlParameter("@PageIndex", SqlDbType.Int),
     new SqlParameter("@IsReCount", SqlDbType.Bit),
     new SqlParameter("@OrderType", SqlDbType.Bit),
     new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
     };
   parameters[0].Value = "MessageTable";
   parameters[1].Value = "ID";
   parameters[2].Value = PageSize;
   parameters[3].Value = PageIndex;
   parameters[4].Value = 0;
   parameters[5].Value = 0;
   parameters[6].Value = strWhere; 
   return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
  }*/

  #endregion  成员方法
 }
}

 表:User

 

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Yeah.IDAL;
using Maticsoft.DBUtility;//请先添加引用
namespace Yeah.SQLServerDAL
{
 ///


 /// 数据访问类User。
 ///

 public class User:IUser
 {
  public User()
  {}
  #region  成员方法
  ///
  /// 是否存在该记录
  ///

  public bool Exists(int TbID,string UserID)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select count(1) from User");
   strSql.Append(" where TbID=@TbID and UserID=@UserID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4),
     new SqlParameter("@UserID", SqlDbType.VarChar,50)};
   parameters[0].Value = TbID;
   parameters[1].Value = UserID;

   return DbHelperSQL.Exists(strSql.ToString(),parameters);
  }


  ///


  /// 增加一条数据
  ///

  public int Add(Yeah.Model.User model)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("insert into User(");
   strSql.Append("UserID,UserPassword,AddTime,Sex,Integral)");
   strSql.Append(" values (");
   strSql.Append("@UserID,@UserPassword,@AddTime,@Sex,@Integral)");
   strSql.Append(";select @@IDENTITY");
   SqlParameter[] parameters = {
     new SqlParameter("@UserID", SqlDbType.VarChar,50),
     new SqlParameter("@UserPassword", SqlDbType.VarChar,50),
     new SqlParameter("@AddTime", SqlDbType.DateTime),
     new SqlParameter("@Sex", SqlDbType.VarChar,2),
     new SqlParameter("@Integral", SqlDbType.Int,4)};
   parameters[0].Value = model.UserID;
   parameters[1].Value = model.UserPassword;
   parameters[2].Value = model.AddTime;
   parameters[3].Value = model.Sex;
   parameters[4].Value = model.Integral;

   object bj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
   if (obj == null)
   {
    return 1;
   }
   else
   {
    return Convert.ToInt32(obj);
   }
  }
  ///


  /// 更新一条数据
  ///

  public void Update(Yeah.Model.User model)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("update User set ");
   strSql.Append("UserPassword=@UserPassword,");
   strSql.Append("AddTime=@AddTime,");
   strSql.Append("Sex=@Sex,");
   strSql.Append("Integral=@Integral");
   strSql.Append(" where TbID=@TbID and UserID=@UserID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4),
     new SqlParameter("@UserID", SqlDbType.VarChar,50),
     new SqlParameter("@UserPassword", SqlDbType.VarChar,50),
     new SqlParameter("@AddTime", SqlDbType.DateTime),
     new SqlParameter("@Sex", SqlDbType.VarChar,2),
     new SqlParameter("@Integral", SqlDbType.Int,4)};
   parameters[0].Value = model.TbID;
   parameters[1].Value = model.UserID;
   parameters[2].Value = model.UserPassword;
   parameters[3].Value = model.AddTime;
   parameters[4].Value = model.Sex;
   parameters[5].Value = model.Integral;

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

  ///


  /// 删除一条数据
  ///

  public void Delete(int TbID,string UserID)
  {
   
   StringBuilder strSql=new StringBuilder();
   strSql.Append("delete User ");
   strSql.Append(" where TbID=@TbID and UserID=@UserID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4),
     new SqlParameter("@UserID", SqlDbType.VarChar,50)};
   parameters[0].Value = TbID;
   parameters[1].Value = UserID;

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


  ///


  /// 得到一个对象实体
  ///

  public Yeah.Model.User GetModel(int TbID,string UserID)
  {
   
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select  top 1 TbID,UserID,UserPassword,AddTime,Sex,Integral from User ");
   strSql.Append(" where TbID=@TbID and UserID=@UserID ");
   SqlParameter[] parameters = {
     new SqlParameter("@TbID", SqlDbType.Int,4),
     new SqlParameter("@UserID", SqlDbType.VarChar,50)};
   parameters[0].Value = TbID;
   parameters[1].Value = UserID;

   Yeah.Model.User model=new Yeah.Model.User();
   DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
   if(ds.Tables[0].Rows.Count>0)
   {
    if(ds.Tables[0].Rows[0]["TbID"].ToString()!="")
    {
     model.TbID=int.Parse(ds.Tables[0].Rows[0]["TbID"].ToString());
    }
    model.UserID=ds.Tables[0].Rows[0]["UserID"].ToString();
    model.UserPassword=ds.Tables[0].Rows[0]["UserPassword"].ToString();
    if(ds.Tables[0].Rows[0]["AddTime"].ToString()!="")
    {
     model.AddTime=DateTime.Parse(ds.Tables[0].Rows[0]["AddTime"].ToString());
    }
    model.Sex=ds.Tables[0].Rows[0]["Sex"].ToString();
    if(ds.Tables[0].Rows[0]["Integral"].ToString()!="")
    {
     model.Integral=int.Parse(ds.Tables[0].Rows[0]["Integral"].ToString());
    }
    return model;
   }
   else
   {
    return null;
   }
  }

  ///


  /// 获得数据列表
  ///

  public DataSet GetList(string strWhere)
  {
   StringBuilder strSql=new StringBuilder();
   strSql.Append("select TbID,UserID,UserPassword,AddTime,Sex,Integral ");
   strSql.Append(" FROM User ");
   if(strWhere.Trim()!="")
   {
    strSql.Append(" where "+strWhere);
   }
   return DbHelperSQL.Query(strSql.ToString());
  }

  /*
  ///


  /// 分页获取数据列表
  ///

  public DataSet GetList(int PageSize,int PageIndex,string strWhere)
  {
   SqlParameter[] parameters = {
     new SqlParameter("@tblName", SqlDbType.VarChar, 255),
     new SqlParameter("@fldName", SqlDbType.VarChar, 255),
     new SqlParameter("@PageSize", SqlDbType.Int),
     new SqlParameter("@PageIndex", SqlDbType.Int),
     new SqlParameter("@IsReCount", SqlDbType.Bit),
     new SqlParameter("@OrderType", SqlDbType.Bit),
     new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
     };
   parameters[0].Value = "User";
   parameters[1].Value = "ID";
   parameters[2].Value = PageSize;
   parameters[3].Value = PageIndex;
   parameters[4].Value = 0;
   parameters[5].Value = 0;
   parameters[6].Value = strWhere; 
   return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
  }*/

  #endregion  成员方法
 }
}

 大家可以看一下,上面的数据层的方法跟接口类里提供的数据接口是一一对应的,大家可以对照着看一下,

顺便了解一下接口的内容!

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-555243/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5329078