|
一、特點介紹
1、處理脫機數(shù)據(jù),在多層應(yīng)用程序中很有用。
2、可以在任何時候查看DataSet中任意行的內(nèi)容,允許修改查詢結(jié)果的方法。
3、處理分級數(shù)據(jù)
4、緩存更改
5、XML的完整性:DataSet對象和XML文檔幾乎是可互換的。
二、使用介紹
1、創(chuàng)建DataSet對象:DataSet ds = new DataSet("DataSetName");
2、查看調(diào)用SqlDataAdapter.Fill創(chuàng)建的結(jié)構(gòu)
da.Fill(ds,"Orders");
DataTable tbl = ds.Table[0];
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(col.ColumnName);
3、查看SqlDataAdapter返回的數(shù)據(jù)
①、DataRow對象
DataTable tbl = ds.Table[0];
DataRow row = tbl.Row[0];
Console.WriteLine(ros["OrderID"]);
②、檢查存儲在DataRow中的數(shù)據(jù)
DataTable tbl = row.Table;
foreach(DataColumn col in tbl.Columns)
Console.WriteLine(row[col]);
③、檢查DatTable中的DataRow對象
foreach(DataRow row in tbl.Rows)
DisplayRow(row);
4、校驗DataSet中的數(shù)據(jù)
①、校驗DataColumn的屬性:ReadOnly,AllowDBNull,MaxLength,Unique
②、DataTable對象的Constrains集合:UiqueConstraints,Primarykey,ForeignkeyConstraints
通常不必刻意去創(chuàng)建ForeignkeyConstraints,因為當在DataSet的兩個DataTable對象之間創(chuàng)建關(guān)系時會創(chuàng)建一個。
③、用SqlDataAdapter.Fill模式來檢索模式信息
5、編寫代碼創(chuàng)建DataTable對象
①、創(chuàng)建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方法創(chuàng)建一個與原DataTable結(jié)構(gòu)相同并且包含相同行的新DataTable;Clone方法創(chuàng)建一個與原DataTable結(jié)構(gòu)相同,但沒有包含任何行的新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"]};
當設(shè)置主鍵時,AllowDBNull自動設(shè)置為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;
功能和特性
價格和優(yōu)惠
網(wǎng)站和維護
推廣和優(yōu)化
|