este codigo es en CSharp [original]
Código:
using System; using System.Collections.Generic; using System.Text; using System.Data; using Common.Factory; namespace DataAccessLayer { abstract public class DataObjectBase: IDataObject { private IDbTransaction trans = null; private IDbConnection connection = null; private string currentConnection = String.Empty; private DataTable dataTable = null; private DataRow newRow = null; private bool bCloseExternalConnectionAfterUse = false; private string conditionClause = String.Empty; public string commandTextSelect = string.Empty; public string commandTextInsert = string.Empty; public string commandTextUpdate = string.Empty; public string commandTextDeleteByKey = string.Empty; public string commandTextDeleteAll = string.Empty; #region "CONSTRUCTORS" public DataObjectBase() { currentConnection = Shared.SharedVariables.ConnectionString; ConfigureDataObject(); } public DataObjectBase(string connectionString) { connectionString=ConnectionString; ConfigureDataObject(); } public DataObjectBase(string connectionString, string condition) { currentConnection = connectionString; conditionClause = condition; ConfigureDataObject(); } public DataObjectBase(IDbConnection externalConnection) { connection = externalConnection; ConfigureDataObject(); } public DataObjectBase(IDbConnection externalConnection, string condition) { connection = externalConnection; conditionClause = condition; ConfigureDataObject(); } #endregion #region "PROPERTIES" public IDbTransaction Transaction { get { return trans; } set { if (trans == value) { return; } trans = value; } } public IDbConnection Connection { get { return connection; } set { if (connection == value) { return; } connection = value; ConfigureDataObject(); } } public string ConnectionString { get { return currentConnection; } set { if (currentConnection == value) { return; } currentConnection = value; } } public string Condition { get { return conditionClause; } set { if (conditionClause == value) { return; } conditionClause = value; } } public DataTable DataTable { get { dataTable=GetDataTable(); return dataTable; } } public bool CloseExternalConnectionAfterUse { get { return bCloseExternalConnectionAfterUse; } set { if (bCloseExternalConnectionAfterUse == value) return; bCloseExternalConnectionAfterUse = value; } } #endregion #region "CRUID METHODS" public int InsertData(DataRow row) { this.OpenConnection(); int iResult = 0; IDbCommand cmdInsert = DataFactory.CreateCommand(); cmdInsert.CommandText = commandTextInsert; cmdInsert.CommandType = Shared.SharedVariables.DBCommandType; cmdInsert.Connection = this.connection; if (this.trans != null) { cmdInsert.Transaction = trans; } ConfigureParameterValue(ref cmdInsert, ref row); try { iResult = cmdInsert.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { this.CloseConnection(); if (cmdInsert != null) { cmdInsert.Dispose(); cmdInsert = null; } } return iResult; } public int UpdateData(DataRow row) { this.OpenConnection(); int retVal = 0; IDbCommand cmdUpdate = DataFactory.CreateCommand(); cmdUpdate.CommandText = commandTextUpdate; cmdUpdate.CommandType = Shared.SharedVariables.DBCommandType; cmdUpdate.Connection = this.connection; if (this.trans != null) { cmdUpdate.Transaction = trans; } ConfigureParameterValue(ref cmdUpdate, ref row); try { retVal = cmdUpdate.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { this.CloseConnection(); if (cmdUpdate != null) { cmdUpdate.Dispose(); cmdUpdate = null; } } return retVal; } public int DeleteByKey(string strKey) { int retVal = 0; try { retVal = DeleteAll(commandTextDeleteByKey + strKey); } catch (Exception ex) { throw ex; } finally { } return retVal; } public int DeleteAll(string condition) { int retVal = 0; this.OpenConnection(); IDbCommand cmdDelete; cmdDelete = DataFactory.CreateCommand(); cmdDelete.CommandText = commandTextDeleteAll + condition; cmdDelete.Connection = connection; if (trans != null) cmdDelete.Transaction = trans; try { retVal = cmdDelete.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { this.CloseConnection(); if (cmdDelete != null) { cmdDelete.Dispose(); cmdDelete = null; } } return retVal; } #endregion #region "ABSTRACT CONFIGURATION" abstract protected void ConfigureParameterValue(ref IDbCommand cmd, ref DataRow row); abstract protected void ConfigureDataObject(); #endregion #region "OTHERS METHODS" public DataTable GetDataTable() { this.OpenConnection(); IDataAdapter da = DataFactory.CreateDataAdapter(commandTextSelect + this.conditionClause, connection); DataTable dtTemp = null; DataSet dsTemp = null; try { dsTemp = new DataSet(); dtTemp = new DataTable(); da.Fill(dsTemp); if (dsTemp.Tables.Count != 0) dtTemp = dsTemp.Tables[0]; newRow=dtTemp.NewRow(); } catch (Exception ex) { throw ex; } finally { // Clear the memory used by the command this.CloseConnection(); da = null; } return dtTemp; } public int GetRecordCount() { int iResult = 0; this.OpenConnection(); IDbCommand cmd = DataFactory.CreateCommand(); cmd.CommandText=commandTextSelect + this.conditionClause; cmd.Connection = connection; try { iResult = cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { // Clear the memory used by the command this.CloseConnection(); cmd = null; } return iResult; } protected DataRow GetNewRowFormat() { this.OpenConnection(); IDataAdapter da = DataFactory.CreateDataAdapter(commandTextSelect + " WHERE 1=0", connection); DataTable dtTemp = null; DataSet dsTemp = null; try { dsTemp = new DataSet(); dtTemp = new DataTable(); da.Fill(dsTemp); if (dsTemp.Tables.Count != 0) dtTemp = dsTemp.Tables[0]; newRow = dtTemp.NewRow(); } catch (Exception ex) { throw ex; } finally { // Clear the memory used by the command this.CloseConnection(); da = null; } return dtTemp.NewRow(); } public void OpenConnection() { if(currentConnection != "") { if (connection == null) connection = DataFactory.CreateConnection(); if (connection.State == ConnectionState.Closed) { connection.ConnectionString = this.currentConnection; connection.Open(); } } else { if(connection.State==ConnectionState.Closed) { connection.Open(); bCloseExternalConnectionAfterUse=true; } } } public void CloseConnection() { if(currentConnection != "") { if(connection!=null) { connection.Dispose(); connection=null; } } else { if(bCloseExternalConnectionAfterUse) connection.Close(); } } public DataRow NewRow() { if(newRow==null) newRow=GetNewRowFormat(); return newRow; } #endregion } }