Fijate tengo este codigo el cual llamo conexion.vb y tiene varias funciones, esto me lo facilito un amigo.
Código vb:
Ver originalImports System.Data.Odbc
Public Class Conexion
Dim datos As OdbcConnection
Sub New()
Conectar()
End Sub
Sub Conectar()
datos = New OdbcConnection("DSN=bdparley")
datos.Open()
If Not datos.State = ConnectionState.Open Then
MsgBox("Error en el servidor de base de datos")
End If
End Sub
Function ejecutar(ByVal sql As String) As Boolean
Try
Dim comando As OdbcCommand
comando = New OdbcCommand(sql, datos)
comando.ExecuteNonQuery()
ejecutar = True
Catch ex As Exception
ejecutar = False
MsgBox(ex.ToString & " " & sql)
End Try
End Function
Function consultar(ByVal sql As String) As OdbcDataReader
Try
Dim comando As OdbcCommand
comando = New OdbcCommand(sql, datos)
consultar = comando.ExecuteReader()
Catch ex As Exception
consultar = Nothing
MsgBox("errror")
End Try
End Function
Public Sub llenarCombo(ByRef combo As ComboBox, ByVal sql As String)
Try
Dim comando As OdbcCommand
comando = New OdbcCommand(sql, datos)
Dim tabla As OdbcDataReader
tabla = comando.ExecuteReader()
combo.Items.Clear()
While tabla.Read
combo.Items.Add(tabla.GetValue(0))
'tabla.NextResult()
End While
Catch ex As Exception
MsgBox("Error" & ex.ToString)
End Try
End Sub
Function generarCodigo(ByVal Tabla As String, ByVal campoClave As String) As Integer
Dim sql As String = "SELECT MAX(`" & campoClave & "`) FROM `" & Tabla & "`"
Dim comando As OdbcCommand
comando = New OdbcCommand(sql, datos)
Dim rs As Odbc.OdbcDataReader
rs = comando.ExecuteReader
If rs.Read Then
If IsNumeric(rs.GetValue(0)) Then
generarCodigo = rs.GetValue(0) + 1
Else
generarCodigo = 1
End If
Else
generarCodigo = 1
End If
End Function
Function MostrarCampo(ByVal Tabla As String, ByVal Campo As String, ByVal filtro As String) As String
Dim rs As OdbcDataReader
Dim sql As String = "select *from `" & Tabla & "` where " & filtro
Dim comando As OdbcCommand
comando = New OdbcCommand(sql, datos)
rs = comando.ExecuteReader
If Not rs.Read Then
MostrarCampo = ""
Else
MostrarCampo = rs(Campo)
End If
End Function
End Class
de esta manera lleno un combo:
[HIGHLIGHT="vb"]
Sub Inicializar()
Dim conex As New Conexion
conex.llenarCombo(ComboBox1, "select nombre from liga where codDeporte=1")
limpiar()
Bloquear(False)
End Sub
[/HIGHLIGHT]