You are on page 1of 4

Prctica de Informtica III: Consulta a base de datos con ADO.

NET SQL2005

using using using using using using using

System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; System.Data.OleDb;

namespace conexionbd { /// <summary> /// Descripcin breve de Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.DataGrid DataGrid1; private System.Windows.Forms.Button consulta; private System.Windows.Forms.Button salir; private System.Data.OleDb.OleDbConnection oleDbConnection1; /// <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.DataGrid1 = new System.Windows.Forms.DataGrid(); this.consulta = new System.Windows.Forms.Button(); this.salir = new System.Windows.Forms.Button(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); ((System.ComponentModel.ISupportInitialize) (this.DataGrid1)).BeginInit(); this.SuspendLayout(); // // DataGrid1 // this.DataGrid1.DataMember = ""; this.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.DataGrid1.Location = new System.Drawing.Point(32, 104); this.DataGrid1.Name = "DataGrid1"; this.DataGrid1.Size = new System.Drawing.Size(440, 240); this.DataGrid1.TabIndex = 0; this.DataGrid1.Navigate += new System.Windows.Forms.NavigateEventHandler(this.DataGrid1_Navigate); // // consulta // this.consulta.Location = new System.Drawing.Point(40, 64); this.consulta.Name = "consulta"; this.consulta.TabIndex = 2; this.consulta.Text = "consulta"; this.consulta.Click += new System.EventHandler(this.consulta_Click); // // salir // this.salir.Location = new System.Drawing.Point(144, 64); this.salir.Name = "salir"; this.salir.TabIndex = 3; this.salir.Text = "salir"; this.salir.Click += new System.EventHandler(this.salir_Click); // // oleDbConnection1 // //

this.oleDbConnection1.ConnectionString = "Provider=SQLOLEDB;Data Source=DAVIDXP\\SQLEXPRESS;Integrated Security=SSPI;Initial" + " Catalog=alumnos"; //

// Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(512, 365); this.Controls.Add(this.salir); this.Controls.Add(this.consulta); this.Controls.Add(this.DataGrid1);

this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((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()); } // Copia el contenido de un DataReader en un DataTable 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 Form1_Load(object sender, System.EventArgs e) { } private void DataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne) { } private void crear_Click(object sender, System.EventArgs e) { } 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 { } } finally {

// Cierra el DataReader alumnos.Close();

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

} private void salir_Click(object sender, System.EventArgs e) { this.Close(); } private void oleDbConnection1_InfoMessage(object sender, System.Data.OleDb.OleDbInfoMessageEventArgs e) { } } }

You might also like