Practica Informática III Datagrid Lista de alumnos

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace consultauli { /// <summary> /// Descripción breve de Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.Button button2; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Windows.Forms.DataGrid dataGrid1; /// <summary> /// Variable del diseñador requerida. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Necesario para admitir el Diseñador de Windows Forms // InitializeComponent();

// // TODO: agregar código de constructor después de llamar a InitializeComponent // } /// <summary> /// Limpiar los recursos que se estén utilizando. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose();

this.Windows. /// </summary> private void InitializeComponent() { this.Name = "textBox1".Label().Location = new System.Button().Click += new System.Windows.Text = "".Label(). this.Label().OleDbConnection().Location = new System. this. this. // // label1 // this.label1 = new System.TextBox(). // // label2 // this.Forms. 32).label2.Windows.Point(40.Forms.Label().TextBox().TabIndex = 0. .Windows.Text = "consulta".Point(24.Forms.Button().TabIndex = 2. this.TabIndex = 1. this.TextBox().textBox1. No se puede modificar /// el contenido del método con el editor de código.Windows. this.label1. this. this.label1.button1. this. this.Forms. // // button1 // this.label2.oleDbConnection1 = new System.Forms.Forms.label3 = new System. this.button1.Forms.Point(32.Forms.button1_Click). this.dataGrid1)). this.DataGrid().ComponentModel.button1.Drawing.button1. this.SuspendLayout().Name = "label1".textBox1.label2 = new System. this.EventHandler(this. this.textBox2 = new System.button2 = new System.Data.Point(152. this.Windows.Location = new System.Windows.Windows.Forms.button1.textBox1.label4 = new System.Location = new System. 32).Windows. 152).Drawing.Forms. this.Windows.Text = "usuario".OleDb.label1. this. this.TextBox().dataGrid1 = new System. 88). this.textBox4 = new System.textBox1 = new System.Windows.Drawing.} } base.Drawing.Name = "button1".textBox1. this. } #region Código generado por el Diseñador de Windows Forms /// <summary> /// Método necesario para admitir el Diseñador.textBox3 = new System.button1 = new System.Name = "label2".Forms.BeginInit(). ((System.Dispose( disposing ).label1. // // textBox1 // this.ISupportInitialize) (this.

Location = new System.label4.label4.TabIndex = 6. this.button2_Click). // // textBox3 // this. 256). this. this.Integrated Security=SSPI. // // label4 // this.label3. 312).Drawing.label4.Drawing.TabIndex = 4.textBox2.Use Procedure for Prepare=1.Location = new System.TabIndex = 9. // // oleDbConnection1 // this.Name = "label4".label3.TabIndex = 5. // // label3 // this.Click += new System.Name = "textBox4".button2.EventHandler(this. this. this. this.label2.User ID=sa. this.textBox2.oleDbConnection1.textBox2.textBox4.Text = "". 200).Text = "nombre".textBox4.Drawing.Data Source=DAVIDREY. this. 256). this.label2. this.Persist Security .Drawing.Name = "textBox2".Tag with column collation when possible=False. this. this.label3. this.Text = "".Point(176.Name = "button2".Text = "alta".Point(40.Drawing. // // textBox2 // this. this.Name = "label3".TabIndex = 7.Point(32.textBox4.button2.Point(176.Text = "password".Location = new System.textBox3. this.1"". this. // // textBox4 // this.Text = "".textBox3.button2. 152).Location = new System.button2.Name = "textBox3". this. 200).TabIndex = 8.this.textBox2.label4. this.textBox3.Initial Catalog=cuentas.Point(176.button2.Location = new System.textBox4.textBox3. this.ConnectionString = @"Auto Translate=True.Point(32.Text = "usuario".Provider=""SQLOLEDB.Location = new System. // // button2 // this. this.TabIndex = 3.label3.Drawing.

Size(272. this.ISupportInitialize) (this.textBox1).dataGrid1.Controls. this.Controls.dataGrid1.label1).AutoScaleBaseSize = new System.SystemColors. this. . this.Run(new Form1()).HeaderForeColor = System.EndInit().Add(this. this.textBox4).button2).Drawing.Controls. this. 13).Text = "Form1".dataGrid1.Controls. this.Add(this.label2). this.label3).FieldCount. // // Form1 // this.Add(this.Drawing.Add(this.Add(this.Add(DR. 64).Use Encryption for Data=False.Size(664.Size(5. this. this. this. } #endregion /// <summary> /// Punto de entrada principal de la aplicación. this.Info=False.Point(344.label4).Drawing.Controls.Controls. this. /// </summary> [STAThread] static void Main() { Application. this. 216).Name = "dataGrid1".dataGrid1.Columns.Show("Existe").textBox3).Size = new System. this.Name = "Form1". // Ajusta el nombre de las columnas del DataTable for(int i = 0. 453).textBox2). i++) { Tbl. MessageBox. this. } DataTable ConvDeDataReader(IDataReader DR) { // Crea un objeto DataTable que contenga los resultados DataTable Tbl = new DataTable().Packet Size=4096".GetName(i)).Workstation ID=DAVIDREY.Drawing.dataGrid1).Drawing.ResumeLayout(false). this.Add(this. this.Add(this.Add(this.ComponentModel.ControlText. ((System.Controls.Add(this.Location = new System. i < DR.Add(this.dataGrid1.Controls. // // dataGrid1 // this.dataGrid1.Controls.Add(this.button1). this. this.Controls.TabIndex = 10.DataMember = "".ClientSize = new System.dataGrid1)).Controls.

de la línea } // Crea un arreglo ("array") que contenga los valores object[] Linea = new object[DR. System.ExecuteReader().FieldCount]. OleDbCommand Cmd = new OleDbCommand("select * from usuarios ". try { en un DataTable ConvDeDataReader(alumnos).Close().GetValues(Linea). OleDbDataReader alumnos. // Agrega los valores y los coloca DataTable Tbl = // Muestra el DataTable en el grid dataGrid1.oleDbConnection1). try { // Crea el objeto asociado al comando SQL //OleDbCommand Cmd = new OleDbCommand("select * from usuarios where usuario like '"+textBox1.DataSource = Tbl. } .Read()) { // Inserta los valores del registro actual DR. } return Tbl. } private void button1_Click(object sender.EventArgs e) { // Abre la conexión oleDbConnection1.Add(Linea).Text+"'".Open(). // Lee todos los registros while (DR.Rows. } } finally { // Cierra la conexión oleDbConnection1. // Agrega el registro al objeto DataTable Tbl. } finally { // Cierra el DataReader alumnos. // Ejecuta el comando y coloca el resultado en el DataReader alumnos = Cmd.oleDbConnection1).Close().

"+textBox4.EventArgs e) { // Abre la conexión oleDbConnection1. el DataReader // Ejecuta el comando y coloca el resultado en Cmd.oleDbConn ection1).Text+"')".} private void button2_Click(object sender.Text+"'. } finally { // Cierra la conexión oleDbConnection1.Close(). } } } } .password) values ('"+textBox2.Text+"'.Open(). try { // Crea el objeto asociado al comando SQL OleDbCommand Cmd = new OleDbCommand("insert into usuarios (nombre."+textBox3.usuario.ExecuteNonQuery(). System.