csharp: 用Enterprise Library对象实体绑定数据

简介:  Enterprise Library: https://msdn.microsoft.com/en-us/library/ff648951.aspx /// <summary> /// 是否存在该记录 /// </summary> /// <param name="ReportID"></


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 BookPlaceID=@BookPlaceID ");
            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 int Add(BookPlaceListInfo model)
        {
            int i=0;
            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);
            i=db.ExecuteNonQuery(dbCommand);
            return i;
        }
        /// <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("BookPlaceName=@BookPlaceName,");
            strSql.Append("BookPlaceCode=@BookPlaceCode,");
            strSql.Append("BookPlaceParent=@BookPlaceParent,");
            strSql.Append(" where BookPlaceID=@BookPlaceID ");
            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 bookPlaceID=@bookPlaceID ");
            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 bookPlaceID=@bookPlaceID ");
            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;
 
        }

目录
相关文章
|
3月前
|
开发框架 .NET
未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider
未能找到 CodeDom 提供程序类型“Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider
43 1
|
3月前
|
JSON API 数据格式
【Azure Developer】使用Microsoft Graph API 如何批量创建用户,用户属性中需要包含自定义字段(如:Store_code,Store_name等)
【Azure Developer】使用Microsoft Graph API 如何批量创建用户,用户属性中需要包含自定义字段(如:Store_code,Store_name等)
|
Windows
命名空间“System”中不存在类型或命名空间名称“Windows”(是缺少程序集引用吗?)
命名空间“System”中不存在类型或命名空间名称“Windows”(是缺少程序集引用吗?)
命名空间“System”中不存在类型或命名空间名称“Windows”(是缺少程序集引用吗?)
|
C#
在编写wpf界面时候中出现如下错误: 类型引用不明确。至少有两个名称空间(“System.Windows”和“System.Windows”)中已出现名为“VisualStateManager”的类型。请考虑调整程序集 XmlnsDefinition 特性。
原文:在编写wpf界面时候中出现如下错误: 类型引用不明确。至少有两个名称空间(“System.Windows”和“System.Windows”)中已出现名为“VisualStateManager”的类型。
995 0
|
C#
How do I duplicate a resource reference in code behind in WPF?如何在WPF后台代码中中复制引用的资源?
原文 https://stackoverflow.com/questions/28240528/how-do-i-duplicate-a-resource-reference-in-code-behind-in-wpf 如何在WPF后台代码中中复制引用的资源? In my application, I have a color resources.
833 0