| 每一個DataSet都是一個或多個DataTable 對象的集合(DataTable相當于數(shù)據(jù)庫中的表),這些對象由數(shù)據(jù)行(DataRow)、數(shù)據(jù)列(DataColumn)、字段名(Column Name)、數(shù)據(jù)格(Item),以及約束(Constraint)和有關DataTable對象中數(shù)據(jù)的關系(Relations)與數(shù)據(jù)顯示排序(DataView)信息組成。 DataView用來在觀察數(shù)據(jù)時提供排序和過濾的功能。DataColumn用來對表中的數(shù)據(jù)值進行一定的規(guī)限。比如哪一列數(shù)據(jù)的默認值是什么、哪一列數(shù)據(jù)值的范圍是什么、哪個是主鍵、數(shù)據(jù)值是否是只讀等。 由于一個DataSet可能存在多張表,這些表可能存在關聯(lián)關系,因此用parentRelations和childRelations來表述。ParentRelations表是父表,childRelations是子表,子表是對父表的引用,這樣就使得一個表中的某行與另一個表中的某一行甚至整個表相關聯(lián)。 1.DataTableCollection 類表示 DataSet 的表的集合。 DataTableCollection dtc = ds.Tables; DataTable customerTable = dtc["Product"]; 2.DataTable進行動態(tài)的篩選和排序調用DataTable.Select()方法,獲取 DataRow 對象的數(shù)組。 (1)獲取所有行。 DataRow[] rows = dt.Select(); (2)按主鍵順序(如沒有主鍵,則按照添加順序)獲取符合篩選條件的行。 DataRow[] rows = dt.Select("ID>52"); (3)獲取符合篩選條件的行,并按指定的排序條件排序。 DataRow[] rows = dt.Select("ID>52","ID DESC"); (4)獲取符合篩選條件和指定狀態(tài)的行,并按指定的排序條件排序。 string strExpr = "ID>52"; string strSort = "ID DESC"; DataRow[] foundRows = dt.Select(strExpr, strSort, DataViewRowState.OriginalRows); 3.DataTable進行數(shù)據(jù)統(tǒng)計我們在使用SQL Server時,可以輕松地對數(shù)據(jù)進行Sum、Aver、Count等操作以獲得統(tǒng)計結果,那么,在已經(jīng)把數(shù)據(jù)檢索出來的DataSet(DataTable)中如何進行統(tǒng)計呢?特別是通過第三方接口Web Service獲得了DataSet,這個時候,沒有辦法去執(zhí)行Select語句來獲取這些統(tǒng)計,怎么辦呢? 辦法總比問題多,其實在DataTable中也是可以進行數(shù)據(jù)統(tǒng)計的。 下面就通過幾個簡單的示例,介紹一下如何無須通過逐條記錄進行計算就可以輕松地獲得DataTable中的記錄統(tǒng)計結果。這里調用的是功能強大的DataTable的函數(shù)Compute。 (1)函數(shù)說明: public object Compute(string strExpression,string strFilter); l strExpression:要計算的表達式字符串,基本上類似于Sql Server中的統(tǒng)計表達式。 l strFilter:統(tǒng)計的過濾字符串,只有滿足這個過濾條件的記錄才會被統(tǒng)計。 (2)調用示例。 假設一個產(chǎn)品銷售表P_Sell,描述商場中各銷售人員的銷售記錄,如表5-2所示。 表5-2 產(chǎn)品銷售表 
 l 統(tǒng)計所有性別為女的銷售員的數(shù)量: object n = table.Compute("count(ID)", "Sex = 0"); l 統(tǒng)計所有年齡大于20歲的銷售員的數(shù)量: int c=(int)table.Compute("count(ID)", "Birthday<'" DateTime.Today.AddYears(-20) "'"); l 統(tǒng)計銷售產(chǎn)品的平均價格: decimal ap=(decimal)table.Compute("avg(Price)", "true"); l 統(tǒng)計產(chǎn)品代碼為1的產(chǎn)品銷售數(shù)量: object m = table.Compute("sum(Num)", "ProductId='sj'"); l 統(tǒng)計所有產(chǎn)品的銷售總金額:要統(tǒng)計銷售總金額,table中不存在某項產(chǎn)品某個促銷員銷售的金額數(shù)據(jù),但我們可以通過Quantity*Price來獲得。比如table.Compute("Sum(Quantity*Price)","true");。 這里有一個問題是,DataTable的統(tǒng)計功能沒有SqlServer強大,這個寫法是錯誤的! 因為Compute的統(tǒng)計不具備Sum(Quantity*Price)這樣的數(shù)據(jù)的功能。那怎么辦呢? 對于這樣復雜數(shù)據(jù)的統(tǒng)計,我們可以通過在DataTable中創(chuàng)建一個新的DataColumn來完成,比如為“total”,同時設置該字段的Expression為Quantity*Price,這樣我們就可以使用統(tǒng)計功能了。 DataColumn dc = new DataColumn("total", Type.GetType("System.Decimal")); dc.Expression = "Num*Price"; table.Columns.Add(dc); object s=table.Compute("sum(total)", "true"); 當然,這個功能也可以通過 DataGrid增加一個模板列,在ItemDataBind事件里實現(xiàn)計算。 4.合并兩個DataTable表的數(shù)據(jù)DataTable dt1 = ds.Tables[0]; DataTable dt2 = ds.Tables[1]; dt1.Merge(dt2, true, MissingSchemaAction.AddWithKey); 5.DataView 類(1)得到DataView。 DataView dv = ds.Tables[0].DefaultView; //或 DataView dv = new DataView(ds.Tables["Product"], "ID > 52", "ID DESC", DataViewRowState.CurrentRows); (2)得到DataView的行數(shù)據(jù)。 foreach (DataRowView rowview in dv) { for (int i = 0; i < dv.Table.Columns.Count; i ) { Response.Write(rowview[i] "<br>"); } } (3)對結果集過濾排序。 DataView dv = ds.Tables[0].DefaultView; dv.RowFilter = "ID > 52"; dv.Sort = "ID DESC"; int c = dv.Count; if (c > 51) { for (int n = 50; n < c; n ) { dv.Delete(n); } } this.DataGrid1.DataSource = dv; | 
|  |