DataSet详细用法(最全面)讲义资料

上传人:yulij****0329 文档编号:134133772 上传时间:2020-06-02 格式:DOCX 页数:13 大小:24.39KB
返回 下载 相关 举报
DataSet详细用法(最全面)讲义资料_第1页
第1页 / 共13页
DataSet详细用法(最全面)讲义资料_第2页
第2页 / 共13页
DataSet详细用法(最全面)讲义资料_第3页
第3页 / 共13页
DataSet详细用法(最全面)讲义资料_第4页
第4页 / 共13页
DataSet详细用法(最全面)讲义资料_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《DataSet详细用法(最全面)讲义资料》由会员分享,可在线阅读,更多相关《DataSet详细用法(最全面)讲义资料(13页珍藏版)》请在金锄头文库上搜索。

1、C#:DataSet用法详解 第15页DataSet用法详细一、特点介绍1、处理脱机数据,在多层应用程序中很有用。2、可以在任何时候查看DataSet中任意行的内容,允许修改查询结果的方法。3、处理分级数据4、缓存更改5、XML的完整性:DataSet对象和XML文档几乎是可互换的。二、使用介绍1、创建DataSet对象:DataSet ds = new DataSet(DataSetName);2、查看调用SqlDataAdapter.Fill创建的结构da.Fill(ds,Orders);DataTable tbl = ds.Table0;foreach(DataColumn col in

2、 tbl.Columns)Console.WriteLine(col.ColumnName);3、查看SqlDataAdapter返回的数据DataRow对象DataTable tbl = ds.Table0;DataRow row = tbl.Row0;Console.WriteLine(rosOrderID);检查存储在DataRow中的数据DataTable tbl = row.Table;foreach(DataColumn col in tbl.Columns)Console.WriteLine(rowcol);检查DatTable中的DataRow对象foreach(DataRow

3、 row in tbl.Rows)DisplayRow(row);4、校验DataSet中的数据校验DataColumn的属性:ReadOnly,AllowDBNull,MaxLength,UniqueDataTable对象的Constrains集合:UiqueConstraints,Primarykey, ForeignkeyConstraints通常不必刻意去创建ForeignkeyConstraints,因为当在DataSet的两个DataTable对象之间创建关系时会创建一个。用SqlDataAdapter.Fill模式来检索模式信息5、编写代码创建DataTable对象创建DataT

4、able对象: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中,就必须使用Cop

5、y方法或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 DataColumntbl

6、.ColumnsCustomersID;当设置主键时,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(It

7、emTotal,typeof(Decimal),Quantity*UnitPrice);6、修改DataTable内容添加新DataRowDataRow row = ds.TablesCustomers.NewRow();rowCustomerID = ALFKI;ds.TablesCustomers.Rows.Add(row);object aValues =ALFKI,Alfreds,Anders,030-22222;da.TablesCustomers.LoadDataRow(aValues,false);修改当前行修改行的内容逼供内不会自动修改数据库中相应的内容,对行所做的修改被视为是

8、随后将使用SqlDataAdapter对象来提交交给数据库的待定的更改。DataRow rowCustomer;rowCustomer = ds.TablesCustoemrs.Rows.Find(ANTON);if(rowCustomer = null) /没有查找客户else rowCustomerCompanyName =NewCompanyName; rowCustomerContactName =NewContactName;/推荐使用这种方式DataRow rowCustomer;rowCustomer = ds.TablesCustoemrs.Rows.Find(ANTON);i

9、f(rowCustomer = null) /没有查找客户else rowCustomer.BeginEdit(); rowCustomerCompanyName =NewCompanyName; rowCustomerContactName =NewContactName; rowCustomer.EndEdit();/null表示不修改该列的数据obejct aCustomer =null,NewCompanyName,NewContactName,nullDataRow rowCustomer;rowCustomer = ds.TablesCustomers.Rows.Find(ALFK

10、I);rowCustomer.ItemArray = aCustomer;处理DataRow的空值/查看是否为空DataRow rowCustomer;rowCustomer = ds.TablesCustomers.Rows.Find(ALFKI);if(rowCustomer.IsNull(Phone)Console.WriteLine(Its Null);elseConsole.WriteLine(Its not Null);/赋予空值rowCustomerPhone = DBNull.Value;删除DataRowDataRow rowCustomer;rowCustomer = ds

11、.TablesCustomers.Rows.Find(ALFKI);rowCustomer.Delete();清除DataRowDataRow rowCustomer = ds.TablesCustomers.Rows.Find(ALFKI);rowCustomer.ItemArray = aCustomer;da.TablesCustomers.Remove(rowCustomer);或者ds.TablesCustomers.RemoveAt(intIndex);使用DataRow.RowState属性 :Unchanged,Detached,Added,Modified,Deletedpr

12、ivate void DemonstrateRowState() / Run a function to create a DataTable with one column. DataTable myTable = MakeTable(); DataRow myRow; / Create a new DataRow. myRow = myTable.NewRow(); / Detached row. Console.WriteLine(New Row + myRow.RowState); myTable.Rows.Add(myRow); / New row. Console.WriteLin

13、e(AddRow + myRow.RowState); myTable.AcceptChanges(); / Unchanged row. Console.WriteLine(AcceptChanges + myRow.RowState); myRowFirstName = Scott; / Modified row. Console.WriteLine(Modified + myRow.RowState); myRow.Delete(); / Deleted row. Console.WriteLine(Deleted + myRow.RowState);检查DataRow中的挂起更改Dat

14、aRow rowCustomer;rowCustomer = ds.TablesCustomers.Rows.Find(ALFKI);rowCustomerCompanyName = NewCompanyName;string strNewCompanyName,strOldCompanyName;Console.WriteLine(rowCustomerCompanyName,DataRowVersion.Current); Console.WriteLine(rowCustomerCompanyName,DataRowVersion.Original);遍历DataSetforeach(DataTable dt in dataSet.Tables) foreach(DataRow dr in dt.Rows) foreach(DataColumn dc in dr.Table.Columns) Console.WriteLine(drdc);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 教学课件 > 高中课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号