Public MustInherit Class gDatos
#Region "Declaración de Variables"
Protected mServidor As String
Protected mBase As String
Protected mConexion As System.Data.IdbConnection
Protected mCadenaConexion As String
#End Region
#Region "Propiedades"
'Nombre del Servidor de Base de Datos
Public Property Servidor() As String
Get
Return mServidor
End Get
Set(ByVal Value As String)
mServidor = Value
End Set
End Property
'Nombre de la base de Datos
Public Property Base() As String
Get
Return mBase
End Get
Set(ByVal Value As String)
mBase = Value
End Set
End Property
' Definición de la cadena de Conexión
Public MustOverride Property CadenaConexion() As String
#End Region
#Region "Privadas"
' Devuelve un objeto Conexión
Protected ReadOnly Property Conexion() As System.Data.IDbConnection
Get
If mConexion Is Nothing Then ' si no existe
' llama al método de la clase que lo hereda
mConexion = CrearConexion(Me.CadenaConexion)
End If
With mConexion
' Controla que la conexión esté abierta
If .State <> ConnectionState.Open Then .Open()
End With
Return mConexion
End Get
End Property
#End Region
#Region "Lecturas"
Public Overloads Function TraerDataSet( _
ByVal ProcedimientoAlmacenado As String) _
As System.Data.DataSet
'Se crea el Dataset que luego será llenado y retornado
Dim mDataSet As New System.Data.DataSet()
CrearDataAdapter(ProcedimientoAlmacenado).Fill(mDataSet)
Return mDataSet
End Function
Public Overloads Function TraerDataSet( _
ByVal ProcedimientoAlmacenado As String, _
ByVal ParamArray Argumentos() As System.Object) _
As System.Data.DataSet
Dim mDataSet As New System.Data.DataSet()
CrearDataAdapter( _
ProcedimientoAlmacenado, _
Argumentos).Fill(mDataSet)
Return mDataSet
End Function
Public Overloads Function TraerDataTable( _
ByVal ProcedimientoAlmacenado As String) _
As System.Data.DataTable
Return TraerDataSet( _
ProcedimientoAlmacenado).Tables(0).Copy
End Function
Public Overloads Function TraerDataTable( _
ByVal ProcedimientoAlmacenado As String, _
ByVal ParamArray Argumentos() As System.Object) _
As System.Data.DataTable
Return TraerDataSet( _
ProcedimientoAlmacenado, _
Argumentos).Tables(0).Copy
End Function
Public Overloads Function TraerValor( _
ByVal ProcedimientoAlmacenado As String) _
As System.Object
With Comando(ProcedimientoAlmacenado)
.ExecuteNonQuery()
Dim oPar As System.Data.IDataParameter
For Each oPar In .Parameters
If oPar.Direction = _
ParameterDirection.InputOutput Or _
oPar.Direction = _
ParameterDirection.Output Then
Return oPar.Value
Exit For
End If
Next
End With
End Function
Public Overloads Function TraerValor( _
ByVal ProcedimientoAlmacenado As String, _
ByVal ParamArray Argumentos() As System.Object) _
As System.Object
Dim mCom As System.Data.IDbCommand = _
Comando(ProcedimientoAlmacenado)
CargarParametros(mCom, Argumentos)
With mCom
.ExecuteNonQuery()
Dim oPar As System.Data.IDataParameter
For Each oPar In .Parameters
If oPar.Direction = _
ParameterDirection.InputOutput Or _
oPar.Direction = _
ParameterDirection.Output Then
Return oPar.Value
Exit For
End If
Next
End With
End Function
#End Region
#Region "Acciones"
Protected MustOverride Function CrearConexion( _
ByVal Cadena As String) _
As System.Data.IDbConnection
Protected MustOverride Function Comando( _
ByVal ProcedimientoAlmacenado As String) _
As System.Data.IDbCommand
Protected MustOverride Function CrearDataAdapter( _
ByVal ProcedimientoAlmacenado As String, _
ByVal ParamArray Args() As System.Object) _
As System.Data.IDataAdapter
Protected MustOverride Sub CargarParametros( _
ByVal Comando As System.Data.IDbCommand, _
ByVal Args() As System.Object)
Public Overloads Function Ejecutar( _
ByVal ProcedimientoAlmacenado As String) _
As Integer
Return Comando( _
ProcedimientoAlmacenado).ExecuteNonQuery
End Function
Public Overloads Function Ejecutar( _
ByVal ProcedimientoAlmacenado As String, _
ByVal ParamArray Argumentos() As System.Object) _
As Integer
Dim mCom As System.Data.SqlClient.SqlCommand = _
Comando(ProcedimientoAlmacenado)
Dim Resp As Integer
CargarParametros(mCom, Argumentos)
Resp = mCom.ExecuteNonQuery
Dim oPar As System.Data.SqlClient.SqlParameter
Dim i As Integer
For i = 0 To mCom.Parameters.Count - 1
With mCom.Parameters(i)
If .Direction = ParameterDirection.InputOutput _
Or .Direction = ParameterDirection.Output Then
Argumentos.SetValue(.Value, i - 1)
End If
End With
Next
Return Resp
End Function
#End Region
#Region "Transacciones"
Private mTransaccion As System.Data.IDbTransaction
Private EnTransaccion As Boolean
Public Sub IniciarTransaccion()
mTransaccion = Me.Conexion.BeginTransaction
EnTransaccion = True
End Sub
Public Sub TerminarTransaccion()
Try
mTransaccion.Commit()
Catch ex As System.Exception
Throw ex
Finally
EnTransaccion = False
mTransaccion = Nothing
End Try
End Sub
Public Sub AbortarTransaccion()
Try
mTransaccion.Rollback()
Catch Ex As System.Exception
Throw Ex
Finally
mTransaccion = Nothing
EnTransaccion = False
End Try
End Sub
#End Region
End Class
Esta es Otra.....ojo............................................................................................
Imports System.Data.SqlClient
Protected Const CONNECTION_ERROR_MSG As String = _
"To run this sample, you must have SQL " & _
"or MSDE with the Northwind database installed. For " & _
"instructions on installing MSDE, view the ReadMe file."
Protected Const MSDE_CONNECTION_STRING As String = _
"Server=(local)\NetSDK;" & _
"DataBase=Pachacamac;" & _
"Integrated Security=SSPI"
Protected Const SQL_CONNECTION_STRING As String = _
"Server=localhost;" & _
"DataBase=Pachacamac;" & _
"Integrated Security=SSPI"
Protected strConn As String = SQL_CONNECTION_STRING
Public Class DatosSQLServer
Inherits Curso.Datos.gDatos
Shared mColComandos As New System.Collections.Hashtable
Sub New()
End Sub
Sub New(ByVal CadenaConexion As String)
Me.New()
Me.CadenaConexion = CadenaConexion
End Sub
Sub New(ByVal Servidor As String, ByVal Base As String)
Me.New()
Me.Base = Base
Me.Servidor = Servidor
End Sub
Public Overrides Property CadenaConexion() As String
Get
If Len(MyBase.mCadenaConexion) = 0 Then
If Len(Me.Servidor) <> 0 And Len(Me.Base) <> 0 Then
Dim sCadena As New System.Text.StringBuilder( _
"data source=<SERVIDOR>;" & _
"initial catalog=<BASE>;password='';" & _
"persist security info=True;" & _
"user id=sa;packet size=4096")
sCadena.Replace("<SERVIDOR>", Me.Servidor)
sCadena.Replace("<BASE>", Me.Base)
'Dim sCadena As New System.Text.StringBuilder("Server=localhost;" & _
'"DataBase=Pachacamac;" & _
'"Integrated Security=SSPI")
mCadenaConexion = sCadena.ToString
Else
Throw New _
System.Exception( _
"No se puede establecer la cadena de conexión")
End If
End If
Return mCadenaConexion
End Get
Set(ByVal Value As String)
mCadenaConexion = Value
End Set
End Property
Protected Overrides Sub CargarParametros(ByVal Comando As System.Data.IDbCommand, ByVal Args() As Object)
Dim i As Integer
With Comando
For i = 0 To Args.GetUpperBound(0)
.Parameters(i + 1).Value = Args(i)
Next
End With
End Sub
Protected Overrides Function Comando(ByVal ProcedimientoAlmacenado As String) As System.Data.IDbCommand
Dim mComando As System.Data.SqlClient.SqlCommand
If mColComandos.Contains(ProcedimientoAlmacenado) Then
mComando = _
CType(mColComandos.Item(ProcedimientoAlmacenado) _
, System.Data.SqlClient.SqlCommand)
Else
Dim oConexion2 As New System.Data.SqlClient.SqlConnection(CadenaConexion)
oConexion2.Open()
mComando = New _
System.Data.SqlClient.SqlCommand( _
ProcedimientoAlmacenado, oConexion2)
Dim mConstructor As New _
System.Data.SqlClient.SqlCommandBuilder
mComando.CommandType = CommandType.StoredProcedure
mConstructor.DeriveParameters(mComando)
oConexion2.Close()
mColComandos.Add(ProcedimientoAlmacenado, mComando)
End If
With mComando
.Connection = Me.Conexion
.Transaction = Me.Traer_mTransaccion()
End With
Return mComando
End Function
Protected Overrides Function CrearConexion(ByVal Cadena As String) As System.Data.IDbConnection
Return New System.Data.SqlClient.SqlConnection(Cadena)
End Function
Protected Overrides Function CrearDataAdapter(ByVal ProcedimientoAlmacenado As String, ByVal ParamArray Args() As Object) As System.Data.IDataAdapter
Dim mCom As System.Data.SqlClient.SqlCommand = _
Comando(ProcedimientoAlmacenado)
' Si se han recibido Argumentos,
'se procede a asignar los valores correspondientes
If Not Args Is Nothing Then
CargarParametros(mCom, Args)
End If
Return New System.Data.SqlClient.SqlDataAdapter(mCom)
End Function
End Class