You are on page 1of 10

PRACTICA # 4: MICROSOFT VISUAL STUDIO 2005.

- VISUAL C# (Versin con Base de Datos Access 2007) PROBLEMA: Hacer una Base de Datos de Libros con los siguientes campos: (Autor, titulo, subtitulo, materia, editorial, clasificacin, precio, foto.) Deber ser un sistema con las siguientes opciones: . DESPLIEGUE DE LIBROS INSERCCION DE REGISTROS DE LIBROS EDICION EN DATOS EN REGISTROS DE LIBROS BUSQUEDA SELECTIVA POR CLASIFICACION DEL LIBRO BAJAS DE REGISTROS DE LIBROS

CREAR LA SIGUIENTE BASE DE DATOS Nombre de la Base de Datos: BIBLIO ruta: c:\BIBLIO.accdb Tabla: libro Campos: clave autonumrico autor texto titulo texto subtitulo texto materia texto editorial texto clasificacin texto precio numro doble foto texto

libro
clave autor titulo subtitulo materia editorial clasificacion precio foto QA45.455 1500 s/f 5000 s/f 2000 s/f 1 Aho, Alfred V. Estructuras de Datos y Estructuras Algoritmos Algoritmos y Estructura de Datos Curso de Programacion Inftormatica Addison II Wesley Informatica II Algoritmos

2 Joyanes Aguilar, Fundamentos de Luis Programacin 3 Ceballos, MicroSoft C# Francisco Javier

McGraw Hill QA878.J2 Alfa Omega QH34.C23

CREAR UNA APLICACIN DE VISIAL C# (Archivo, nuevo proyecto, aplicacin para

Windows.- Nombre: bib01) 1.- CREAR UNA VENTANA DE DESPLIEGUE DE LIBROS

