Ahora agregamos una nueva clase, damos click derecho sobre nuestro proyecto en el Solution Explorer, nos vamos a la opción Add y luego seleccionamos Class
y colocamos este codigo:
Imports System.Data
Imports System.Data.OleDb
Public Class Class1
Public cnn As New OleDbConnection(My.Settings.yo)
Dim dt As DataTable
Dim oda As OleDbDataAdapter
Dim cmd As OleDbCommand
Public Function CargarDatos() As DataTable
Try
Using oda = New OleDbDataAdapter("select * from imagen", cnn)
dt = New DataTable
oda.Fill(dt)
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return dt
End Function
Public Sub ModificarDatos(ByVal val1, ByVal val2, ByVal val3, ByVal val4)
Dim sql As String = "Update imagen set nombre_foto= '" & val1 & "', comentarios= '" & val2 & "', ruta_foto= '" & val3 & "' where id= '" & val4 & "'"
Try
cnn.Open()
cmd = New OleDbCommand
cmd.Connection = cnn
cmd.CommandText = "update imagen SET nombre_foto = @nfoto, comentarios=@com, ruta_foto=@rfoto where id = @id"
cmd.Parameters.AddWithValue("@nfoto", val1)
cmd.Parameters.AddWithValue("@com", val2)
cmd.Parameters.AddWithValue("@rfoto", val3)
cmd.Parameters.AddWithValue("@cid", val4)
cmd.ExecuteNonQuery()
MessageBox.Show("Dato modificado satisfactoriamente")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cnn.Close()
cmd.Dispose()
End Try
End Sub
Public Sub EliminarDatos(ByVal val1)
Try
cnn.Open()
cmd = New OleDbCommand
cmd.Connection = cnn
cmd.CommandText = "delete from imagen where id = @id"
cmd.Parameters.AddWithValue("@cid", val1)
cmd.ExecuteNonQuery()
MessageBox.Show("Dato eliminado satisfactoriamente")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cnn.Close()
cmd.Dispose()
End Try
End Sub
Public Sub IngresarDatos(ByVal val1, ByVal val2, ByVal val3)
Try
cnn.Open()
cmd = New OleDbCommand
cmd.Connection = cnn
cmd.CommandText = "insert into imagen(nombre_foto, comentarios, ruta_foto) values (@nfoto, @com, @rfoto)"
cmd.Parameters.AddWithValue("@nfoto", val1)
cmd.Parameters.AddWithValue("@com", val2)
cmd.Parameters.AddWithValue("@rfoto", val3)
cmd.ExecuteNonQuery()
MessageBox.Show("Datos ingresados satisfactoriamente")
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cnn.Close()
cmd.Dispose()
End Try
End Sub
End Class
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Dim bs As New BindingSource
Dim access As New Class1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mostrando()
End Sub
Private Sub mostrando()
Try
bs.DataSource = ACCESS.CargarDatos
BindingNavigator1.BindingSource = bs
DataGridView1.DataSource = bs
TextBox1.DataBindings.Clear()
TextBox2.DataBindings.Clear()
TextBox3.DataBindings.Clear()
TextBox4.DataBindings.Clear()
TextBox1.DataBindings.Add("text", bs, "nombre_foto")
TextBox2.DataBindings.Add("text", bs, "comentarios")
TextBox3.DataBindings.Add("text", bs, "ruta_foto")
TextBox4.DataBindings.Add("text", bs, "Id")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Ingresar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ingresar.Click
ACCESS.IngresarDatos(TextBox1.Text, TextBox2.Text, TextBox3.Text)
mostrando()
End Sub
Private Sub Bmodificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bmodificar.Click
ACCESS.ModificarDatos(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text)
mostrando()
End Sub
Private Sub Beliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Beliminar.Click
ACCESS.EliminarDatos(TextBox4.Text)
mostrando()
End Sub
End Class
Compilamos, ejecutamos y ya.