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

Practica Informtica 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> /// Descripcin 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 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.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.textBox4 = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize) (this.dataGrid1)).BeginInit(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(24, 32); this.label1.Name = "label1"; this.label1.TabIndex = 0; this.label1.Text = "usuario"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(152, 32); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(32, 88); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "consulta"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label2 // this.label2.Location = new System.Drawing.Point(40, 152); this.label2.Name = "label2";

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

Info=False;Workstation ID=DAVIDREY;Use Encryption for Data=False;Packet Size=4096"; // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(344, 64); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(272, 216); this.dataGrid1.TabIndex = 10; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(664, 453); this.Controls.Add(this.dataGrid1); this.Controls.Add(this.button2); this.Controls.Add(this.textBox4); this.Controls.Add(this.label4); this.Controls.Add(this.textBox3); this.Controls.Add(this.label3); this.Controls.Add(this.textBox2); this.Controls.Add(this.label2); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; ((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(); MessageBox.Show("Existe"); // Ajusta el nombre de las columnas del DataTable for(int i = 0; i < DR.FieldCount; i++) { Tbl.Columns.Add(DR.GetName(i));

de la lnea

} // Crea un arreglo ("array") que contenga los valores 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 button1_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 usuarios where usuario like '"+textBox1.Text+"'",oleDbConnection1); OleDbCommand Cmd = new OleDbCommand("select * from usuarios ",oleDbConnection1); OleDbDataReader alumnos; // Ejecuta el comando y coloca el resultado en el DataReader alumnos = Cmd.ExecuteReader(); try { en un DataTable ConvDeDataReader(alumnos); // Agrega los valores y los coloca DataTable Tbl = // Muestra el DataTable en el grid dataGrid1.DataSource = Tbl; } finally { // Cierra el DataReader alumnos.Close(); }

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

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

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

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