程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> csharp: 用Enterprise Library對象實體綁定數據,csharpenterprise

csharp: 用Enterprise Library對象實體綁定數據,csharpenterprise

編輯:C#入門知識

csharp: 用Enterprise Library對象實體綁定數據,csharpenterprise


 

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;

        }

 http://www.codeproject.com/Articles/12035/Updating-a-DataSet-with-Multiple-Tables-using-Ente 

// Dbcommandwrapper
// Database myDatabase = DatabaseFactory.CreateDatabase();
// DBCommandWrapper myCommand = myDatabase.GetStoredProcCommandWrapper("sp_ConsignedItemsInsert");

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved