locked
helper for EntLibrary Data Block (ver 4.1) RRS feed

  • Question

  • User1867197125 posted

    Hi all,

    any helper class for reduce code lines  using ent Library:

     

                Database db = DatabaseFactory.CreateDatabase("ConnectionStrings.Oracle.D04PES01");
                using (DbCommand cm = db.GetStoredProcCommand("TBL_POC_TEST_TIPOS.TBL_POC_TEST_TIPOS_FBY_PK"))
                {
                    db.AddInParameter(cm, "P_ID_TEST_TIPOS", DbType.String, id);
                    cm.Parameters.Add(CreateCursorParameter("P_REFCURSOR"));
    
                    // Using "using" will cause both the DataReader and connection to be 
                    // closed. (ExecuteReader will close the connection when the 
                    // DataReader is closed.)
                    using (IDataReader dataReader = db.ExecuteReader(cm))
                    {
                        if (dataReader.Read())
                        {
                            factory.Add(dataReader);
                        }
                        //while (dataReader.Read())
                        //{
                        //    factory.Add(dataReader);
                        //    break;
                        //}
                        return factory.Entity;
                    }
                }
    

    thanks in advance...

     

     

    Tuesday, October 20, 2009 5:29 AM

Answers

  • User-627724879 posted

    Use this as a reference to get you started it is the framework I have been using since 1.x days of the DAAB

    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System;
    using System.Web.Caching;
    using System.Collections.Generic;
    using System.Web;
    using System.Collections;
    
    
    namespace [YourApplicationNamespace].Data
    {
        public abstract class BaseRepository : IDisposable
        {
    
    		private int _cacheDuration = 0;
    		public int CacheDuration
    		{
    
    			get
    			{
    				return _cacheDuration;
    			}
    
    			set
    			{
    				_cacheDuration = value;
    			}
    		}
    
    		private String _connectionString = String.Empty;
    		public String ConnectionString
    		{
    
    			get
    			{
    				return _connectionString;
    			}
    
    			set
    			{
    				_connectionString = value;
    			}
    		}
    
    		public static string CoprsConnectionString
    		{
    			get
    			{
    				return WebContext.CoprsConnectionString;
    			}
    
    		}
    
    		private bool _enableCaching = true;
    		public bool EnableCaching
    		{
    			get
    			{
    				return _enableCaching;
    			}
    			set
    			{
    				_enableCaching = value;
    			}
    		}
    
            protected static Cache AppCache
            {
                get
                {
                    if (null == HttpContext.Current)
                    {
                        return null;
                    }
                    return HttpContext.Current.Cache;
                }
            }
    
    		public Object GetCacheData( string vCacheKey )
    		{
    			if( null == AppCache )
    			{
    				return null;
    			}
    
    			return AppCache[ vCacheKey ];
    		}
    
    		/// <summary>
    		/// Remove from the ASP.NET cache all items whose key starts with the input prefix
    		/// </summary>
    		public void PurgeCacheItems( string prefix )
    		{
    			if( null == AppCache )
    			{
    				return;
    			}
    
    			prefix = prefix.ToLower();
    			List<string> itemsToRemove = new List<string>();
    
    			IDictionaryEnumerator enumerator = AppCache.GetEnumerator();
    			while( enumerator.MoveNext() )
    			{
    				if( enumerator.Key.ToString().ToLower().StartsWith( prefix ) )
    					itemsToRemove.Add( enumerator.Key.ToString() );
    			}
    
    			foreach( string itemToRemove in itemsToRemove )
    				AppCache.Remove( itemToRemove );
    		}
    
    		public void CacheData( string key, object data )
    		{
    			if( null == AppCache )
    			{
    				return;
    			}
    
                if (EnableCaching && data != null)
                {
                    AppCache.Insert(key, data, null,
                       DateTime.Now.AddSeconds(CacheDuration), TimeSpan.Zero);
                }
            }
    
            //protected CacheManager _primitivesCache;
    
    		private String _cacheKey = String.Empty;
    		public String CacheKey
    		{
    
    			get
    			{
    				return _cacheKey;
    			}
    
    			set
    			{
    				_cacheKey = value;
    			}
    		}
    
    		#region " DataSet "
    
    		public DataSet ExecuteDataSet( string sqlCommand )
    		{
    			return this.ExecuteDataSet( sqlCommand, CommandType.StoredProcedure, null );
    		}
    
    		public DataSet ExecuteDataSet( string sqlCommand, CommandType ct )
    		{
    			return this.ExecuteDataSet( sqlCommand, ct, null );
    		}
    
    		public DataSet ExecuteDataSet( string sqlCommand, CommandType ct, string Param, object ParamValue )
    		{
    			SqlParameter[] arParam = new SqlParameter[] { new SqlParameter( Param, ParamValue ) };
    			return this.ExecuteDataSet( sqlCommand, ct, arParam );
    		}
    
    		public DataSet ExecuteDataSet( string sProc, CommandType ct, SqlParameter[] arParam )
    		{
    			Database database = DatabaseFactory.CreateDatabase( ConnectionString );
    			DbCommand storedProcCommand = database.GetStoredProcCommand( sProc );
    			if( null != arParam )
    			{
    				foreach( SqlParameter parameter in arParam )
    				{
    					database.AddInParameter( storedProcCommand, parameter.ParameterName,
    						parameter.DbType, parameter.Value );
    				}
    			}
    			return database.ExecuteDataSet( storedProcCommand );
    		}
    
    
    		#endregion
    
    		#region " ExecuteScalar "
    
    
    		public Object ExecuteScalar( string sqlCommand, CommandType ct )
    		{
    			return this.ExecuteScalar( sqlCommand, ct, null );
    		}
    
    		public Object ExecuteScalar( string sqlCommand, CommandType ct, string Param, object ParamValue )
    		{
    			SqlParameter[] arParam = new SqlParameter[] { new SqlParameter( Param, ParamValue ) };
    			return this.ExecuteScalar( sqlCommand, ct, arParam );
    		}
    
    		public Object ExecuteScalar( string sProc, CommandType ct, SqlParameter[] arParam )
    		{
    			Database database = DatabaseFactory.CreateDatabase( ConnectionString );
    			DbCommand storedProcCommand = database.GetStoredProcCommand( sProc );
    			if( null != arParam )
    			{
    				foreach( SqlParameter parameter in arParam )
    				{
    					database.AddInParameter( storedProcCommand, parameter.ParameterName,
    						parameter.DbType, parameter.Value );
    				}
    			}
    			return ( database.ExecuteScalar( storedProcCommand ).ToString() );
    		}
    
    		#endregion
    
    		#region " NonQuery "
    
    		public int ExecuteNonQuery( string sqlCommand, CommandType ct )
    		{
    			return this.ExecuteNonQuery( sqlCommand, ct, null );
    		}
    
    		public int ExecuteNonQuery( string sqlCommand, CommandType ct, string Param, object ParamValue )
    		{
    			SqlParameter[] arParam = new SqlParameter[] { new SqlParameter( Param, ParamValue ) };
    			return this.ExecuteNonQuery( sqlCommand, ct, arParam );
    		}
    
    		public int ExecuteNonQuery( string sProc, CommandType ct, SqlParameter[] arParam )
    		{
    			Database _database = DatabaseFactory.CreateDatabase( ConnectionString );
    			DbCommand storedProcCommand = _database.GetStoredProcCommand( sProc );
    			if( null != arParam )
    			{
    				foreach( SqlParameter parameter in arParam )
    				{
    					_database.AddInParameter( storedProcCommand, parameter.ParameterName,
    						parameter.DbType, parameter.Value );
    				}
    			}
    			return _database.ExecuteNonQuery( storedProcCommand );
    		}
    
    
    		#endregion
    
    		#region " DataReader "
    
    		public SqlDataReader ExecuteReader( string sqlCommand )
    		{
    			return this.ExecuteReader( sqlCommand, CommandType.StoredProcedure, null );
    		}
    
    		public SqlDataReader ExecuteReader( string sqlCommand, CommandType ct )
    		{
    			return this.ExecuteReader( sqlCommand, ct, null );
    		}
    
    		public SqlDataReader ExecuteReader( string sqlCommand, CommandType ct, string Param, object ParamValue )
    		{
    			SqlParameter[] arParam = new SqlParameter[] { new SqlParameter( Param, ParamValue ) };
    			return this.ExecuteReader( sqlCommand, ct, arParam );
    		}
    
    		public SqlDataReader ExecuteReader( string sProc, CommandType ct, SqlParameter[] arParam )
    		{
    			Database database = DatabaseFactory.CreateDatabase( ConnectionString );
    			DbCommand storedProcCommand = database.GetStoredProcCommand( sProc );
    			
    			if( null != arParam )
    			{
    				foreach( SqlParameter parameter in arParam )
    				{
    					database.AddInParameter( storedProcCommand, parameter.ParameterName,
    						parameter.DbType, parameter.Value );
    				}
    			}
    			return ( SqlDataReader )database.ExecuteReader( storedProcCommand );
    		}
    
    		#endregion
    
    		private Database SetupCommandCall( string sProc, CommandType ct, SqlParameter[] arParam )
    		{
    			Database _database = DatabaseFactory.CreateDatabase( ConnectionString );
    			DbCommand storedProcCommand = _database.GetStoredProcCommand( sProc );
    			
    			if( null != arParam )
    			{
    				foreach( SqlParameter parameter in arParam )
    				{
    					_database.AddInParameter( storedProcCommand, parameter.ParameterName,
    						parameter.DbType, parameter.Value );
    				}
    			}
    			
    			return _database;
    		}
    
    		protected void RemoveAllCache()
    		{}
    
    		protected void RemoveCacheItem( string sCacheName )
    		{}
    
    		#region IDisposable Members
    
    		public void Dispose()
    		{}
    
    		#endregion
    	}
    }
    


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 20, 2009 8:33 AM