0% found this document useful (0 votes)
52 views2 pages

Visualizar Los Registros de Una Tabla en Un Control Listview

This document describes how to display database records from a table in a ListView control in Windows Forms. It instructs the user to design a form with a ListView, button, and two labels. It provides code to connect to a SQL Server database, execute a SQL query, and populate the ListView with the results. The code opens a connection, clears existing columns and items from the ListView, executes a SELECT statement to get employees data, adds columns for each field, then loops through the results to add each row to the ListView. It handles exceptions and closes the connection.

Uploaded by

Anonymous BwTccx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

Visualizar Los Registros de Una Tabla en Un Control Listview

This document describes how to display database records from a table in a ListView control in Windows Forms. It instructs the user to design a form with a ListView, button, and two labels. It provides code to connect to a SQL Server database, execute a SQL query, and populate the ListView with the results. The code opens a connection, clears existing columns and items from the ListView, executes a SELECT statement to get employees data, adds columns for each field, then loops through the results to add each row to the ListView. It handles exceptions and closes the connection.

Uploaded by

Anonymous BwTccx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

VISUALIZAR LOS REGISTROS DE UNA TABLA EN UN CONTROL LISTVIEW

Diseñar el siguiente formulario:

Insertar al formulario 1 listview, un botón y dos etiquetas

ListView Name: lvwResult


GridLines: True
TabIndex:3
View: Details
Button Name: cmdExecute
Text: Cargar
Text Name: txtSql

Escribir el siguiente código:


using System.Data.SqlClient;

private void cmdExecute_Click(object sender, System.EventArgs e)


{
SqlConnection conn = new SqlConnection("server= local;database=Northwind;Integrated
Security=SSPI");
try
{
lvwResult.Columns.Clear() ;
lvwResult.Items.Clear();
conn.Open();
txtSql.Text ="select * from Employees";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = txtSql.Text;
SqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; i< dr.FieldCount; i++)
{
ColumnHeader ch = new ColumnHeader();
ch.Text=dr.GetName(i);
lvwResult.Columns.Add(ch);
}
ListViewItem itmX;
while (dr.Read())
{
itmX=new ListViewItem();
itmX.Text= dr.GetValue(0).ToString();
for (int i=1 ; i< dr.FieldCount; i++)
{
itmX.SubItems.Add(dr.GetValue(i).ToString());
}
lvwResult.Items.Add(itmX);
}
dr.Close();
}
catch ( System.Data.SqlClient.SqlException ex)
{
Console.WriteLine("There was an error in executing the SQL." +
"\nError Message:" + ex.Message, "SQL");
}
finally
{
conn.Close();
}
}

You might also like