práctica de informática iii consulta a bd con ado sql2005
TRANSCRIPT
-
7/30/2019 Prctica de Informtica III consulta a bd con ado sql2005
1/4
-
7/30/2019 Prctica de Informtica III consulta a bd con ado sql2005
2/4
// TODO: agregar cdigo de constructor despus de llamar aInitializeComponent
//}
////// Limpiar los recursos que se estn utilizando.///
protectedoverridevoid Dispose( bool disposing ){
if( disposing ){
if (components != null){
components.Dispose();}
}base.Dispose( disposing );
}
#region Cdigo generado por el Diseador de Windows Forms////// Mtodo necesario para admitir el Diseador. No se puede modificar/// el contenido del mtodo con el editor de cdigo.///privatevoid 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;IntegratedSecurity=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);
-
7/30/2019 Prctica de Informtica III consulta a bd con ado sql2005
3/4
this.Name = "Form1";this.Text = "Form1";this.Load += new System.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)
(this.DataGrid1)).EndInit();this.ResumeLayout(false);
}
#endregion
////// Punto de entrada principal de la aplicacin.///[STAThread]staticvoid Main(){
Application.Run(new Form1());}
// Copia el contenido de un DataReader en un DataTableDataTable ConvDeDataReader(IDataReader DR){
// Crea un objeto DataTable que contenga los resultadosDataTable Tbl = new DataTable();// Ajusta el nombre de las columnas del DataTablefor(int i = 0; i < DR.FieldCount; i++){
Tbl.Columns.Add(DR.GetName(i));}// Crea un arreglo ("array") que contenga los valores de la lneaobject[] Linea = newobject[DR.FieldCount];// Lee todos los registroswhile (DR.Read()){
// Inserta los valores del registro actualDR.GetValues(Linea);// Agrega el registro al objeto DataTableTbl.Rows.Add(Linea);
}return Tbl;
}
privatevoid Form1_Load(object sender, System.EventArgs e){
}
privatevoid DataGrid1_Navigate(object sender,System.Windows.Forms.NavigateEventArgs ne)
{
}
privatevoid crear_Click(object sender, System.EventArgs e){
}
privatevoid consulta_Click(object sender, System.EventArgs e){
// Abre la conexinoleDbConnection1.Open();try{
// Crea el objeto asociado al comando SQLOleDbCommand Cmd = new OleDbCommand("select * from alumno",oleDbConnection1);
OleDbDataReader alumnos;// Ejecuta el comando y coloca el resultado en el
DataReaderalumnos = Cmd.ExecuteReader();try{
// Agrega los valores y los coloca en un DataTableDataTable Tbl = ConvDeDataReader(alumnos);
// Muestra el DataTable en el gridDataGrid1.DataSource = Tbl;
}
-
7/30/2019 Prctica de Informtica III consulta a bd con ado sql2005
4/4
finally{
// Cierra el DataReaderalumnos.Close();
}}finally{
// Cierra la conexinoleDbConnection1.Close();
}
}
privatevoid salir_Click(object sender, System.EventArgs e){
this.Close();}
privatevoid oleDbConnection1_InfoMessage(object sender,System.Data.OleDb.OleDbInfoMessageEventArgs e)
{
}}
}