asp.net標準三層架構--工廠模式

iDotNetSpace發表於2009-02-18

下面我先把類給大家傳上來, 大家先看一下: 

表: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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章