Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Private Sub bGuardar_Click(ByVal sender As System.Object, ByVal e As System.Even tArgs) Handles bGuardar.

Click Try 'checa q realment el listview tenga items If ListView1.Items.Count = 0 Then MsgBox("No hay datos para agregar", MsgBoxStyle.Information) Exit Sub End If ' Inicializar y abrir la conexin a la base de datos 'Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Dat a Source=datos.mdb") Dim MySqlConnection As MySqlConnection MySqlConnection = New MySqlConnection() MySqlConnection.ConnectionString = "server=localhost; user id= root; password=root;database=curso;" Try MySqlConnection.Open() MessageBox.Show("Conexion a la BD abierta") Catch ex As MySqlException MessageBox.Show("No puede conectarse a la BD" & ex.Message) End Try Dim myadapter As New MySqlDataAdapter With ListView1 For i As Integer = 0 To .Items.Count - 1 Dim sql1 = "INSERT INTO " & _ "alumno(matricula,nombre,apellido_pat,apellido_mat,grupo,pa ss) " & _ "Values('" & .Items(i).Text & _ "','" & .Items(i).SubItems(1).Text "','" & .Items(i).SubItems(2).Text "','" & .Items(i).SubItems(3).Text "','" & .Items(i).SubItems(4).Text "','" & .Items(i).SubItems(5).Text "');" 'Dim sql1 = "LOAD DATA LOCAL INFILE " Dim command As New MySqlCommand command.Connection = MySqlConnection command.CommandText = sql1 myadapter.SelectCommand = command mydata = command.ExecuteReader() Next End With mydata.Close() ' inicializar el comando para insertar los registros 'Dim Comando As New OleDbCommand() 'Comando.Connection = cn ' asigna la conexin al OleDbCommand ' Recorrer los items 'With lv 'For i As Integer = 0 To .Items.Count - 1 ' sql ' Comando.CommandText = "INSERT INTO " & _ '"tContactos(Nombre,Apellido,Telefono) " & _ & & & & & _ _ _ _ _

' "Values('" & .Items(i).Text & _ ' "','" & .Items(i).SubItems(1).Text & _ ' "','" & .Items(i).SubItems(2).Text & _ ' "')" ' Ejecutar el sql ' Comando.ExecuteNonQuery() ' Next ' MsgBox("Listo", MsgBoxStyle.Information, "Exportar a Access") 'End With ' cierra la conexin ' errores Catch ex As Exception MsgBox(ex.Message.ToString, MsgBoxStyle.Critical) End Try End Sub

You might also like