第一步:创建数据库连接符
private SqlConnection sqlConnection;
string connectionString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
sqlConnection = new SqlConnection(connectionString);
第二步:创建查询语句
try
{
SqlDataAdapter adapter = new SqlDataAdapter("select * from Customers", sqlConnection);
using (adapter)
{
DataTable customer_table = new DataTable();
adapter.Fill(customer_table);
customerList.DisplayMemberPath = "name";
customerList.SelectedValue = "Id";
customerList.ItemsSource = customer_table.DefaultView;
}
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}