You are on page 1of 3

Imports System.

Data
Imports System.Data.SqlClient
Public Class Consulta
Dim str_conexion As String = "Data Source=LEONARDO_N\SQL_EXPRESS;Initial Catalog
=newWareSoft;Integrated Security=True"
'La cadena de conexion obviamente la tienen que cambiar por la de ustedes.
Dim conexion As New SqlConnection
Dim cmd As SqlCommand
Public Sub New()
End Sub
Public Property srt_conexion() As String
Get
Return Me.str_conexion
End Get
Set(ByVal str As String)
Me.str_conexion = str
End Set
End Property
Public Sub New(ByVal str As String)
Me.str_conexion = str
End Sub
Public Sub consulta_non_query(ByVal consulta As String)
'Este metodo recibe como parametro la consulta completa y sirve para hacer INSER
T, UPDATE Y DELETE
conexion.ConnectionString = str_conexion
cmd = New SqlCommand(consulta, conexion)
conexion.Open()
Try
cmd.ExecuteNonQuery()
MsgBox("La operacion se realizo con exito!", MsgBoxStyle.Information, "Operacion
exitosa!" )
Catch ex As Exception
MsgBox("Error al operar con la base de datos!", MsgBoxStyle.Critical, "Error!" )
End Try
conexion.Close()
End Sub
Public Function consulta_reader(ByVal consulta As String) As DataTable
'Este metodo recibe como parametro la consulta completa y sirve para hacer SELEC
T
Dim dt As New DataTable
conexion.ConnectionString = str_conexion
cmd = New SqlCommand(consulta, conexion)
conexion.Open()
Try
dt.Load(cmd.ExecuteReader())
Catch ex As Exception
MsgBox("Error al operar con la base de datos!", MsgBoxStyle.Critical, "Error!" )
End Try
conexion.Close()
Return dt
End Function
Public Sub cargar_lista(ByRef lista As ListBox, ByVal consulta As String, ByVal
valueMember As String, ByVal displayMember As String)
Dim dt As New Data.DataTable
conexion.ConnectionString = str_conexion
cmd = New SqlCommand(consulta, conexion)
conexion.Open()
Try
dt.Load(cmd.ExecuteReader())
lista.DataSource = dt
lista.ValueMember = valueMember
lista.DisplayMember = displayMember
Catch ex As Exception
MsgBox("Error al operar con la base de datos!", MsgBoxStyle.Critical, "Error!" )
End Try
conexion.Close()
End Sub
Public Sub cargar_combo(ByRef combo As ComboBox, ByVal consulta As String, ByVal
valueMember As String, ByVal displayMember As String)
Dim dt As New DataTable
conexion.ConnectionString = str_conexion
cmd = New SqlCommand(consulta, conexion)
conexion.Open()
Try
dt.Load(cmd.ExecuteReader())
combo.DataSource = dt
combo.ValueMember = valueMember
combo.DisplayMember = displayMember
Catch ex As Exception
MsgBox("Error al operar con la base de datos!", MsgBoxStyle.Critical, "Error!" )
End Try
conexion.Close()
End Sub
Public Function verificar_existencia(ByVal consulta As String) As Boolean

'Devuelve true si existe, entonces no grabamos, o devuelve false si no existe en


toinces debemos grabar.
Dim dt As New DataTable
conexion.ConnectionString = str_conexion
cmd = New SqlCommand(consulta, conexion)
conexion.Open()
Try
dt.Load(cmd.ExecuteReader())
Catch ex As Exception
MsgBox("Error al operar con la base de datos!", MsgBoxStyle.Critical, "Error!" )
End Try
conexion.Close()
If dt.Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
End Class

You might also like