当前位置:网站首页>ADO.NET之SqlDataAdpter对象
ADO.NET之SqlDataAdpter对象
2022-07-01 23:23:00 【衢州小风风】
5.1何为SqlDataAdapter
5.1.2 创建并使用SqlDataAdapter对象
strSQL ="select * From tbname";
SqlConnection = new SqlConnection(strConnection);
SqlCommand = new SqlCommand(strSQL,cn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd
SqlDataAdapter 构造函数
//传连接字串,这个方法会生成一个新的SqlConnection对象
SqlDataAdapter da = new SqlDataAdapter(strSQL,strConnection);
//传连接对象
SqlDataAdapter da = new SqlDataAdapter(strSQL,cn);
//传SqlCommand对象
SqlCommand cmd = new SqlCommand(strSQL,cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
5.2.2 从查询中获取结果
1.使用Fill() 方法
调用SqlDataAdapter类的Fill方法会执行储存在SqlDataAdapter中的SqlCommand对象属性中的查询,并将结果储存在DataSet.
以下代码调用Fill方法,并显示储存在DataSet中的结果
//没时间睡觉了,抓紧时间呀,岁月不咬人,时间吹人老。
strSQL ="select userName,userID From customers";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSQL,strConnection);
da.Fill(ds);
foreach(DataRow row in ds.Tables[0].Rows)
Console.Write("{0} - {1}",Row["userID"],row["userName"]);
//这里的Fil方法隐式l创建一个DataTable对象,并且TableName是"Table"
这个值的描述性比较差,可以用以下代码来自定义填充的tableName
SqlDataAdapter da = new SqlDataAdapter(strSQL,cn);
da.TableMappings.Add("Table","customers");//添加表名
DataSet ds = new DataSet();
da.Fill(ds);
Console.Write("Table ={0}",ds.Tables[0].TableName);
//也可用下面的方法指定Fill参数,不用手工添加表名
5.2.2从查询中获取结果
4.使用重载的Fill方法
strSQL ="Select * From customers";
SqlCommand cmd = new SqlCommand(strSQL,cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach(DataRow row in dt.Rows)
Console.Write("{0}-{1}",row[1],row[2]);
用SqlDataAdapter类的Fill方法分页
//从第一条记录开始,填充20条记录到"Products"的DataTable中
SqlDataAdapter.Fill(DataSet,0,20,"Products");
//这个方法很容易对查询进行分页,但其效率比较低。
边栏推荐
- Daily three questions 6.30
- Microservice stability management
- [micro service sentinel] sentinel integrates openfeign
- What is mosaic?
- SWT/ANR问题--SWT 导致 low memory killer(LMK)
- 边缘计算概述
- Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
- Is it safe to choose mobile phone for stock trading account opening in Shanghai?
- Matplotlib常用设置
- Linux基础 —— CentOS7 离线安装 MySQL
猜你喜欢

from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘

Commemorate becoming the first dayus200 tripartite demo contributor

Redis数据类型和应用场景

纪念成为首个DAYUs200三方demo贡献者

How to display real-time 2D map after rviz is opened

2022年危险化学品经营单位安全管理人员考试题及在线模拟考试

使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object

字典、哈希表、数组的概念

Postgresql源码(57)HOT更新为什么性能差距那么大?

Huisheng Huiying 2022 intelligent, fast and simple video editing software
随机推荐
SWT/ANR问题--SWT 导致 kernel fuse deadlock
vs2015 AdminDeployment.xml
Matplotlib common charts
[leetcode] length of the last word [58]
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
ARP报文头部格式和请求流程
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
Matplotlib common settings
力扣今日题-241. 为运算表达式设计优先级
Experience of practical learning of Silicon Valley products
Stm32f030f4 drives tim1637 nixie tube chip
Distance measurement - Hamming distance
Typescript enumeration
TS初次使用、ts类型
共享电商的背后: 共创、共生、共享、共富,共赢的共富精神
SQL optimization
De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
ShanDong Multi-University Training #3
SWT / anr problem - SWT causes kernel fuse deadlock
软件架构的本质