using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataSet Ds = new DataSet();
string strConnectionString = "Data Source=localhost,3601;Network
Library=DBMSSOCN;Initial Catalog=empdb;User ID=myusername;Password=mypwd;";
SqlConnection objconnection = new SqlConnection(strConnectionString);
using (SqlCommand cmd = new SqlCommand("SELECT [empcode] FROM [empdb]",
objconnection))
{
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(Ds);
}
}
var empList = Ds.Tables[0].AsEnumerable().Select(dataRow =>
dataRow.Field<int>("empcode")).ToList();
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.DataSource = empList;
comboBox1.SelectedIndex = 0;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.SelectedItem.ToString();
}
}
}