//创建数据库连接
SqlConnection con = new SqlConnection("server=.;database = test; uid = sa; pwd = 123456");
try
{
//打开数据库连接
con.Open();
//数据适配器,传输数据库数据
SqlDataAdapter sda = new SqlDataAdapter("select id as 编号, name as 姓名 from talbe", con);
DataSet ds = new DataSet();
//填充数据集DataSet
sda.Fill(ds);
//绑定表格
DataGridView1.DataSource = ds.Tables[0];
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
con.Close();
}