A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 熊丽 中级黑马   /  2013-8-1 23:24  /  1144 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用SqlDataAdapter+DataSet
SqlConnection con = new SqlConnection();
con.ConnectionString ="server=(local);uid=sa;pwd=sa;database=Northwind;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Employees";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
con.Open();//打开数据库连接
DataSet ds = new DataSet();
sda.Fill(ds, "Employees");//用Employees表填充数据集
con.Close();//关闭数据库连接
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
用SqlDataReader
SqlConnection con = new SqlConnection
ConnectionString ="server=(local);uid=sa;pwd=sa;database=Northwind;";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Employees";
cmd.Connection = con;
con.Open();//打开数据库连接
SqlDataReader sdr = cmd.ExecuteReader();
this.GridView1.DataSource = sdr;
this.GridView1.DataBind();//将数据绑定到GridView控件中
con.Close();//关闭数据库连接

3 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报

呵呵O(∩_∩)O~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马