Вы находитесь на странице: 1из 14

Practica de Informtica III : Altas, Bajas, Consultas y Cambios en SQL utilizando ADO.

NET

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace altas_bajas_consultas_cambios { /// <summary> /// Descripcin breve de Form1. /// </summary> public class Form1 : System.Windows.Forms.Form {

private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox caja1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox caja2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox caja3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox caja4; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox caja5; private System.Windows.Forms.Button alta; private System.Windows.Forms.Button baja; private System.Windows.Forms.Button consulta; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox caja6; private System.Windows.Forms.Label label7; private System.Windows.Forms.TextBox caja7; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.RadioButton calif1; private System.Windows.Forms.RadioButton calif2; private System.Windows.Forms.RadioButton calif3; private System.Windows.Forms.DataGrid dataGrid1; private System.Windows.Forms.Label label8; private System.Windows.Forms.TextBox caja8; private System.Windows.Forms.Button modifica; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Windows.Forms.Button salir; /// <summary> /// Variable del diseador requerida. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Necesario para admitir el Diseador de Windows Forms // InitializeComponent(); // // TODO: agregar cdigo de constructor despus de llamar a InitializeComponent // } /// <summary> /// Limpiar los recursos que se estn utilizando. /// </summary> protected override void Dispose( bool disposing )

{ if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Cdigo generado por el Diseador de Windows Forms /// <summary> /// Mtodo necesario para admitir el Diseador. No se puede modificar /// el contenido del mtodo con el editor de cdigo. /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.caja1 = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.caja2 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.caja3 = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.caja4 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.caja5 = new System.Windows.Forms.TextBox(); this.alta = new System.Windows.Forms.Button(); this.baja = new System.Windows.Forms.Button(); this.consulta = new System.Windows.Forms.Button(); this.label6 = new System.Windows.Forms.Label(); this.caja6 = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.caja7 = new System.Windows.Forms.TextBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.calif3 = new System.Windows.Forms.RadioButton(); this.calif2 = new System.Windows.Forms.RadioButton(); this.calif1 = new System.Windows.Forms.RadioButton(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.label8 = new System.Windows.Forms.Label(); this.caja8 = new System.Windows.Forms.TextBox(); this.modifica = new System.Windows.Forms.Button(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.salir = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout();

((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(32, 24); this.label1.Name = "label1"; this.label1.TabIndex = 0; this.label1.Text = "matricula"; // // caja1 // this.caja1.Location = new System.Drawing.Point(152, 24); this.caja1.Name = "caja1"; this.caja1.Size = new System.Drawing.Size(64, 20); this.caja1.TabIndex = 1; this.caja1.Text = ""; // // label2 // this.label2.Location = new System.Drawing.Point(32, 72); this.label2.Name = "label2"; this.label2.TabIndex = 2; this.label2.Text = "nombre"; // // caja2 // this.caja2.Location = new System.Drawing.Point(152, 72); this.caja2.Name = "caja2"; this.caja2.Size = new System.Drawing.Size(160, 20); this.caja2.TabIndex = 3; this.caja2.Text = ""; // // label3 // this.label3.Location = new System.Drawing.Point(32, 120); this.label3.Name = "label3"; this.label3.TabIndex = 4; this.label3.Text = "calificacion1"; // // caja3 // this.caja3.Location = new System.Drawing.Point(152, 120); this.caja3.Name = "caja3"; this.caja3.Size = new System.Drawing.Size(64, 20); this.caja3.TabIndex = 5; this.caja3.Text = "";

// // label4 // this.label4.Location = new System.Drawing.Point(32, 168); this.label4.Name = "label4"; this.label4.TabIndex = 6; this.label4.Text = "calificacion2"; // // caja4 // this.caja4.Location = new System.Drawing.Point(152, 168); this.caja4.Name = "caja4"; this.caja4.Size = new System.Drawing.Size(64, 20); this.caja4.TabIndex = 7; this.caja4.Text = ""; // // label5 // this.label5.Location = new System.Drawing.Point(32, 216); this.label5.Name = "label5"; this.label5.TabIndex = 8; this.label5.Text = "calificacion3"; // // caja5 // this.caja5.Location = new System.Drawing.Point(152, 208); this.caja5.Name = "caja5"; this.caja5.Size = new System.Drawing.Size(64, 20); this.caja5.TabIndex = 9; this.caja5.Text = ""; // // alta // this.alta.Location = new System.Drawing.Point(40, 256); this.alta.Name = "alta"; this.alta.TabIndex = 10; this.alta.Text = "alta alumno"; this.alta.Click += new System.EventHandler(this.alta_Click); // // baja // this.baja.Location = new System.Drawing.Point(560, 16); this.baja.Name = "baja"; this.baja.TabIndex = 11; this.baja.Text = "baja alumno";

this.baja.Click += new System.EventHandler(this.baja_Click); // // consulta // this.consulta.Location = new System.Drawing.Point(160, 256); this.consulta.Name = "consulta"; this.consulta.TabIndex = 12; this.consulta.Text = "consulta"; this.consulta.Click += new System.EventHandler(this.consulta_Click); // // label6 // this.label6.Location = new System.Drawing.Point(320, 16); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(120, 23); this.label6.TabIndex = 13; this.label6.Text = "matricula a dar de baja"; // // caja6 // this.caja6.Location = new System.Drawing.Point(456, 16); this.caja6.Name = "caja6"; this.caja6.Size = new System.Drawing.Size(64, 20); this.caja6.TabIndex = 14; this.caja6.Text = ""; // // label7 // this.label7.Location = new System.Drawing.Point(312, 120); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(200, 23); this.label7.TabIndex = 15; this.label7.Text = "matricula a cambiar de calificaciones"; // // caja7 // this.caja7.Location = new System.Drawing.Point(536, 112); this.caja7.Name = "caja7"; this.caja7.Size = new System.Drawing.Size(72, 20); this.caja7.TabIndex = 16; this.caja7.Text = ""; // // groupBox1 //

this.groupBox1.Controls.Add(this.calif3); this.groupBox1.Controls.Add(this.calif2); this.groupBox1.Controls.Add(this.calif1); this.groupBox1.Location = new System.Drawing.Point(320, 152); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(304, 168); this.groupBox1.TabIndex = 17; this.groupBox1.TabStop = false; this.groupBox1.Text = "seleccionar calificacion a cambiar"; // // calif3 // this.calif3.Location = new System.Drawing.Point(24, 120); this.calif3.Name = "calif3"; this.calif3.TabIndex = 2; this.calif3.Text = "calificacion3"; // // calif2 // this.calif2.Location = new System.Drawing.Point(24, 72); this.calif2.Name = "calif2"; this.calif2.Size = new System.Drawing.Size(96, 24); this.calif2.TabIndex = 1; this.calif2.Text = "calificacion2"; // // calif1 // this.calif1.Location = new System.Drawing.Point(24, 32); this.calif1.Name = "calif1"; this.calif1.TabIndex = 0; this.calif1.Text = "calificacion1"; // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(48, 408); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(584, 200); this.dataGrid1.TabIndex = 18; // // label8 //

this.label8.Location = new System.Drawing.Point(328, 328); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(120, 24); this.label8.TabIndex = 19; this.label8.Text = "nueva calificacion"; // // caja8 // this.caja8.Location = new System.Drawing.Point(488, 328); this.caja8.Name = "caja8"; this.caja8.Size = new System.Drawing.Size(64, 20); this.caja8.TabIndex = 20; this.caja8.Text = ""; // // modifica // this.modifica.Location = new System.Drawing.Point(376, 368); this.modifica.Name = "modifica"; this.modifica.TabIndex = 21; this.modifica.Text = "modifica"; this.modifica.Click += new System.EventHandler(this.modifica_Click); // // oleDbConnection1 // this.oleDbConnection1.ConnectionString = @"Auto Translate=True;Integrated Security=SSPI;User ID=sa;Data Source=DRXP;Tag with column collation when possible=False;Initial Catalog=alumnosutsc;Use Procedure for Prepare=1;Provider=""SQLOLEDB.1"";Persist Security Info=False;Workstation ID=DRXP;Use Encryption for Data=False;Packet Size=4096"; // // salir // this.salir.Location = new System.Drawing.Point(48, 336); this.salir.Name = "salir"; this.salir.TabIndex = 22; this.salir.Text = "salir"; this.salir.Click += new System.EventHandler(this.salir_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(680, 630); this.Controls.Add(this.salir); this.Controls.Add(this.modifica);

this.Controls.Add(this.caja8); this.Controls.Add(this.label8); this.Controls.Add(this.dataGrid1); this.Controls.Add(this.groupBox1); this.Controls.Add(this.caja7); this.Controls.Add(this.label7); this.Controls.Add(this.caja6); this.Controls.Add(this.label6); this.Controls.Add(this.consulta); this.Controls.Add(this.baja); this.Controls.Add(this.alta); this.Controls.Add(this.caja5); this.Controls.Add(this.label5); this.Controls.Add(this.caja4); this.Controls.Add(this.label4); this.Controls.Add(this.caja3); this.Controls.Add(this.label3); this.Controls.Add(this.caja2); this.Controls.Add(this.label2); this.Controls.Add(this.caja1); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// Punto de entrada principal de la aplicacin. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } DataTable ConvDeDataReader(IDataReader DR) { // Crea un objeto DataTable que contenga los resultados DataTable Tbl = new DataTable(); // Ajusta el nombre de las columnas del DataTable for(int i = 0; i < DR.FieldCount; i++) {

Tbl.Columns.Add(DR.GetName(i)); } // Crea un arreglo ("array") que contenga los valores de la lnea object[] Linea = new object[DR.FieldCount]; // Lee todos los registros while (DR.Read()) { // Inserta los valores del registro actual DR.GetValues(Linea); // Agrega el registro al objeto DataTable Tbl.Rows.Add(Linea); } return Tbl; } private void consulta_Click(object sender, System.EventArgs e) {// Abre la conexin oleDbConnection1.Open(); try { // Crea el objeto asociado al comando SQL OleDbCommand Cmd = new OleDbCommand("select * from alumno",oleDbConnection1); OleDbDataReader alumnos; // Ejecuta el comando y coloca el resultado en el DataReader alumnos = Cmd.ExecuteReader(); try { // Agrega los valores y los coloca en un DataTable DataTable Tbl = ConvDeDataReader(alumnos); // Muestra el DataTable en el grid dataGrid1.DataSource = Tbl; } finally { // Cierra el DataReader alumnos.Close(); } } finally { // Cierra la conexin

oleDbConnection1.Close(); } }

private void alta_Click(object sender, System.EventArgs e) { if(caja1.Text!=""&&caja2.Text!=""&&caja3.Text! =""&&caja4.Text!=""&&caja5.Text!="") { oleDbConnection1.Open(); bool existe; try { OleDbCommand Cmd2 = new OleDbCommand("select matricula from alumno where(matricula='"+caja1.Text+"')",oleDbConnection1); OleDbDataReader alumnos; alumnos=Cmd2.ExecuteReader(); existe=alumnos.Read(); alumnos.Close(); // Crea el objeto asociado al comando SQL if(existe==false) { OleDbCommand Cmd = new OleDbCommand("insert into alumno (matricula,nombre,calificacion1,calificacion2,calificacion3) values ('"+caja1.Text+"','"+caja2.Text+"','"+caja3.Text+"','"+caja4.Text+"','"+caja5.Text+"')",o leDbConnection1); Cmd.ExecuteNonQuery(); } else MessageBox.Show("la matricula ya existe"); } finally { // Cierra la conexin oleDbConnection1.Close();

} } else MessageBox.Show("Faltan campos por llenar"); caja1.Clear(); caja2.Clear(); caja3.Clear(); caja4.Clear(); caja5.Clear(); } private void baja_Click(object sender, System.EventArgs e) { if(caja6.Text!="") { oleDbConnection1.Open(); bool existe; try { OleDbCommand Cmd2 = new OleDbCommand("select matricula from alumno where(matricula='"+caja6.Text+"')",oleDbConnection1); OleDbDataReader alumnos; alumnos=Cmd2.ExecuteReader(); existe=alumnos.Read(); alumnos.Close(); // Crea el objeto asociado al comando SQL if(existe==true) { OleDbCommand Cmd = new OleDbCommand("delete from alumno where(matricula ='"+caja6.Text+"')",oleDbConnection1); Cmd.ExecuteNonQuery(); } else MessageBox.Show("la matricula NO existe");

} finally { // Cierra la conexin oleDbConnection1.Close();

} } else MessageBox.Show("El campo matricula esta vacio"); caja6.Clear(); } private void modifica_Click(object sender, System.EventArgs e) { if(caja7.Text!=""&&(calif1.Checked==true || calif2.Checked==true ||calif3.Checked==true )&& caja8.Text!="") { oleDbConnection1.Open(); bool existe; try { OleDbCommand Cmd2 = new OleDbCommand("select matricula from alumno where(matricula='"+caja7.Text+"')",oleDbConnection1); OleDbDataReader alumnos; alumnos=Cmd2.ExecuteReader(); existe=alumnos.Read(); alumnos.Close(); if(existe==true) { // Crea el objeto asociado al comando SQL if(calif1.Checked==true) { OleDbCommand Cmd = new OleDbCommand("update alumno set calificacion1 ='"+caja8.Text+"' where(matricula ='"+caja7.Text+"')",oleDbConnection1); Cmd.ExecuteNonQuery(); } if(calif2.Checked==true) { OleDbCommand Cmd = new OleDbCommand("update alumno set calificacion2 ='"+caja8.Text+"' where(matricula ='"+caja7.Text+"')",oleDbConnection1); Cmd.ExecuteNonQuery(); } if(calif3.Checked==true) { OleDbCommand Cmd = new OleDbCommand("update alumno set calificacion3 ='"+caja8.Text+"' where(matricula ='"+caja7.Text+"')",oleDbConnection1); Cmd.ExecuteNonQuery(); }

} else MessageBox.Show("la matricula NO existe"); } finally { // Cierra la conexin oleDbConnection1.Close(); } } else MessageBox.Show("faltan campos por llenar"); caja7.Clear(); calif1.Checked=false; calif2.Checked=false; calif3.Checked=false; caja8.Clear(); } private void salir_Click(object sender, System.EventArgs e) { this.Close(); } } }

Вам также может понравиться