Ver Mensaje Individual
  #7 (permalink)  
Antiguo 12/03/2012, 13:52
snowdogs
 
Fecha de Ingreso: noviembre-2009
Mensajes: 89
Antigüedad: 14 años, 5 meses
Puntos: 1
Respuesta: Botones Dinamicos

Fijate tengo este codigo el cual llamo conexion.vb y tiene varias funciones, esto me lo facilito un amigo.

Código vb:
Ver original
  1. Imports System.Data.Odbc
  2. Public Class Conexion
  3.     Dim datos As OdbcConnection
  4.     Sub New()
  5.         Conectar()
  6.     End Sub
  7.     Sub Conectar()
  8.         datos = New OdbcConnection("DSN=bdparley")
  9.         datos.Open()
  10.         If Not datos.State = ConnectionState.Open Then
  11.             MsgBox("Error en el servidor de base de datos")
  12.         End If
  13.     End Sub
  14.     Function ejecutar(ByVal sql As String) As Boolean
  15.         Try
  16.             Dim comando As OdbcCommand
  17.             comando = New OdbcCommand(sql, datos)
  18.             comando.ExecuteNonQuery()
  19.             ejecutar = True
  20.         Catch ex As Exception
  21.             ejecutar = False
  22.             MsgBox(ex.ToString & " " & sql)
  23.  
  24.         End Try
  25.  
  26.     End Function
  27.     Function consultar(ByVal sql As String) As OdbcDataReader
  28.         Try
  29.             Dim comando As OdbcCommand
  30.             comando = New OdbcCommand(sql, datos)
  31.             consultar = comando.ExecuteReader()
  32.         Catch ex As Exception
  33.             consultar = Nothing
  34.             MsgBox("errror")
  35.  
  36.         End Try
  37.  
  38.     End Function
  39.     Public Sub llenarCombo(ByRef combo As ComboBox, ByVal sql As String)
  40.         Try
  41.             Dim comando As OdbcCommand
  42.             comando = New OdbcCommand(sql, datos)
  43.             Dim tabla As OdbcDataReader
  44.             tabla = comando.ExecuteReader()
  45.             combo.Items.Clear()
  46.             While tabla.Read
  47.                 combo.Items.Add(tabla.GetValue(0))
  48.                 'tabla.NextResult()
  49.            End While
  50.         Catch ex As Exception
  51.             MsgBox("Error" & ex.ToString)
  52.  
  53.         End Try
  54.  
  55.     End Sub
  56.     Function generarCodigo(ByVal Tabla As String, ByVal campoClave As String) As Integer
  57.         Dim sql As String = "SELECT MAX(`" & campoClave & "`) FROM `" & Tabla & "`"
  58.         Dim comando As OdbcCommand
  59.         comando = New OdbcCommand(sql, datos)
  60.         Dim rs As Odbc.OdbcDataReader
  61.         rs = comando.ExecuteReader
  62.         If rs.Read Then
  63.             If IsNumeric(rs.GetValue(0)) Then
  64.                 generarCodigo = rs.GetValue(0) + 1
  65.             Else
  66.                 generarCodigo = 1
  67.             End If
  68.  
  69.         Else
  70.             generarCodigo = 1
  71.         End If
  72.     End Function
  73.  
  74.  
  75.     Function MostrarCampo(ByVal Tabla As String, ByVal Campo As String, ByVal filtro As String) As String
  76.         Dim rs As OdbcDataReader
  77.         Dim sql As String = "select *from `" & Tabla & "` where " & filtro
  78.         Dim comando As OdbcCommand
  79.         comando = New OdbcCommand(sql, datos)
  80.  
  81.         rs = comando.ExecuteReader
  82.         If Not rs.Read Then
  83.             MostrarCampo = ""
  84.         Else
  85.             MostrarCampo = rs(Campo)
  86.         End If
  87.  
  88.     End Function
  89.  
  90. End Class
  91.  
  92.  
  93. de esta manera lleno un combo:
  94.  
  95. [HIGHLIGHT="vb"]
  96. Sub Inicializar()
  97.         Dim conex As New Conexion
  98.         conex.llenarCombo(ComboBox1, "select nombre from liga where codDeporte=1")
  99.         limpiar()
  100.         Bloquear(False)
  101.  
  102.     End Sub
[/HIGHLIGHT]