using System;
using System.Windows.Forms;
using System.Data.SqlClient;
public class DataReaderTest : Form
{
private ListBox lstNames;
private string connectionString = "Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI";
public DataReaderTest()
{
lstNames = new ListBox();
lstNames.Dock = DockStyle.Fill;
Controls.Add(lstNames);
Load += new EventHandler(DataReaderTest_Load);
}
public static void Main()
{
DataReaderTest t = new DataReaderTest();
Application.Run(t);
}
private void DataReaderTest_Load(object sender, System.EventArgs e)
{
string SQL = "SELECT ContactName FROM Customers";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(SQL, con);
SqlDataReader r = null;
con.Open();
r = cmd.ExecuteReader();
while (r.Read()) {
lstNames.Items.Add(r["ContactName"]);
}
con.Close();
}
}
|