1. CONEXIÓN A BASES DE
DATOS
Pasos para conectar con visual net.
Y Access
Diego Gómez
(Presentación guía para la clase de electiva)
2. • 1. Tener en cuenta el sitio donde se guardo.
• 2. crear la bases de datos y guardarla en el mismo sitio donde se guarda el
proyecto
• 3. Crear un modulo
3. Código conexión
• Public connAs New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersGateway NV79DocumentsVisual Studio
2012ProjectsBasesdedatosBasesdedatosbinDebugbinDebugbd1.accdb;Persist Security Info=false")
• 'cadena de conexion
• Public cmdAs New OleDb.OleDbCommand
• Public drAs OleDb.OleDbDataReader
• Public Sub conectarse()
• 'excepciones
• Try
• conn.Open()
• MsgBox("exitosa")
• Catch ex As Exception
• MsgBox(ex.ToString)
• EndTry
• End Sub
4. CREAR CONSULTA
• Public Sub consultar(ByRef identificacion As String)
• 'parametro de identificacion de la persona
• cmd.Connection = conn
• 'Se utiliza conn que fue la utilizada en la conexion
• cmd.CommandType = CommandType.Text
• If identificacion <> "" Then
• cmd.CommandText = "SELECT NOMBRES, APELLIDOS, CORREO, DIRECCION, IDENTIFICACION FROM PERSONA
WHERE IDPERSONA=" + identificacion
• 'si es en blanco muestra todo
• Else
• 'consulta sin where
• cmd.CommandText = "SELECT NOMBRES, APELLIDOS, CORREO, DIRECCION, IDENTIFICACION FROM PERSONA"
• End If
5. • Try
• 'data reader
• dr = cmd.ExecuteReader()
• If dr.HasRowsThen
• While dr.Read()
• MsgBox(dr(0).ToString + " " + dr(1).ToString + " " + dr(2).ToString + " " + dr(3).ToString + " " + dr(4).ToString + " ")
• End While
• Else
• MsgBox("No datos")
• End If
• dr.Close()
• Catch ex As Exception
• MsgBox(ex.ToString)
• EndTry
• End Sub
• End Module
6. DATA GRID VIEW
• EN EL FORM LOAD CREAR EL SIGUIENTE CODIGO
• Private Sub llenarGrid()
• Dim ds As New DataSet
• Dim dt As New DataTable
• Dim strSql As String = "SELECT IDPERSONA, NOMBRES, APELLIDOS, CORREO, DIRECCION FROM PERSONA"
• 'ADAPTADOR
• Dim adp As New OleDb.OleDbDataAdapter(strSql, conn)
• 'CREAR LA TABLA
• ds.Tables.Add("tabla")
• 'RELLENA LOS DATOS DE LA TABLA
• adp.Fill(ds.Tables("tabla"))
• 'TOMA LOS VALORES DEL DATASET
• Me.DataGridView1.DataSource = ds.Tables("tabla")
• End Sub
7. • LUEGO EN EL FORM LOAD SE DEBE AGREGAR:
• llenarGrid()
• PROPIEDAD ANCHOR
• SE UTILIZAN PARA DEFINIR LA FORMA Y COMPORTAMIENTO DEL DATAGRID
8. Bases de datos en acces
• Tabla (personas)
• iDPersona tipo numérico
• Nombre Tipo texto
• Apellidos tipo texto
• Telefono tipo numérico
• Documento tipo texto
• Ndocumento tipo numerico