0% found this document useful (0 votes)
5 views

CONEXION MYSQL Y ORACLE VB

The document contains a Visual Basic application that connects to MySQL and Oracle databases to manage records of 'afiliados' and 'estudianrte'. It includes functionalities to insert, search, and display records using buttons for saving, searching, and viewing data. Additionally, it provides error handling for database connections and operations.

Uploaded by

u1301640
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CONEXION MYSQL Y ORACLE VB

The document contains a Visual Basic application that connects to MySQL and Oracle databases to manage records of 'afiliados' and 'estudianrte'. It includes functionalities to insert, search, and display records using buttons for saving, searching, and viewing data. Additionally, it provides error handling for database connections and operations.

Uploaded by

u1301640
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Imports MySql.Data.

MySqlClient

Public Class Form1


Dim conexion As MySqlConnection
Dim exito As Boolean = True

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e


As System.EventArgs) Handles MyBase.Load
conexion = New MySqlConnection("server=localhost;User
Id=root;password

='';database=prueba")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal


e As System.EventArgs) Handles Button1.Click

Try
conexion.Open()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
exito = False
End Try

If exito Then
Dim sql As MySqlCommand = New MySqlCommand
sql.Connection = conexion
sql.CommandText = "INSERT INTO afiliados
(codigo,nombre,direccion,telefono)
VALUES('" &
TextBox1.Text & "','" &
TextBox2.Text & "','" &
TextBox3.Text & "','" &
TextBox4.Text & "')"
sql.CommandType = CommandType.Text
sql.ExecuteReader()
conexion.Close()
End If

End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button2.Click
Try
conexion.Open()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
exito = False
End Try

If exito Then
Dim a As Integer
a = Val(TextBox1.Text)
Dim sql As MySqlCommand = New MySqlCommand
sql.Connection = conexion
sql.CommandText = "SELECT * FROM afiliados Where
codigo=" & a & ""
sql.CommandType = CommandType.Text

Dim dr As MySqlDataReader
dr = sql.ExecuteReader()
dr.Read()
If dr.HasRows Then
TextBox2.Text = dr(1)
TextBox3.Text = dr(2)
TextBox4.Text = dr(3)
Else
MsgBox("Registro No Encontrado")
End If

conexion.Close()
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles Button3.Click
Try
conexion.Open()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
exito = False
End Try

If exito Then
Dim da As New MySqlDataAdapter
da.SelectCommand = New MySqlCommand("SELECT * FROM
afiliados", conexion)
Dim dr As New DataTable

da.Fill(dr)
datos.DataSource = dr
conexion.Close()
End If
End Sub
End Class
Imports System.Data.OracleClient
Public Class Form1
Dim con As OracleConnection
Dim exito As Boolean = True

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
con = New OracleConnection("Data Source = XE;User ID = system;Password =
123456;Unicode = true")
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button1.Click
Try
con.Open()
Catch ex As OracleException

MsgBox(ex.Message)
exito = False
End Try

If (exito) Then
MsgBox("parece")
Dim cmm As OracleCommand = New OracleCommand()
cmm.Connection = con
cmm.CommandText = "INSERT INTO estudianrte (codigo,nombre,edad,curso) VALUES('" +
TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "')"
cmm.CommandType = CommandType.Text
cmm.ExecuteReader()
End If
con.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles Button2.Click
Try
con.Open()
Catch ex As OracleException
MsgBox(ex.Message.ToString)
exito = False
End Try

If exito Then
Dim a As Integer
a = Val(TextBox1.Text)
Dim sql As OracleCommand = New OracleCommand
sql.Connection = con
sql.CommandText = "SELECT * FROM estudianrte Where codigo=" & a & ""
sql.CommandType = CommandType.Text

Dim dr As OracleDataReader
dr = sql.ExecuteReader()
dr.Read()
If dr.HasRows Then
TextBox2.Text = dr(1)
TextBox3.Text = dr(2)
TextBox4.Text = dr(3)
Else
MsgBox("Registro No Encontrado")
End If

con.Close()
End If

End Sub
End Class

Buton1:Guardar
Buton2:Buscar
Buton3:Ver

Para validar adiciono al botón guardar así

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Try
conexion.Open()
Catch ex As MySqlException
MsgBox(ex.Message.ToString)
exito = False

End Try
If exito Then
Try
Dim sql As MySqlCommand = New MySqlCommand
sql.Connection = conexion
sql.CommandText = "insert Into afiliados
(codigo,nombre,apellido,telefono)VALUES('" & TextBox1.Text & "','" &
TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "' )"
sql.CommandType = CommandType.Text
sql.ExecuteReader()

Catch ex As MySqlException
MsgBox(ex.Message.ToString)
exito = False
End Try
conexion.Close()

End If
End Sub
Para llevar los registros de la tabla a los textbox..
Private Sub Datos_RowEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles Datos.RowEnter
Dim var As Integer
var = e.RowIndex
TextBox1.Text = Datos.Rows(var).Cells(0).Value.ToString()
TextBox2.Text = Datos.Rows(var).Cells(1).Value.ToString()
TextBox3.Text = Datos.Rows(var).Cells(2).Value.ToString()
TextBox4.Text = Datos.Rows(var).Cells(3).Value.ToString()
End Sub

You might also like