You are on page 1of 4

ADO .

NET
Aplicación para navegar y editar registros en la tabla “City”
ADO .NET
• Los botones avanzar, retroceder, primero, último e “Ir a” nos van
mostrando los registros en las ventanas de texto.
Private Sub Avanzar(ByVal sender As System.Object, ByVal e As System.EventArgs)
If indice = myDataset.Tables("City").Rows.Count - 1 Then
MsgBox("Ultimo Registro")
Else
indice += 1 ‘índice es un atributo que sirve para saber en que registro estoy dentro de la tabla del
‘dataset
CargarDatos()
End If
End Sub
Private Sub CargarDatos()
Dim fila As DataRow
fila = myDataset.Tables("City").Rows(indice)
Me.TextBox1.Text = fila("ID")
Me.TextBox2.Text = fila("Name")
Me.TextBox3.Text = fila("CountryCode")
Me.TextBox4.Text = fila("District")
Me.TextBox5.Text = fila("Population")
Me.Label5.Text = "Registro: " + Str$(indice) + " de: " + Str$(myDataset.Tables("City").Rows.Count)
End Sub
ADO .NET
• Los botones insertar, modificar y eliminar sirven
para modificar los registros de la bbdd.
• Existen dos maneras diferentes de hacerlo:
1. Mandando Comandos a través del
DataAdapter:
– La clase DataAdapter tiene cuatro tipos diferentes de
comandos: SelectCommand, InsertCommand,
UpdateCommand y DeleteCommand.
– Fill(), ExecuteNonQuery().
2. Modificando el DataSet y utilizando el método:
– myDataAdapter.update(mydataset, “city”)
Private Sub Insertar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button6.Click

Dim InsComm As New MySQLCommand("Insert Into City" + "(ID, Name, CountryCode, District, Population)
VALUES(@ID, @Name, @CountryCode, @District, @Population);", DBCon)

myAdapter.InsertCommand = InsComm
myAdapter.InsertCommand.CommandType = CommandType.Text

myAdapter.InsertCommand.Parameters.Add(New MySQLParameter("@ID", DbType.String))


myAdapter.InsertCommand.Parameters.Add(New MySQLParameter("@Name", DbType.String))
myAdapter.InsertCommand.Parameters.Add(New MySQLParameter("@CountryCode", DbType.String))
myAdapter.InsertCommand.Parameters.Add(New MySQLParameter("@District", DbType.String))
myAdapter.InsertCommand.Parameters.Add(New MySQLParameter("@Population", DbType.String))

myAdapter.InsertCommand.Parameters("@ID").Value = Me.TextBox1.Text
myAdapter.InsertCommand.Parameters("@Name").Value = Me.TextBox2.Text
myAdapter.InsertCommand.Parameters("@CountryCode").Value = Me.TextBox3.Text
myAdapter.InsertCommand.Parameters("@District").Value = Me.TextBox4.Text
myAdapter.InsertCommand.Parameters("@Population").Value = Me.TextBox5.Text

DBCon.Open()
Try
Dim i As Integer = myAdapter.InsertCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
End Try

DBCon.Close()
End Sub

You might also like