"DSN Prueba Uid " " PWD " " ": Conexion Odbcdatareader Odbcconnection Odbccommand
"DSN Prueba Uid " " PWD " " ": Conexion Odbcdatareader Odbcconnection Odbccommand
OdbcCommand Dim usuario, password As String Public Sub conexion(ByVal usuario As String, ByVal password As String) Try If conectar.State = ConnectionState.Closed Then conectar.ConnectionString = "DSN=prueba;Uid=" & usuario & ";pwd=" & password & ";" conectar.Open() End If Catch ex As Exception MsgBox("No se puede establecer la conexion a la base de datos") End Try End Sub Public Sub New(ByVal usuario As String, ByVal password As String) End Sub Public Sub New() conexion(usuario, password) End Sub Public Sub ddl(ByVal peticion As String) Try If Not conectar.State = ConnectionState.Open Then conectar.Open() End If Dim Comando As New Odbc.OdbcCommand(peticion, conectar) Comando.ExecuteNonQuery() conectar.Close() MsgBox("Proceso realizado con exito.") Catch ex As Exception conectar.Close() MsgBox(ex.Message, vbCritical, "Error") End Try End Sub Function Consulta(ByVal Peticion As String) As Odbc.OdbcDataReader Try If Not conectar.State = ConnectionState.Open Then conectar.Open() End If Dim Comando As New Odbc.OdbcCommand(Peticion, conectar) Comando.ExecuteNonQuery() Reader = Comando.ExecuteReader Return Reader Catch ex As Exception conectar.Close() MsgBox(ex.Message, MsgBoxStyle.Critical, "Error") Return Nothing End Try End Function End Class
Public Class Prueba Public Conec As New Conexion("root", "root") Public peticion As String Public buscar As Odbc.OdbcDataReader Private Sub Prueba_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click peticion = "select * from CatalogoCuentas where DescripcionCuenta=" & Me.TextBox2.Text buscar = Conec.Consulta(peticion) While buscar.Read MsgBox("Ya existe la Cuenta con ese Nombre") Exit Sub End While peticion = "insert into CatalogoCuentas values('" & Me.TextBox1.Text & "','" & Me.TextBox1.Text & "','" & Me.TextBox1.Text & "')" Conec.ddl(peticion) End Sub End Class