使用Command、DataReader和DataSet两种方法实现数据绑定

方法1:使用Command和DataReader

SqlConnection con = new SqlConnection("server=.;database=Department;uid=sa;pwd=123456");
con.Open();
string sqlStr = "select * from emp";
SqlCommand sqlCmd = new SqlCommand(sqlStr, con);
SqlDataReader reader = sqlCmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();

方法2:使用DataSet数据集(DataAdapter可以无需打开(con.Open()方法),可以自己实现)

            SqlDataAdapter sda = new SqlDataAdapter();
SqlConnection con = DB.createCon();
SqlCommand cmd = new SqlCommand();
string sqlStr = "select * from emp";
sda.SelectCommand = new SqlCommand(sqlStr, con);
DataSet ds = new DataSet();
sda.Fill(ds, "employee");
GridView1.DataSource = ds.Tables["employee"].DefaultView;
GridView1.DataBind();

Fill方法隐式执行DataAdapter的SelectCommand中的SQL查询,查询的结果用于定义DataSet表的结构,并用数据来填充表。

SqlCommand和SqlDataAdapter的区别

SqlCommand对应DataReader  SqlDataAdapter对应DataSet DataAdapter表示一组SQL命令和一个数据库连接,它们用于填充DataSet和更新数据源

.NET中读取数据库中的数据有两种方式,一种是基于连接的方式,就象你所给出的程序那种方式,还有一种就是基于非连接的方式,就是你说的使用DataSet保存结果集。

基于连接方式读取数据时,结果放在DataReader流中(内存流),DataReader是只向前且是只读,本代码中将DataReader的数据读出后放在一个泛型集合里,将泛型集合返回给方法的调用者,方法的调用者就可以对泛型集合进行各种操作。

基于非连接方式读取数据时,需要将查询语句赋给DataAdapter的SelectCommand方法,通过DataAdapter的Fill方法将数据库中的数据填充到DataSet,然后DataSet就可以与数据显示控件进行绑定,进行用户交互了

SQLHelper中的方法:(方法2和此方法其实一样)

SqlDataAdapter是数据适配器,而SqlCommand是命令对象,SqlDataAdapterda = new SqlDataAdapter(cmd);就是执行你的SQL。

CommandType.Text代表执行的是SQL语句

CommandType.StoreProcedure代表执行的是存储过程

CommandType代表要执行的类型

Public Function ExecSelect(ByVal cmdText As String, ByVal cmdType As CommandType, ByVal sqlParams As SqlParameter()) As DataTable  

        Dim sqlAdapter As SqlDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
'还是给cmd赋值
cmd.CommandText = cmdText
cmd.CommandType = cmdType
cmd.Connection = conn
cmd.Parameters.AddRange(sqlParams) '参数添加
sqlAdapter = New SqlDataAdapter(cmd) '实例化adapter
Try
sqlAdapter.Fill(ds) '用adapter将dataSet填充
dt = ds.Tables(0) 'datatable为dataSet的第一个表
cmd.Parameters.Clear() '清除参数
Catch ex As Exception
MsgBox("查询失败", CType(vbOKOnly + MsgBoxStyle.Exclamation, MsgBoxStyle), "警告")
Finally '最后一定要销毁cmd
Call CloseCmd(cmd)
End Try
Return dt
End Function

最新文章

  1. Codeforces Round #381(div 2)
  2. win7使用自带资源管理器来登陆FTP
  3. css3写出0.5px的边框
  4. mouseover与mouseenter与mousemove的区别mouseout与mouseleave的区别
  5. 清除浮动clearfix
  6. mysql循环插入千万级数据
  7. windows快速进入安装目录
  8. (转发)storm 入门原理介绍
  9. Python介绍以及安装
  10. 技术宅学习Linux系统还是很有前途的
  11. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
  12. Android开发之对ListView的数据进行排序
  13. spring not_support 该方法被事务方法调用时 不会加入spring事务 只是执行jdbc普通的事务
  14. vue2.0 tab切换几种方式
  15. Global Mapper
  16. 2017-2018-1 20155226 《信息安全系统设计基础》课下实践——实现mypwd
  17. NO.5:自学python之路------标准库,正则表达式
  18. C++各大名库
  19. IOS中为tableViewCell增加右侧标记(选中或者更多)
  20. C# EF更新当前实体报错 ObjectManager无法管理具有相同键值的多个对象

热门文章

  1. KMP算法的Next数组详解(转)
  2. Flexigrid的使用(整合Struts2)
  3. LeetCode_Merge Two Sorted Lists
  4. mysql基础:登录退出,修改用户密码,添加删除用户
  5. hdu2955(变形01背包)
  6. 华为C8816电信版ROOT过程
  7. ASP.NET 联想控件(Autocomplete)测试可用 ascx
  8. Uva10290 - {Sum+=i++} to Reach N
  9. VS2010升级VS2013后,出现没有定义类型“PowerPacks.ShapeContainer”错误解决方法
  10. UVA 10820 Send a Table euler_phi功能