Sie sind auf Seite 1von 7

CONEXION # CAPAS C#

Clase Datos

Clase
Funciones

Clase
Objetos

Capa
Presentacion

Cada una de las flechas nos indica las


referencias que tiene cada uno de los
proyectos entre s, los puntos iniciales
indican las dependencias que tiene cada
proyecto, como ejemplo, La Clase
Objetos, es dependiente de Clase Datos,
y la de Clase Funciones y al mismo
Tiempo es tambin la de Capa de
Presentacin.
El proyecto principal es la capa de
presentacin.
La Clase datos, funciones y objetos son
proyectos de tipo Class Library, clase
librera, estas proyecto sern agregadas
de la solucin del proyecto en si y que sea
de tipo Biblioteca de Clases.

Capa Datos
Clase Conexion.cs
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Data.SqlClient;
System.Data;

namespace Clase_Datos
{
public class Conexion
{
#region
private SqlConnection cnn = null;
#endregion
#region Constructor De la Clase para
Instanciarlo.
public
strCadenaConexion)
{

Conexion(string

this.cnn
=
SqlConnection(strCadenaConexion);
}
#endregion
#region Metodos y Funciones

new

public void Conectado()


{
try
{
cnn.Open();
cnn.BeginTransaction()

}
catch (Exception ex)
{
throw
new
Exception("Ha
ocurrido
un
error
al
abrir
la
conexion"+"\n\nDetalles
del
error."+ex.Message);
}
}
public void Desconectado()
{
try
{
cnn.Close();
cnn.Dispose();
cnn.ChangeDatabase("NuevaBaseDatos");//Aca le
escribimos la nueva base de datos.
cnn = null;
}
catch(Exception ex)
{
throw
new
Exception("Ha
Ocurrido un error al desconectar"+"\n\nDetalles
del error"+ex.Message );
}
}
//Funcion: funcion para realizar una
consulta Simple
public
DataTable
Consulta(string
strSQL)
{
try
{
SqlDataAdapter
da
=
new
SqlDataAdapter();
da.SelectCommand
=
new
SqlCommand();
da.SelectCommand.CommandText =
strSQL;
da.SelectCommand.Connection
=
cnn;
DataSet datos = new DataSet();
da.Fill(datos);
return datos.Tables[0];
}
catch(Exception ex)
{
throw new Exception("Ha ocurrido un
error
en
la
Consulta"+"\n\nDetalles
del
error"+ex.Message);
}
}
//Funcion: pasando algunos parametros
para

public
DataTable
Consulta(string
strSQL, object[] objparametros)
{
try
{
SqlDataAdapter
da
=
new
SqlDataAdapter();
da.SelectCommand
=
new
SqlCommand();
da.SelectCommand.CommandText =
strSQL;
//Realizo
el
recorrido
de
algunos parametros
foreach(SqlParameter param in
objparametros)
{
da.SelectCommand.Parameters.Add((SqlParameter)
param);
}
da.SelectCommand.Connection
=
cnn;
DataSet datos = new DataSet();
da.Fill(datos);
return datos.Tables[0];
}
catch (Exception ex)
{
throw
new
Exception("Ha
ocurriod un error al Consultar"+"\n\nDetalles
del Error."+ex.Message );
}
}
public
DataTable
Consulta_StoreProcedure(string
strNombreProcedure, object[]objParametros)
{
try
{
SqlDataAdapter
da
=
new
SqlDataAdapter(strNombreProcedure, cnn);
da.SelectCommand.CommandType =
CommandType.StoredProcedure;
foreach(SqlParameter

param

in

{
try
{
SqlCommand

cmd

new

SqlCommand();
cmd.Connection = cnn;
return cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw
new
Exception("Ha
ocurrido un error al Ejecutar la operacionde
consulta" + "\n\nDetalles del error" +
ex.Message);
}
}
//Funcion: Es la que nos ralizar una
operacion pero con parametros parecido a la
consulta anterior pero con uso
//de parametros.
public
int
EjecutarOperacion(string
strSQL, object[] objParametros)
{
try
{
SqlCommand
cmd
=
new
SqlCommand(strSQL);
foreach(SqlParameter

param

in

objParametros )
{
cmd.Parameters.Add((SqlParameter)param);
}
cmd.Connection = cnn;
return cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw
new
Exception("Ha
ocurrido
un
error
en
Ejecutar
Operacion"+strSQL+"\n\nDetalles
del
error"+ex.Message );
}

objParametros)
{

}
param.IsNullable = true;

da.SelectCommand.Parameters.Add((SqlParameter)
param);
}
DataSet datos = new DataSet();
da.Fill(datos);
return datos.Tables[0];
}
catch (Exception ex)
{
throw
new
Exception("Ha
ocurrido
un
error
de
Consulta
Store
Procedure"+"\n\nDetalles
del
error."+ex.Message);
}
}
//Funcion: Funcion que contenga una
consulta para ejecutar una operacion de la base
de datos.
public
int
EjecutarOperacion(string
strSQL)

public
int
EjecutarOperacion_StoreProcedure(string
strNombreProcedureAlm, object[] objParametros)
{
try
{
SqlCommand
cmd
=
new
SqlCommand(strNombreProcedureAlm, cnn);
cmd.CommandType
=
CommandType.StoredProcedure;
foreach (SqlParameter param in
objParametros)
{
cmd.Parameters.Add((SqlParameter )param);
}
return cmd.ExecuteNonQuery();
}
catch (Exception ex)

{
throw
new
Exception("Ha
Ocurrido un error en Ejecutar Operacion
"+"\n\nDetalles del Error"+ex.Message);

adaptador.SelectCommand.CommandText
sentencia;

foreach (SqlParameter item


in parametros)
{

}
}
#endregion
}
}

adaptador.SelectCommand.Parameters.Add
(item);
}

Conexin satica.cs
using System.Data.SqlClient;
using System.Data;
using System.Threading.Tasks;

DataSet

resultado

new

DataSet();

namespace Clase_Datos
{
public class ConectionSatitc
{
private SqlConnection conn =
new
SqlConnection(@"Data
Source=AMILKAR-PC\AMILKAR;Initial
Catalog=Banco_Online;Integrated
Security=True");
public bool Conectar()
{
try
{
conn.Open();
return true;
}
catch (Exception)
{
return false;
}
}
public void Desconectar()
{
conn.Close();
}
public
DataTable
EjecutarConsultaXxX(string
sentencia,
params SqlParameter[] parametros)
{
//Modo Desconectado
SqlDataAdapter adaptador =
new SqlDataAdapter();
adaptador.SelectCommand
new SqlCommand();

adaptador.SelectCommand.Connection
conn;

adaptador.Fill(resultado);
return resultado.Tables[0];
}
}

Transaccin
Datos

de

la

capa

using System.Data.SqlClient;
using Clase_Objetos;
namespace Clase_Datos
{
public class TransCuentaH
{
//La Cadena de Conexion Paso por
Default que me servira dentro de la
funcion
//Que es lo me dara el Web.config
private string strCadenaConexion
= string.Empty;
//Creo el Contructor de la Clase
TransCuentaH
public
TransCuentaH(string
strCadenaConexion)
{
this.strCadenaConexion
=
strCadenaConexion;
}

public
DataTable
LlenarCombo(Cuenta_habiente
objCuentaHabiente)
{
try
{
//Instancio la conexion
que contiene el Acceso a Datos.
Conexion objAccesoDatos
= new Conexion(strCadenaConexion);
objAccesoDatos.Conectado();

return
objAccesoDatos.Consulta_StoreProcedure
("Mante_cuentaHabiente",
new object []
{
new
SqlParameter
("@cod_cliente",objCuentaHabiente.cod_
cliente !=0 ?(object)objCuentaHabiente
.cod_cliente :DBNull.Value),
new
SqlParameter
("@cui",(object)objCuentaHabiente.cui
??DBNull.Value ),
new
SqlParameter
("@dpi",(object)objCuentaHabiente.dpi?
? DBNull.Value ),
new
SqlParameter
("@nit",(object)objCuentaHabiente.nit
??DBNull.Value ),
new
SqlParameter
("@nombres",(object)objCuentaHabiente.
nombres??DBNull.Value),
new
SqlParameter
("@apellidos",(object)objCuentaHabient
e .apellidos ??DBNull .Value),
new
SqlParameter
("@direccion",(object)objCuentaHabient
e.direccion??DBNull.Value ),
new
SqlParameter
("@telefono",(object)objCuentaHabiente
.telefono??DBNull.Value),
new
SqlParameter
("@email",(object)objCuentaHabiente.em
ail ??DBNull.Value ),
new
SqlParameter
("@fecha_registro",
DBNull.Value),//DE
LO
CONTRARIO
M
DESPLIEGA DATOS DE FECHA INCORRECTO Y
//NO M REEALIZA
LA OPERACION DEL COMBO, EXCELENTE.
new
SqlParameter ("@p_opcion","COMBO")
});
}
catch(Exception ex)
{
throw new Exception("Ha
ocurrido un error en la Operacion del
Combo" + "\n\nDetales de error:\n" +
ex.Message);

}
}
public
int
InsertarCuentaHabiente(Cuenta_habiente
objCuentaHabiente)
{
try
{
Conexion objAccesoDatos
= new Conexion(strCadenaConexion);
objAccesoDatos.Conectado();
int
resultado
=
objAccesoDatos.EjecutarOperacion_Store
Procedure("Mante_cuentaHabiente",
new object[]
{
new
SqlParameter("@cod_cliente",DBNull.Val
ue),
new
SqlParameter("@cui",objCuentaHabiente.
cui),
new
SqlParameter("@dpi",objCuentaHabiente.
dpi),
new
SqlParameter("@nit",objCuentaHabiente.
nit),
new
SqlParameter("@nombres",objCuentaHabie
nte.nombres),
new
SqlParameter("@apellidos",objCuentaHab
iente.apellidos),
new
SqlParameter("@direccion",objCuentaHab
iente.direccion),
new
SqlParameter("@telefono",objCuentaHabi
ente.telefono),
new
SqlParameter("@email",objCuentaHabient
e .email),
new
SqlParameter("@fecha_registro",objCuen
taHabiente .fecha_registro),
new
SqlParameter("@p_opcion","INSERT")
});
objAccesoDatos.Desconectado();
return resultado;
}
catch(Exception)
{
throw;
}

}
public
int
UpdateCuentaHabiente(Cuenta_habiente
objCuentaHabiente)
{
try
{
Conexion objAccesoDatos
= new Conexion(strCadenaConexion);
objAccesoDatos.Conectado();
int
resultado
=
objAccesoDatos.EjecutarOperacion_Store
Procedure("Mante_cuentaHabiente",
new object[]
{
new
SqlParameter("@cod_cliente",objCuentaH
abiente.cod_cliente),
new
SqlParameter("@cui",objCuentaHabiente.
cui),
new
SqlParameter("@dpi",objCuentaHabiente.
dpi),
new
SqlParameter("@nit",objCuentaHabiente.
nit),
new
SqlParameter("@nombres",objCuentaHabie
nte .nombres),
new
SqlParameter("@apellidos",objCuentaHab
iente.apellidos),
new
SqlParameter("@direccion",objCuentaHab
iente.direccion),
new
SqlParameter("@telefono",objCuentaHabi
ente.telefono),
new
SqlParameter("@email",objCuentaHabient
e .email),
new
SqlParameter("@fecha_registro",objCuen
taHabiente.fecha_registro),
new
SqlParameter("@p_opcion","UPDATE")
});
objAccesoDatos.Desconectado();
return resultado;
}
catch (Exception)
{
throw;
}
}

public
int
EliminarCuentaHabiente(Cuenta_habiente
objCuentaHabiente)
{
try
{
Conexion objAccesoDatos
= new Conexion(strCadenaConexion);
objAccesoDatos.Conectado();
int
resultado
=
objAccesoDatos.EjecutarOperacion_Store
Procedure("Mante_cuentaHabiente",
new object[]
{
new
SqlParameter("@cod_cliente",objCuentaH
abiente.cod_cliente),
new
SqlParameter("@cui",objCuentaHabiente.
cui),
new
SqlParameter("@dpi",objCuentaHabiente.
dpi),
new
SqlParameter("@nit",objCuentaHabiente.
nit),
new
SqlParameter("@nombres",objCuentaHabie
nte .nombres),
new
SqlParameter("@apellidos",objCuentaHab
iente.apellidos),
new
SqlParameter("@direccion",objCuentaHab
iente.direccion),
new
SqlParameter("@telefono",objCuentaHabi
ente.telefono),
new
SqlParameter("@email",objCuentaHabient
e .email),
new
SqlParameter("@fecha_registro",objCuen
taHabiente.fecha_registro),
new
SqlParameter("@p_opcion","ELIMINAR")
});
objAccesoDatos.Desconectado();
return resultado;
}
catch(Exception ex)
{
throw new Exception("Ha
ocurrido un error en Eliminar al
Cliente." + "\n\nDetalles del error.\n"
+ ex.Message);

}
}
}
}

Clase Funciones
OperaCuentaH.cs
using System.Data.SqlClient;
using Clase_Objetos;
using Clase_Datos;
namespace Clase_Funciones
{
public class OperaCuentaH
{
//Devolvemos un cadena que este
vacia de la conexion pq el q asegure que
no este
//vacia esl Web.config
private
string
strCadenaConexion = string.Empty;
public
OperaCuentaH(string
strCadenaConexion)
{
this.strCadenaConexion=strCadenaConexi
on;
}
public
DataTable
LlenarCombo(Cuenta_habiente
objCuentaHabiente)
{
try
{
TransCuentaH objTrans=new
TransCuentaH(strCadenaConexion);
return
objTrans.LlenarCombo(objCuentaHabiente
);
}
catch(Exception)
{
throw;
}
}
public
static
List<Cuenta_habiente>
ConsultaCuentaHabientesTipo(int
cod_cliente)
{
try
{
return
TransCuentaH.ConsultaCuentaHabientesTi
po(cod_cliente);

}
catch
{
throw;
}
}
public
int
InsertarCuentaHabiente(Cuenta_habiente
objCuentaHabientes)
{
try
{
TransCuentaH
objTranCuentaH
=
new
TransCuentaH(strCadenaConexion);
return
objTranCuentaH.InsertarCuentaHabiente(
objCuentaHabientes);
}
catch (Exception )
{
throw;
}
}
public
int
UpdateCuentaHabiente(Cuenta_habiente
objCuentaHabientes)
{
try
{
TransCuentaH
objTranCuentaH
=
new
TransCuentaH(strCadenaConexion);
return
objTranCuentaH.UpdateCuentaHabiente(ob
jCuentaHabientes);
}
catch (Exception)
{
throw;
}
}
public
int
EliminarCuentaHabiente(Cuenta_habiente
objCuentaHabiente)
{
try
{
TransCuentaH
objTranCuentaH
=
new
TransCuentaH(strCadenaConexion);
return
objTranCuentaH.EliminarCuentaHabiente(
objCuentaHabiente);
}
catch (Exception)
{

throw;
}
}
}
}

Clase Objetos
Cuenta_Habiente.cs
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Clase_Objetos
{
public class Cuenta_habiente
{
public int cod_cliente { get;
set; }
public int cui { get; set; }
public string dpi { get; set; }
public string nit { get; set; }
public string nombres { get;
set; }
public
string
apellidos{get;set;}
public string direccion { get;
set; }
public
string
telefono{get;set;}
public string email { get; set;
}
public DateTime fecha_registro
{ get; set; }
}
}

Das könnte Ihnen auch gefallen