ADO.NET关键点DataSet(转)
(1)ADO.NET关键点---结构及概述
(2)ADO.NET关键点---数据提供程序
(3)ADO.NET关键点---DataSet
0、DataSet概述
使用Dataset的几个步骤
1.创建到数据源的连接:
SQLConnection con =new SQLConnection("server=localhost;uid=sa;pwd=;database=pubs");
2.创建DataSetCommand对象:
SQLDataSetCommand cmd =new SQLDataSetCommand("SELECT * FROM Authors", con);
3.创建一个Dataset对象
DataSet ds = new DataSet();
4.调用DataSetCommand的FillData方法填充数据。(该方法会自动控制链接状态)
int iRowCount = cmd.FillDataSet(ds, "Authors");
5.循环读取DataSet中的数据
for(int i=0; i< iRowCount; i++){
DataRow dr = ds.Tables[0].Rows[i];
Console.WriteLine(dr["au_lname"]);
}
1、DataSet类
DataSet类完整的例子:
建立一个与NorthWind示例数据库对应的数据集:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DataSet创建与填充:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DataSet的读取:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Debug.WriteLine("该表拥有的行数" + mytable.Rows.Count.ToString() );
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2、DataTable类
DataTable数据表:
存在于DataSet或DataView的Tables集合属性中,它有三个集合分别对应它的三个属性
属性 |
说明 |
Columns |
存放DataColumn对象 |
Rows |
存放DataRow对象 |
Constraints |
存放Constraint对象 |
删除DataTable:
DataTable tbl_Employee = myDataSet.Tables["Employee"]; //删除表,操作前先要获取引用
If (myDataSet.Tables.CanRemove(tbl_Employee)) myDataSet.Tables.Remove("tbl_Employee");
myDataSet.Tables.Remove(Employee); //直接删除
myDataSet.Tables.Clear(); //全部删除
添加 DataTable:
myDataSet.Tables.Add(myDataTable); //前边完整例子中有这个
DataTable myDataTable = myDataSet.Tables.Add ("Employee"); //添加一个指定名称的DataTable
查询
通过DataTable的Select方法用SQL语句筛选并返回DataRow[]
DataView:
DataView是一个DataTable中DataRow的子集转化为的DataRowView集合(列不变)。
它不但能像DataTable的Select方法一样筛选排序,还能直接用于数据绑定。
常用属性:
属性 |
说明 |
RowFilter |
设置筛选条件(Sql语句) |
Sort |
设置排序条件(列名+DESC) |
RowState |
设置RowState的枚举值 |
3、其它类
类似的情况及其它说明:
添加行、列、约束相似,先新建,然后设置相关属性,然后添加。
设置Column的时候可以用System.Type.GetType方法或者typeof操作符来获取类型。
myTable.Columns.Add("name", typeof(String));
myColumn.DataType = System.Type.GetType (System.String);
myRow.Delete 用于标记删除,而不是直接删除,这样便于更新数据源,使数据库更改。
对每行的数据进行更改时,最终都需要使用myRow.AcceptChanges来确认更新执行。
更新数据集时用到行的状态(RowState)和字段值的版本(Current,Original等)
设置主键 CustomerTable.PrimaryKey =new DataColumn[] {CustomerTable.Columns["Uid"]};
可以添加关系 DataRelation。可以向Constraint集合添加两类约束:
ForeignKeyConstraint
UniqueConstraint
通过主键来筛选一行,返回一个数据行,用Rows属性的Find方法
DataRow ResultRow = tbl_Customers.Rows.Find("ALFKI");
作者: XuGang 网名:钢钢 |
出处: http://xugang.cnblogs.com |
声明: 本文版权归作者和博客园共有。转载时必须保留此段声明,且在文章页面明显位置给出原文连接地址! |