237 lines
10 KiB
C#
237 lines
10 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Data.Common;
|
|
using Microsoft.Practices.EnterpriseLibrary.Data;
|
|
using System.Collections;
|
|
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
|
|
|
|
namespace AyADAL
|
|
{
|
|
public class BaseDAL
|
|
{
|
|
|
|
public string strProcedimiento(ref DbParameter[] p_dbpParametros, string p_strProcedimiento, string p_strStringConexion)
|
|
{
|
|
string strMensaje = string.Empty;
|
|
//DatabaseFactory.SetDatabaseProviderFactory(new DatabaseProviderFactory());
|
|
DatabaseProviderFactory factory = new DatabaseProviderFactory();
|
|
Database dbData = CrearConexion();
|
|
DbCommand dbCommando = null;
|
|
|
|
try
|
|
{
|
|
//if (!string.IsNullOrEmpty(p_strStringConexion))
|
|
// dbData = factory.Create(p_strStringConexion);// DatabaseProviderFactory.CreateDatabase(p_strStringConexion);
|
|
//else
|
|
// dbData = factory.Create(); // DatabaseFactory.CreateDatabase();
|
|
|
|
dbCommando = dbData.GetStoredProcCommand(p_strProcedimiento);
|
|
|
|
if (p_dbpParametros != null)
|
|
{
|
|
for (int i = 0; i < p_dbpParametros.Length; i++)
|
|
{
|
|
if (p_dbpParametros[i] != null && p_dbpParametros[i].Direction == ParameterDirection.Input)
|
|
{
|
|
dbData.AddInParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Value);
|
|
}
|
|
else if (p_dbpParametros[i].Direction == ParameterDirection.InputOutput)
|
|
dbData.AddOutParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Size);
|
|
}
|
|
}
|
|
dbData.ExecuteNonQuery(dbCommando);
|
|
p_dbpParametros[0].Value = dbCommando.Parameters[0].Value;
|
|
p_dbpParametros[1].Value = dbCommando.Parameters[1].Value;
|
|
strMensaje = dbCommando.Parameters["@P_MENSAJE"].Value + " con el Status: " + dbCommando.Parameters["@P_STATUS"].Value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
dbCommando.Connection.Close();
|
|
}
|
|
return strMensaje;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Se utiliza para las opciones de Insert, Update y Delete en la base de datos
|
|
/// </summary>
|
|
/// <param name="p_dbpParametros"></param>
|
|
/// <param name="p_strProcedimiento"></param>
|
|
/// <param name="p_strStringConexion"></param>
|
|
/// <returns></returns>
|
|
|
|
public int intProcedimiento(string p_strProcedimiento,ref DbParameter[] p_dbpParametros, string p_strStringConexion)
|
|
{
|
|
int nrsultado = 0;
|
|
|
|
DatabaseProviderFactory factory = new DatabaseProviderFactory();
|
|
Database dbData = CrearConexion();
|
|
DbCommand dbCommando = null;
|
|
DataSet dsResultado = null;
|
|
try
|
|
{
|
|
//if (!string.IsNullOrEmpty(p_strStringConexion))
|
|
// dbData = factory.Create(p_strStringConexion);
|
|
|
|
dbCommando = dbData.GetStoredProcCommand(p_strProcedimiento);
|
|
|
|
if (p_dbpParametros != null)
|
|
{
|
|
for (int i = 0; i < p_dbpParametros.Length; i++)
|
|
{
|
|
if (p_dbpParametros[i] != null && p_dbpParametros[i].Direction == ParameterDirection.Input)
|
|
{
|
|
dbData.AddInParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Value);
|
|
}
|
|
else if (p_dbpParametros[i].Direction == ParameterDirection.InputOutput)
|
|
dbData.AddOutParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Size);
|
|
}
|
|
}
|
|
dsResultado = dbData.ExecuteDataSet(dbCommando);
|
|
|
|
p_dbpParametros[0].Value = dbCommando.Parameters[0].Value;
|
|
p_dbpParametros[1].Value = dbCommando.Parameters[1].Value;
|
|
|
|
if (dsResultado.Tables.Count > 0)
|
|
{
|
|
if (dsResultado.Tables["Table"].Rows[0]["id"].ToString() != "")
|
|
nrsultado = Convert.ToInt32(dsResultado.Tables["Table"].Rows[0]["id"].ToString());
|
|
}
|
|
//strMensaje = dbCommando.Parameters["@P_MENSAJE"].Value + " con el Status: " + dbCommando.Parameters["@P_STATUS"].Value;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
dbCommando.Connection.Close();
|
|
}
|
|
return nrsultado;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Se utiliza para hacer consultas en la base de datos
|
|
/// </summary>
|
|
/// <param name="p_strProcedimiento"></param>
|
|
/// <param name="p_dbpParametros"></param>
|
|
/// <param name="p_strStringConexion"></param>
|
|
/// <returns></returns>
|
|
|
|
public DataSet dsProcedimientoAlmacenado(string p_strProcedimiento,ref DbParameter[] p_dbpParametros, string p_strStringConexion)
|
|
{
|
|
DataSet dsValoresRetornados = new DataSet();
|
|
|
|
DatabaseProviderFactory factory = new DatabaseProviderFactory();
|
|
Database dbData = CrearConexion();
|
|
DbCommand dbCommando = null;
|
|
|
|
string strMes;
|
|
string strDia;
|
|
|
|
|
|
|
|
int contadorIteracionFecha = 0;
|
|
|
|
try
|
|
{
|
|
//if (!string.IsNullOrEmpty(p_strStringConexion))
|
|
// dbData = new GenericDatabase(ConfigurationManager.ConnectionStrings["AV_Tramites"].ToString(),DbProviderFactories.GetFactory("System.Data.SqlClient"));// factory.crea(p_strStringConexion);
|
|
|
|
dbCommando = dbData.GetStoredProcCommand(p_strProcedimiento);
|
|
|
|
|
|
if (p_dbpParametros != null)
|
|
{
|
|
for (int i = 0; i < p_dbpParametros.Length; i++)
|
|
{
|
|
if (p_dbpParametros[i] != null && p_dbpParametros[i].Direction == ParameterDirection.Input)
|
|
{
|
|
if (p_dbpParametros[i].DbType == DbType.DateTime && p_dbpParametros[i].Value != null)
|
|
{
|
|
DateTime dtFecha = Convert.ToDateTime(p_dbpParametros[i].Value);
|
|
|
|
//Si el mes es menor a 10 da error, hay que agregarle el 0
|
|
if (dtFecha.Month < 10)
|
|
strMes = "0" + dtFecha.Month.ToString();
|
|
else
|
|
strMes = dtFecha.Month.ToString();
|
|
|
|
if (contadorIteracionFecha > 0)
|
|
{
|
|
dtFecha = dtFecha.AddDays(1);
|
|
contadorIteracionFecha = 0;
|
|
}
|
|
else
|
|
{
|
|
contadorIteracionFecha += 1;
|
|
}
|
|
//Si el dia es menor a 10 da error, hay que agregarle el 0
|
|
if (dtFecha.Day < 10)
|
|
strDia = "0" + dtFecha.Day.ToString();
|
|
else
|
|
strDia = dtFecha.Day.ToString();
|
|
|
|
p_dbpParametros[i].Value = dtFecha.Year.ToString() + strMes + strDia;
|
|
p_dbpParametros[i].DbType = DbType.String;
|
|
p_dbpParametros[i].Size = 10;
|
|
|
|
dbData.AddInParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Value);
|
|
|
|
|
|
}
|
|
else
|
|
dbData.AddInParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Value);
|
|
}
|
|
else if (p_dbpParametros[i].Direction == ParameterDirection.InputOutput)
|
|
dbData.AddOutParameter(dbCommando, p_dbpParametros[i].ParameterName, p_dbpParametros[i].DbType, p_dbpParametros[i].Size);
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
dsValoresRetornados = dbData.ExecuteDataSet(dbCommando);
|
|
p_dbpParametros[0].Value = dbCommando.Parameters[0].Value;
|
|
p_dbpParametros[1].Value = dbCommando.Parameters[1].Value;
|
|
//int result = dbData.ExecuteNonQuery(dbCommando);
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
dbCommando.Connection.Close();
|
|
}
|
|
return dsValoresRetornados;
|
|
}
|
|
|
|
private Database CrearConexion()
|
|
{
|
|
Database dbRespuesta = null;
|
|
try
|
|
{
|
|
Helper.CryptographyManager cmManager = new Helper.CryptographyManager();
|
|
dbRespuesta = new GenericDatabase(cmManager.Desencriptar(ConfigurationManager.ConnectionStrings["AV_Tramites"].ToString()),
|
|
DbProviderFactories.GetFactory("System.Data.SqlClient"));
|
|
//dbRespuesta = new GenericDatabase(ConfigurationManager.ConnectionStrings["AV_Tramites"].ToString(),
|
|
// DbProviderFactories.GetFactory("System.Data.SqlClient"));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
|
|
return dbRespuesta;
|
|
}
|
|
}
|
|
}
|