Sei sulla pagina 1di 3

PRACTICA DE VB.NET ACCESO A UNA BASE DE DATOS ACCESS DESDE VISUAL BASIC.

NET Hacer una base de datos en Access con el nombre de bd1.mdb y hacer una tabla llamada estudiante con los campos carnet y nombre

En visual basic.net realizar el siguiente formulario con textbox,label,button y el componente datagridview

Agregar un modulo Imports System.Data.OleDb Module Module1


Function conectar() Dim conexion As New OleDbConnection Try ' crear el objeto de conexin conexion.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + CurDir() + "\bd1.accdb" 'conexion.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CurDir() + "\bd1.mdb" ' abrir conexin conexion.Open() Catch oExcep As OleDbException ' si se produce algn error, lo capturamos mediante el objeto ' de excepciones particular para el proveedor de OLEDB MessageBox.Show("Error al conectar con datos" & _ ControlChars.CrLf & _ oExcep.Message & ControlChars.CrLf & _ oExcep.Source()) End Try Return conexion End Function Function cargar_grid(ByVal sql As String, ByVal conn As OleDbConnection) Dim da As New OleDb.OleDbDataAdapter(sql, conn) Dim ds As New DataSet 'llenar el dataset da.Fill(ds) Return ds.Tables(0) End Function End Module

Public Class Form1 Private conn As New OleDb.OleDbConnection Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim sql As String conn = conectar() 'cargar el datagridview con los datos del dataset sql = "select * from Estudiante order by nombre" DataGridView1.DataSource = cargar_grid(sql, conn)

End Sub Private Sub DataGridView1_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentDoubleClick Dim carnet As String carnet = DataGridView1.Rows(e.RowIndex).Cells(0).Value Dim SQL As String SQL = "SELECT * FROM Estudiante WHERE carnet = '" & carnet & "'" Dim cmd As New OleDb.OleDbCommand(SQL, conn) cmd.CommandType = CommandType.Text

Dim lectura As OleDb.OleDbDataReader = cmd.ExecuteReader() If lectura.Read = True Then Me.txtcarnet.Text = lectura("carnet").ToString Me.txtnombre.Text = lectura("nombre").ToString End If lectura.Close()

End Sub Private Sub btnguardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnguardar.Click If txtcarnet.Text = "" Then MessageBox.Show("Digite el carnet") txtcarnet.Focus() Exit Sub End If If txtnombre.Text = "" Then MessageBox.Show("Digite el carnet") txtnombre.Focus() Exit Sub End If Dim SQL, var As String SQL = "select carnet from Estudiante WHERE carnet = '" & txtcarnet.Text & "'" Dim cmd As New OleDb.OleDbCommand(SQL, conn) cmd.CommandType = CommandType.Text Dim lectura As OleDb.OleDbDataReader = cmd.ExecuteReader() If lectura.HasRows Then var = "Actualizado" SQL = "UPDATE Estudiante set nombre='" & txtnombre.Text & "' " & _ " where carnet='" & txtcarnet.Text & "'" Else var = "Guardado" SQL = "INSERT INTO Estudiante values('" & txtcarnet.Text & "'" & _ ",'" & txtnombre.Text & "')" End If lectura.Close() cmd.CommandText = SQL cmd.ExecuteNonQuery() MessageBox.Show("Registro " & var) SQL = "SELECT * FROM Estudiante order by nombre" DataGridView1.DataSource = cargar_grid(SQL, conn) btnuevo_Click(Nothing, Nothing) End Sub Private Sub btnuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnuevo.Click txtcarnet.Text = "" txtnombre.Text = "" txtcarnet.Focus() End Sub End Class

Potrebbero piacerti anche