Data Access Using SqlClient
'Con esta clase se pueden ejecutar sentencias de Select, Update, Insert, Detele
Imports System.Data.SqlClient
Public Class DataLayer
Private ConexionStr As String = "Data Source=SERVIDOR\sqlexpress;Initial Catalog=dbAsVentas;Integrated Security=True"
Private strSQL As String
Public Property ConexionString() As String
Get
Return ConexionStr
End Get
Set(ByVal value As String)
ConexionStr = value
End Set
End Property
'''
''' Ejecuta Comandos de Update, Insert, Delete
''' Instruccion SQL que se ejecutara
'''
Public Function SaveData(ByVal SqlInstrucction As String) As Boolean
Dim oConexion As New SqlConnection(ConexionStr)
Dim oComand As SqlCommand
Try
oConexion.Open()
oComand = New SqlCommand(SqlInstrucction, oConexion)
oComand.ExecuteNonQuery()
oConexion.Close()
oConexion.Dispose()
oComand.Dispose()
Return True
Catch ex As Exception 'Error si la conexion falla
MsgBox(ex.Message)
Return False
End Try
End Function
'''
''' Pone los datos recuperados por una Instruccion SQL string en un DataTable
''' Instruccion SQL que se ejecutara
'''
Public Function GetData(ByVal SqlInstrucction As String) As DataTable
Dim oConexion As SqlConnection = New SqlConnection(ConexionStr)
Dim oComannd As SqlCommand
Dim oAdapter As New SqlDataAdapter
Dim dt As New DataTable
Try
oConexion.Open()
oComannd = New SqlCommand(SqlInstrucction, oConexion)
oAdapter.SelectCommand = oComannd
oAdapter.Fill(dt)
oAdapter.Dispose()
oConexion.Close()
oConexion.Dispose()
oComannd.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return dt
End Function
'''
''' Retorna el valor de la Primera columna de la Instruccion SQL que se ejecutada
''' Instruccion SQL que se ejecutara
''' Indica si El Valor Esperado es Numerio
'''
Public Function GetDbValue(ByVal SqlInstrucction As String, Optional ByVal Numerico As Boolean = False) As Object
Dim oConexion As New SqlConnection(ConexionStr)
Dim oComand As SqlCommand
Dim ValorRetorno As Object = ""
Try
oConexion.Open()
oComand = New SqlCommand(SqlInstrucction, oConexion)
ValorRetorno = oComand.ExecuteScalar()
oConexion.Close()
oConexion.Dispose()
oComand.Dispose()
Catch ex As Exception 'Error si la conexion falla
MsgBox(ex.Message)
End Try
ValorRetorno = ValorRetorno.ToString
If Numerico Then
ValorRetorno = IIf(ValorRetorno.ToString = "", 0, ValorRetorno)
ValorRetorno = Format(ValorRetorno * 1, "###,###,###0.0")
End If
Return ValorRetorno
End Function
End Class
Comentarios
Publicar un comentario