当前位置:网站首页>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");
//这个方法很容易对查询进行分页,但其效率比较低。
边栏推荐
- 从第三次技术革命看企业应用三大开发趋势
- 距离度量 —— 汉明距离(Hamming Distance)
- 通过Go语言创建CA与签发证书
- Linux foundation - centos7 offline installation of MySQL
- Microservice stability management
- Practical application and extension of plain framework
- 【.Net Core】程序相关各种全局文件
- 每日三题 6.29
- [understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing
- 物联网技术应用属于什么专业分类
猜你喜欢
第六章 数据流建模
从第三次技术革命看企业应用三大开发趋势
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
Matplotlib常用图表
SWT / anr problem - SWT causes kernel fuse deadlock
2021 RoboCom 世界机器人开发者大赛-高职组复赛
Concepts of dictionary, hash table and array
2022 crane driver (limited to bridge crane) examination questions and simulation examination
Why is PHP called hypertext preprocessor
2021 RoboCom 世界机器人开发者大赛-高职组初赛
随机推荐
Switch to software testing, knowing these four points is enough!
What professional classification does the application of Internet of things technology belong to
notBlank 和 notEmpty
Practical application and extension of plain framework
Paramètres communs de matplotlib
STM32F030F4驱动TIM1637数码管芯片
typescript枚举
ConcurrentSkipListMap——跳表原理
Is there a piece of code that makes you convinced by human wisdom
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
【ES实战】ES上的安全性运行方式
Zhongang Mining: it has inherent advantages to develop the characteristic chemical industry dominated by fluorine chemical industry
TS初次使用、ts类型
from pip._internal.cli.main import main ModuleNotFoundError: No module named ‘pip‘
jpa手写sql,用自定义实体类接收
2021 RoboCom 世界机器人开发者大赛-本科组初赛
What category does the Internet of things application technology major belong to
sql 优化
【.Net Core】程序相关各种全局文件
mt管理器测试滑雪大冒险