dataset用法
dataset用法
dataset用法一、DataSet的介绍
1、DataSet类是ADO.NET中最核心的成员之一,它是从 数据源中检索到的数据在内存中的缓存。DataSet 由一组 DataTable 对象组成,每一个DataSet都有很多个DataTables和Relationships。RelationShip应该也是一种表,特殊的是,这个表只是用来联系两个数据表的。每一个DataTable都有很多datarows和datacols, 也包括ParentRelations,ChildRelations 和一些限制条件像主键不可以重复的限制。
2、DataSet的功能有浏览、排序、搜索、过滤、处理分级数据、缓存更改等。还可以与XML数据互换。
3、每一个DataSet都有很多个DataTables和Relationships。RelationShip应该也是一种表,特殊的是,这个表只是用来联系两个数据表的。每一个DataTable都有很多datarows和datacols, 也包括ParentRelations,ChildRelations 和一些限制条件像主键不可以重复的限制。
4、在从数据库完成数据抽取后,DataSet就是数据的存放地,它是各种数据源中的数据在计算机内存中映射成的缓存,所以有时说DataSet可以看成是一个数据容器。
二、DataSet对象的特性
1、独立性。DataSet独立于各种数据源。
2、离线(断开)和连接。
3、DataSet对象是一个可以用XML形式表示的 数据视图,是一种数据关系视图。
4、可以在任何时候查看DataSet中任意行的内容,允许修改查询结果的方法。
5、XML的完整性:DataSet对象和XML文档几乎是可互换的。
三、DataSet使用方法一般有
1、把数据库中的数据通过DataAdapter对象填充DataSet
四、DataSet具体用法实例
1、创建DataSet对象:
DataSet ds = new DataSet("DataSetName");
2、查看调用SqlDataAdapter.Fill创建的结构
da.Fill(ds,"Orders");
DataTable tbl = ds.Table[0];
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(col.ColumnName);
3、查看SqlDataAdapter返回的数据
①、DataRow对象
DataTable tbl = ds.Table[0];
DataRow row = tbl.Row[0];
Console.WriteLine(ros["OrderID"]);
②、检查存储在DataRow中的数据
DataTable tbl = row.Table;
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(row[col]);
4、校验DataSet中的数据
①、校验DataColumn的属性:ReadOnly,AllowDBNull,MaxLength,Unique
②、DataTable对象的Constrains集合:UiqueConstraints,Primarykey, ForeignkeyConstraints
通常不必刻意去创建ForeignkeyConstraints,因为当在DataSet的两个DataTable对象之间创建关系时会创建一个。
③、用SqlDataAdapter.Fill模式来检索模式信息
5、编写代码创建DataTable对象
①创建DataTable对象:
DataTable tbl = new DataTable("TableName");
②将DataTable添加到DataSet对象的Table集合
DataSet ds = new DataSet();
DataTable tbl = new DataTable("Customers");
ds.Tables.Add(tbl);
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add("Customers");
DataTable对象只能存在于至多一个DataSet对象中。如果希望将DataTable添加到多个DataSet中,就必须使用Copy方法或Clone方法。Copy方法创建一个与原DataTable结构相同并且包含相同行的新DataTable;Clone方法创建一个与原DataTable结构相同,但没有包含任何行的新DataTable。
③为DataTable添加列
DataTable tbl = ds.Tables.Add("Orders");
DataColumn col =tbl.Columns.Add("OrderID",typeof(int));
col.AllowDBNull = false;
col.MaxLength = 5;
col.Unique = true;
tbl.PrimaryKey = new DataColumn[]{tbl.Columns["CustomersID"]};
当设置主键时,AllowDBNull自动设置为False;
④处理自动增量列
DataSet ds = new DataSet();
DataTable tbl = ds.Tables.Add("Orders");
DataColumn col = tbl.Columns.Add("OrderID",typeof(int));
col.AutoIncrement = true;
col.AutoIncrementSeed = -1;
col.AutoIncrementStep = -1;
col.ReadOnly = true;
⑤添加基于表达式的列
tbl.Columns.Add("ItemTotal",typeof(Decimal),"Quantity*UnitPrice");
6、修改DataTable内容
①添加新DataRow
DataRow row = ds.Tables["Customers"].NewRow();
row["CustomerID"] = "ALFKI";
ds.Tables["Customers"].Rows.Add(row);
object[] aValues ={"ALFKI","Alfreds","Anders","030-22222"};
da.Tables["Customers"].LoadDataRow(aValues,false);
②修改当前行
修改行的内容逼供内不会自动修改数据库中相应的内容,对行所做的修改被视为是随后将使用SqlDataAdapter对象来提交交给数据库的待定的更改。
DataRow rowCustomer;
rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer == null)
//没有查找客户
else
{
rowCustomer["CompanyName"] ="NewCompanyName";
rowCustomer["ContactName"] ="NewContactName";
}
//推荐使用这种方式
DataRow rowCustomer;
rowCustomer = ds.Tables["Custoemrs"].Rows.Find("ANTON");
if(rowCustomer == null) //没有查找客户
else
{
rowCustomer.BeginEdit();
rowCustomer["CompanyName"] ="NewCompanyName";]
rowCustomer["ContactName"] ="NewContactName";
rowCustomer.EndEdit();'
}
//null表示不修改该列的数据
obejct[] aCustomer ={null,"NewCompanyName","NewContactName",null}\
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray = aCustomer;
③处理DataRow的空值
//查看是否为空
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
if(rowCustomer.IsNull("Phone"))
Console.WriteLine("It''''s Null");
else
Console.WriteLine("It''''s not Null");
//赋予空值
rowCustomer["Phone"] = DBNull.Value;
④删除DataRow
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.Delete();
⑤清除DataRow
DataRow rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer.ItemArray = aCustomer;
da.Tables["Customers"].Remove(rowCustomer);
或者
ds.Tables["Customers"].RemoveAt(intIndex);
⑥使用DataRow.RowState属性 :Unchanged,Detached,Added,Modified,Deleted
private void DemonstrateRowState()
{
}
⑦检查DataRow中的挂起更改
DataRow rowCustomer;
rowCustomer = ds.Tables["Customers"].Rows.Find("ALFKI");
rowCustomer["CompanyName"] = "NewCompanyName";
string strNewCompanyName,strOldCompanyName;
Console.WriteLine(rowCustomer["CompanyName",DataRowVersion.Current]);
Console.WriteLine(rowCustomer["CompanyName",DataRowVersion.Original]);
⑧遍历DataSet
foreach(DataTable dt in dataSet.Tables)
foreach(DataRow dr in dt.Rows)
foreach(DataColumn dc in dr.Table.Columns)
Console.WriteLine(dr[dc]);
- dataset用法
- dataset详解(DATASET 与 DATAREADER对象有什么区别)
- html5中table属性(Html5之自定义属性data-,dataset)
- ASP.NET中XML与DataSet的相互转换
- 有种尴尬叫朋友圈忘屏蔽,大学生上演社死现场,父母亲自下场吐槽(有种尴尬叫朋友圈忘屏蔽)
- 朋友圈屏蔽你的人,可以直接看淡了(朋友圈屏蔽你的人)
- 金球奖只青睐那些会戴珠宝的女人(金球奖只青睐那些会戴珠宝的女人)
- 浙江省一个县,人口超40万,建县历史超1100年(浙江省一个县人口超40万)
- 五代十国南唐历代国君(五代十国南唐历代国君)
- 飞机引进工程师杨隆 匠人匠心,只争朝夕(飞机引进工程师杨隆)
热门推荐
- C# string 和 stringbuilder 的区别
- SQL SERVER中查看某个时间段内出错的作业
- python 怎么解析中文(Python中一般处理中文的几种方法)
- python中for语句的无限循环(python使用for循环计算0-100的整数的和方法)
- python3.7标准库官方手册(Python3.7 dataclass使用指南小结)
- nginx给需要转发的链接添加参数(Nginx 根据URL带的参数转发的实现)
- laravel常用的辅助函数介绍(Laravel框架表单验证操作实例分析)
- select 1 from 的作用
- webpack打包分析(webpack代码分片的实现)
- iis7如何使用(IIS7的应用程序池详细解析)