DataGridView显示数据库数据(一)

 

            //创建数据库连接
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();
}

 

posted @ 2012-03-20 17:01  ghypnus  阅读(2104)  评论(0编辑  收藏  举报