你的位置:首页 > ASP.net教程

[ASP.net教程]csharp: 用Enterprise Library对象实体绑定数据

 

Enterprise Library: https://msdn.microsoft.com/en-us/library/ff648951.aspx

 /// <summary>    /// 是否存在该记录    /// </summary>    /// <param name="ReportID"></param>    /// <returns></returns>    public bool Exists(string ReportID)    {      Database db = DatabaseFactory.CreateDatabase();      StringBuilder strSql = new StringBuilder();      strSql.Append("select count(1) from BookPlaceList where [email protected] ");      DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());      db.AddInParameter(dbCommand, "BookPlaceID", DbType.AnsiString, ReportID);      int cmdresult;      object obj = db.ExecuteScalar(dbCommand);      if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))      {        cmdresult = 0;      }      else      {        cmdresult = int.Parse(obj.ToString());      }      if (cmdresult == 0)      {        return false;      }      else      {        return true;      }    }    /// <summary>    /// 增加一条数据    /// </summary>    /// <param name="model"></param>    public void Add(BookPlaceListInfo model)    {      StringBuilder strSql = new StringBuilder();      strSql.Append("insert into BookPlaceList(");      strSql.Append("BookPlaceName,BookPlaceCode,BookPlaceParent)");      strSql.Append(" values (");      strSql.Append("@BookPlaceName,@BookPlaceCode,@BookPlaceParent)");      Database db = DatabaseFactory.CreateDatabase();      DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());      db.AddInParameter(dbCommand, "BookPlaceName", DbType.String, model.BookPlaceName);      db.AddInParameter(dbCommand, "BookPlaceCode", DbType.String, model.BookPlaceCode);      db.AddInParameter(dbCommand, "BookPlaceParent", DbType.Int32, model.BookPlaceParent);      db.ExecuteNonQuery(dbCommand);    }    /// <summary>    /// 更新一条数据    /// </summary>    /// <param name="model"></param>    /// <returns></returns>    public int Update(BookPlaceListInfo model)    {      int i = 0;      StringBuilder strSql = new StringBuilder();      strSql.Append("update BookPlaceList set ");      strSql.Append("[email protected],");      strSql.Append("[email protected],");      strSql.Append("[email protected],");      strSql.Append(" where [email protected] ");      Database db = DatabaseFactory.CreateDatabase();      DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());      db.AddInParameter(dbCommand, "BookPlaceID", DbType.Int32, model.BookPlaceID);      db.AddInParameter(dbCommand, "BookPlaceName", DbType.String, model.BookPlaceName);      db.AddInParameter(dbCommand, "BookPlaceCode", DbType.String, model.BookPlaceCode);      db.AddInParameter(dbCommand, "BookPlaceParent", DbType.Int32, model.BookPlaceParent);      i= db.ExecuteNonQuery(dbCommand);      return i;    }    /// <summary>    ///     /// </summary>    /// <param name="ReportID"></param>    /// <returns></returns>    public int Delete(string ReportID)    {      int i = 0;      StringBuilder strSql = new StringBuilder();      strSql.Append("delete BookPlaceList ");      strSql.Append(" where [email protected] ");      Database db = DatabaseFactory.CreateDatabase();      DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());      db.AddInParameter(dbCommand, "bookPlaceID", DbType.AnsiString, ReportID);      i=db.ExecuteNonQuery(dbCommand);      return i;    }    /// <summary>    /// 获得数据列表    /// </summary>    /// <param name="strWhere"></param>    /// <returns></returns>    public DataSet GetList(string strWhere)    {      StringBuilder strSql = new StringBuilder();      strSql.Append("select * ");      strSql.Append(" FROM BookPlaceList ");      if (strWhere.Trim() != "")      {        strSql.Append(" where " + strWhere);      }      Database db = DatabaseFactory.CreateDatabase();      return db.ExecuteDataSet(CommandType.Text, strSql.ToString());    }    /// <summary>    /// 获得数据列表(比DataSet效率高,推荐使用)    /// using Microsoft.Practices.EnterpriseLibrary.Data;    ///using Microsoft.Practices.EnterpriseLibrary.Data.Sql;    /// </summary>    /// <param name="strWhere"></param>    /// <returns></returns>    public List<BookPlaceListInfo> GetListArray(string strWhere)    {      StringBuilder strSql = new StringBuilder();      strSql.Append("select * ");      strSql.Append(" FROM BookPlaceList ");      if (strWhere.Trim() != "")      {        strSql.Append(" where " + strWhere);      }      List<BookPlaceListInfo> list = new List<BookPlaceListInfo>();      Database db = DatabaseFactory.CreateDatabase();      using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))      {        while (dataReader.Read())        {          list.Add(ReaderBind(dataReader));        }      }      return list;    }    /// <summary>    /// 得到一个对象实体    /// </summary>    public BookPlaceListInfo GetModel(string bookPlaceID)    {      StringBuilder strSql = new StringBuilder();      strSql.Append("select * from BookPlaceList ");      strSql.Append(" where [email protected] ");      Database db = DatabaseFactory.CreateDatabase();      DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());      db.AddInParameter(dbCommand, "bookPlaceID", DbType.AnsiString, bookPlaceID);      BookPlaceListInfo model = null;      using (IDataReader dataReader = db.ExecuteReader(dbCommand))      {        if (dataReader.Read())        {          model = ReaderBind(dataReader);        }      }      return model;    }    /// <summary>    /// 对象实体绑定数据    /// </summary>    /// <param name="dataReader"></param>    /// <returns></returns>    private BookPlaceListInfo ReaderBind(IDataReader dataReader)    {      BookPlaceListInfo bookPlaceList = new BookPlaceListInfo();      //1.      //object ojb;      //ojb = dataReader["BookPlaceID"];      //if (ojb != null && ojb != DBNull.Value)      //{      //  bookPlaceList.BookPlaceID = (int)ojb;      //}      //ojb = dataReader["BookPlaceName"];      //if (ojb != null && ojb != DBNull.Value)      //{      //  bookPlaceList.BookPlaceName = (string)ojb;      //}      //2.      bookPlaceList.BookPlaceID = (!object.Equals(dataReader["BookPlaceID"], null)) ? (int)dataReader["BookPlaceID"] : 0;      bookPlaceList.BookPlaceName = (!object.Equals(dataReader["BookPlaceName"], null)) ? (string)dataReader["BookPlaceName"] : "";      bookPlaceList.BookPlaceCode = (!object.Equals(dataReader["BookPlaceCode"], null)) ? (string)dataReader["BookPlaceCode"] : "";      bookPlaceList.BookPlaceParent = (!object.Equals(dataReader["BookPlaceParent"], null)) ? (int)dataReader["BookPlaceParent"] : 0;      return bookPlaceList;    }