CONTROLES: Colocar ahora en FORM1 un componente DATAGRIDVIEW con propiedades NAME=GRID1 y READONLY=TRUE Colocar ahora un componente BUTTON1 y en su evento ONCLIK poner el siguiente codigo:
private void button1_Click(object sender, EventArgs e) { // declarando objetos coneccion, adapter y dataset OleDbConnection CANAL; OleDbDataAdapter ORDEN; DataSet TABLA; // creando y enlazando coneccion a la base de datos // para access 2007 CANAL = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\BIBLIO.accdb"); // (para acces 2003) CANAL = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\mibase.mdb"); ORDEN = new OleDbDataAdapter("select * from libro", CANAL); // creando y cargando el dataset TABLA = new DataSet(); ORDEN.Fill(TABLA, "libro"); // cargando y enlazando el DataGridView GRID1.DataSource = TABLA; GRID1.DataMember = "libro"; }

using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;

using System.Data.OleDb;
Nota: agregar al principio esta librera para hacer la conexin de la Base de Datos

2.- HACER UNA APLICACIN DE INSERSION DE REGISTROS.- (Archivo, nuevo proyecto, aplicacin para Windows.- Nombre: bib02)

Controles.9 Label s 7 TextBox 1 Button

name=AUTOR, el que le corresponda

Colocar ahora un componente BUTTON1 y en su evento ONCLIK poner el siguiente cdigo:


// CONTADOR ES UNA VARIABLE GLOBAL int cont = 0; private void button1_Click(object sender, EventArgs e)

{ // creando y cargando coneccion y command OleDbConnection CANAL; OleDbCommand ORDEN; // abriendo la coneccion o enlace CANAL = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\BIBLIO.accdb"); // creando y cargando un objeto OLEDBCOMMAND // instruccion sql insert into mitabla(listacampos) values(listadatos) // @variable es una variable de tipo parametro string q = "insert into libro(autor,titulo,subtitulo,materia,editorial,clasificacion,precio) values(@AUTOR,@TITULO,@SUBTITULO,@MATERIA,@EDITORIAL,@CLASI,@PRECIO)"; ORDEN = new OleDbCommand(q, CANAL); ORDEN.Parameters.Add(new OleDbParameter("@AUTOR", OleDbType.VarWChar, 50)); ORDEN.Parameters["@AUTOR"].Value = AUTOR.Text; ORDEN.Parameters.Add(new OleDbParameter("@TITULO", OleDbType.VarWChar, 50)); ORDEN.Parameters["@TITULO"].Value = TITULO.Text; ORDEN.Parameters.Add(new OleDbParameter("@SUBTITULO", OleDbType.VarWChar, 50)); ORDEN.Parameters["@SUBTITULO"].Value = SUBTITULO.Text; ORDEN.Parameters.Add(new OleDbParameter("@MATERIA", OleDbType.VarWChar, 50)); ORDEN.Parameters["@MATERIA"].Value = MATERIA.Text; ORDEN.Parameters.Add(new OleDbParameter("@EDITORIAL", OleDbType.VarWChar, 50)); ORDEN.Parameters["@EDITORIAL"].Value = EDITORIAL.Text; ORDEN.Parameters.Add(new OleDbParameter("@CLASI", OleDbType.VarWChar, 50)); ORDEN.Parameters["@CLASI"].Value = CLASI.Text; ORDEN.Parameters.Add(new OleDbParameter("@PRECIO", OleDbType.Double)); ORDEN.Parameters["@PRECIO"].Value = PRECIO.Text; ORDEN.Connection.Open(); ORDEN.ExecuteNonQuery(); ORDEN.Connection.Close(); // limpiando TEXTBOXS para otra inserccion AUTOR.Text = " "; TITULO.Text = " "; SUBTITULO.Text = " "; MATERIA.Text = " "; EDITORIAL.Text = " "; CLASI.Text = " "; PRECIO.Text = " "; // avisando inserccion cont = cont + 1; label9.Text = "Registro no.- " + cont.ToString() + " Insertado"; }

3.-EDICION DE DATOS DE REGISTROS DE LIBROS.- (Archivo, nuevo proyecto, aplicacin para Windows.- Nombre: bib03)

Controles.9 Label s 8 TextBox 1 Button

name=AUTOR, el que le corresponda

Colocar ahora un componente BUTTON1 y en su evento ONCLIK poner el siguiente cdigo:


private void button1_Click(object sender, EventArgs e) { // Objetos OLEDB que se ocupan OleDbConnection CANAL; OleDbCommand ORDEN; CANAL = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\BIBLIO.accdb"); // Instruccion sql UPDATE string q = "Update libro set autor=@AUTOR, titulo=@TITULO, subtitulo=@SUBTITULO, materia=@MATERIA, editorial=@EDITORIAL, clasificacion=@CLASI, precio=@PRECIO where clave=" + CLAVE.Text; ORDEN = new OleDbCommand(q, CANAL); ORDEN.Parameters.Add(new OleDbParameter("@AUTOR", OleDbType.VarWChar, 50)); ORDEN.Parameters["@AUTOR"].Value = AUTOR.Text; ORDEN.Parameters.Add(new OleDbParameter("@TITULO", OleDbType.VarWChar, 50)); ORDEN.Parameters["@TITULO"].Value = TITULO.Text; ORDEN.Parameters.Add(new OleDbParameter("@SUBTITULO", OleDbType.VarWChar, 50)); ORDEN.Parameters["@SUBTITULO"].Value = SUBTITULO.Text; ORDEN.Parameters.Add(new OleDbParameter("@MATERIA", OleDbType.VarWChar, 50)); ORDEN.Parameters["@MATERIA"].Value = MATERIA.Text;

ORDEN.Parameters.Add(new OleDbParameter("@EDITORIAL", OleDbType.VarWChar, 50)); ORDEN.Parameters["@EDITORIAL"].Value = EDITORIAL.Text; ORDEN.Parameters.Add(new OleDbParameter("@CLASI", OleDbType.VarWChar, 50)); ORDEN.Parameters["@CLASI"].Value = CLASI.Text; ORDEN.Parameters.Add(new OleDbParameter("@PRECIO", OleDbType.Double)); ORDEN.Parameters["@PRECIO"].Value = PRECIO.Text; ORDEN.Connection.Open(); ORDEN.ExecuteNonQuery(); ORDEN.Connection.Close(); // Limpiando TEXTBOXS para otra edicion AUTOR.Text = " "; TITULO.Text = " "; SUBTITULO.Text = " "; MATERIA.Text = " "; EDITORIAL.Text = " "; CLASI.Text = " "; PRECIO.Text = " ";

// Avisando edicion label9.Text = "REGISTRO EDITADO"; }

4.- BUSQUEDA SELECTIVA POR CLASIFICACION DEL LIBRO.- (Archivo, nuevo proyecto, aplicacin para Windows.- Nombre: bib04)

Controles.2 Label s 1TextBox name=CLASI 1 DATAGRIDVIEW con propiedades NAME=GRID1

Colocar ahora un componente BUTTON1 y en su evento ONCLIK poner el siguiente cdigo:


private void button1_Click(object sender, EventArgs e) { // Objetos OLEDB que se ocupan OleDbConnection CANAL; DataSet TABLA; OleDbDataAdapter ORDEN; CANAL = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\BIBLIO.accdb"); string q = "select * from libro where clasificacion = @CLASI"; ORDEN = new OleDbDataAdapter(q, CANAL); ORDEN.SelectCommand.Parameters.Add(new OleDbParameter("@CLASI", OleDbType.VarChar, 50)); ORDEN.SelectCommand.Parameters["@CLASI"].Value = CLASI.Text; // Creando el dataset y cargandolo TABLA = new DataSet(); ORDEN.Fill(TABLA, "libro"); // Cargando el datagridview GRID1.DataSource = TABLA; GRID1.DataMember = "libro"; }

5.- BAJAS DE REGISTROS DE LIBROS (Archivo, nuevo proyecto, aplicacin para Windows.- Nombre: bib05)

Controles.2 Label s 1TextBox

name=CLAVE

1 button Colocar ahora un componente BUTTON1 y en su evento ONCLIK poner el siguiente cdigo:
private void button1_Click(object sender, EventArgs e) { // Objetos OLEDB que se ocupan OleDbConnection CANAL; OleDbCommand ORDEN; CANAL = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\BIBLIO.accdb"); // Instruccion sql DELETE FROM TABLA WHERE CLAVE=DATO string q = "delete from libro where clave=@CLAVE"; ORDEN = new OleDbCommand(q, CANAL); ORDEN.Parameters.Add(new OleDbParameter("@CLAVE", OleDbType.Integer)); ORDEN.Parameters["@CLAVE"].Value = CLAVE.Text; ORDEN.Connection.Open(); ORDEN.ExecuteNonQuery(); ORDEN.Connection.Close(); // Avisando label2.Text = " REGISTRO ELIMINADO"; }

6.-INTEGRACION DEL SISTEMA EN UN SOLO PROYECTO: MENU PRINCIPAL (Archivo, nuevo proyecto, aplicacin para Windows.- Nombre: bib00) EN ESTE CASO TODAS LAS FORMAS SERAN AGREGADAS EN ESTE PROYECTO PARA CADA BOTON DEFINIDO (se hace un solo proyecto)

Proyecto, Agregar Windows Forms, Nombre: Form2.cs

EN Form2.cs se programa la aplicacin de despligue de datos. As sucesivamente con las aplicaciones siguientes.

PARA CADA BOTON DE Form1.cs (forma del men principal) LIGAR LAS FORMAS CON EL SIGUIENTE CODIGO. Se construye el objeto y se muestra la ventana
private void button1_Click(object sender, EventArgs e) { Form2 ventana2 = new Form2(); } ventana2.Show();

ASI SUCESIVAMENTE PARA LAS OTRAS APLICACIONES


Form3 ventana3 = new Form3(); ventana3.Show();

You might also like