using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; public class ListBoxTableReadingFill : System.Windows.Forms.Form { private System.Data.SqlClient.SqlConnection myConnection; private System.Data.DataSet myDataSet; private System.Data.SqlClient.SqlCommand myCommand; private System.Data.SqlClient.SqlDataAdapter myDataAdapter; private System.Windows.Forms.ListBox employeeList; private System.ComponentModel.Container components = null; public ListBoxTableReadingFill() { InitializeComponent(); string connectionString = "server=(local)\\SQLEXPRESS;database=MyDatabase;Integrated Security=SSPI;"; myConnection = new System.Data.SqlClient.SqlConnection(connectionString); myConnection.Open(); myDataSet = new System.Data.DataSet(); myDataSet.CaseSensitive=true; string commandString = "Select ID, FirstName from Employee"; myCommand = new System.Data.SqlClient.SqlCommand(); myCommand.Connection=myConnection; myCommand.CommandText= commandString; myDataAdapter = new SqlDataAdapter(); myDataAdapter.SelectCommand = myCommand; myDataAdapter.TableMappings.Add("Table", "Employee"); myDataAdapter.Fill(myDataSet); DataTable myDataTable = myDataSet.Tables[0]; foreach (DataRow dataRow in myDataTable.Rows) { employeeList.Items.Add(dataRow["ID"] + ":" + dataRow["FirstName"] ); } } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { this.employeeList = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // employeeList // this.employeeList.Location = new System.Drawing.Point(16, 8); this.employeeList.Name = "employeeList"; this.employeeList.Size = new System.Drawing.Size(216, 147); this.employeeList.TabIndex = 0; // // ListBoxTableReadingFill // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(464, 205); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.employeeList}); this.Name = "ListBoxTableReadingFill"; this.Text = "ListBoxTableReadingFill"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new ListBoxTableReadingFill()); } }