数据集的使用方法和技巧
數(shù)據(jù)集的使用方法和技巧
數(shù)據(jù)集概述
1.1數(shù)據(jù)集
l???????? 是一種代表關(guān)系數(shù)據(jù)的內(nèi)存駐留結(jié)構(gòu)
l???????? 是以XML 形式表示的數(shù)據(jù)視圖,是一種數(shù)據(jù)關(guān)系視圖。
l???????? 在Visual Studio 和.NET Framework 中,XML 是存儲和傳輸各種數(shù)據(jù)時所用的格式。因此,數(shù)據(jù)集與XML 有密切關(guān)系。
1.2數(shù)據(jù)集分類
– 類型化數(shù)據(jù)集
–???? 非類型化數(shù)據(jù)集
1.3類型化數(shù)據(jù)集和非類型化數(shù)據(jù)集的區(qū)別
| ? | 結(jié)構(gòu)體系 | 功能類型 | 檢測 |
| 類型化數(shù)據(jù)集 | 類型化數(shù)據(jù)集這樣一種數(shù)據(jù)集,它先從基DataSet 類派生,然后,使用XML 架構(gòu)文件(.xsd 文件)中的信息生成新類。架構(gòu)中的信息(表、列等)被作為一組第一類對象和屬性生成并編譯為此新數(shù)據(jù)集類。 | 可以直接通過名稱 引用表和列 | 在VS.NET中可以智能感知元素的類型 |
| 非類型化數(shù)據(jù) 集 | 非類型化數(shù)據(jù)集沒有相應(yīng)的內(nèi)置架構(gòu)。與類型化數(shù)據(jù)集一樣,非類型化數(shù)據(jù)集也包含表、列等,但它們只作為集合公開。 | 需要通過Tables集 合引用列 | 不能感知 |
?
1.4 數(shù)據(jù)集的屬性
重點:
– Tables
–???? Relations
?
創(chuàng)建數(shù)據(jù)集和數(shù)據(jù)表
2.1創(chuàng)建數(shù)據(jù)集:
– 設(shè)計時創(chuàng)建非類型化數(shù)據(jù)集
工具箱“數(shù)據(jù)”
–???? 運行時創(chuàng)建非類型化數(shù)據(jù)集
?
2.2 創(chuàng)建數(shù)據(jù)表
? 數(shù)據(jù)表屬性
–最重要的集合:
? Columns
? Rows
? Constraints
? 給數(shù)據(jù)集添加數(shù)據(jù)表
[參考代碼]
//創(chuàng)建數(shù)據(jù)集
DataSet ds = new DataSet("myDS");
DataTable dsMaster = new DataTable("master");
DataTable dsChild = new DataTable("child");
ds.Tables.Add(dsMaster);
ds.Tables.Add(dsChild);
?
2.3 為數(shù)據(jù)表添加列
? Columns集合屬性
? 為數(shù)據(jù)表添加列
?
2.4 為數(shù)據(jù)表添加行
? Rows屬性
? Rows.Add方法
– Add(DataRow):向數(shù)據(jù)表中添加指定的數(shù)據(jù)行
? 為數(shù)據(jù)表添加行
[參考代碼]
//添加列
ds.Tables["master"].Columns.Add("masterID", typeof(Int32));
//添加行
DataRow dr = ds.Tables["master"].NewRow();
dr["masterID"] = "";
ds.Tables["master"].Rows.Add(dr);
//修改表頭
ds.Tables["master"].Columns["masterID"].Caption = "主ID";
?
2.5 為數(shù)據(jù)表添加約束
? ForeignKeyConstraints屬性:保證兩個數(shù)據(jù)表之間對應(yīng)行改變時的關(guān)系
? UniqueConstraints屬性:保證某列數(shù)據(jù)在每行都不相同
? 在運行時為數(shù)據(jù)表添加約束
–添加外鍵約束:修改一個表的值看是否變化
–添加唯一鍵約束
?
2.6 為數(shù)據(jù)集添加關(guān)系
? Relations:定義數(shù)據(jù)表之間的關(guān)系,該集合中可以包含0個或者多個數(shù)據(jù)關(guān)系對象,每個對象表示
兩個表之間的關(guān)系
? 只有數(shù)據(jù)集的EnforeConstraints屬性設(shè)置為true時,DataRelation中建立的約束才會被強(qiáng)制執(zhí)行
? 為數(shù)據(jù)表添加數(shù)據(jù)關(guān)系(非類型數(shù)據(jù)集)
[參考代碼]
//添加唯一鍵
System.Data.UniqueConstraint uc = new UniqueConstraint("唯一鍵名稱",ds.Tables["master"].Columns["masterID"]);
//添加外鍵
System.Data.ForeignKeyConstraint fc = new ForeignKeyConstraint("外鍵名稱", "主表列", "從表列");
//添加關(guān)系
ds.Relations.Add("關(guān)系名","父表列","子表列");
//注意關(guān)系和外鍵的區(qū)別
if (ds.Relations.Count <= 0)
??? return ;
System.Data.DataRow dr = ds.Tables["master"].Rows[0];
System.Data.DataRow[] drArray = dr.GetChildRows(ds.Relations[0]);
//這里的區(qū)別在于 外鍵:如果主表列更新,則從表列都對應(yīng)更新
//關(guān)系:能夠從父表對應(yīng)的一列,獲取子表對應(yīng)的行集
?
2.7 顯示行狀態(tài)
? 數(shù)據(jù)行的RowState屬性反映了自數(shù)據(jù)表創(chuàng)建以來或者上次更新后在數(shù)據(jù)表上采取的操作
? DataRowState的取值
? 顯示狀態(tài)行
[參考代碼]
//顯示行狀態(tài)
DataRowState drs = ds.Tables["master"].Rows[0].RowState;
?
數(shù)據(jù)操作
3.1數(shù)據(jù)集方法
– 克隆數(shù)據(jù)集:只復(fù)制結(jié)構(gòu)
DataSet.Clone ();
– 復(fù)制數(shù)據(jù)集:復(fù)制結(jié)構(gòu)和數(shù)據(jù)
DataSet.Copy ();
3.2數(shù)據(jù)表方法
– Select方法:用于在運行時過濾數(shù)據(jù)表的行并進(jìn)行排序。它不對表的內(nèi)容作改動。該方法只是返回與指定的規(guī)則相匹配的行數(shù)組
//返回DataRow[]集合
DataSet.Tables["master"].Select(“條件”,”排序規(guī)則”);
?
數(shù)據(jù)集內(nèi)的篩選與排序
在填充數(shù)據(jù)集之后,通常會發(fā)現(xiàn)使用表中的不同記錄子集或以不同順序查看數(shù)據(jù)很有用。可以通過對數(shù)據(jù)集內(nèi)的數(shù)據(jù)進(jìn)行篩選和排序來實現(xiàn)這一點。若要簡化此過程,可以創(chuàng)建數(shù)據(jù)視圖,數(shù)據(jù)視圖提供可合并篩選器和排序條件的對象,可用作數(shù)據(jù)綁定的源
?
數(shù)據(jù)集內(nèi)的篩選與排序
可改用內(nèi)置的數(shù)據(jù)集功能來篩選和排序。有兩個選擇:
? 數(shù)據(jù)表支持Select 方法,您可調(diào)用該方法來篩選和排序。該方法并不更改表中記錄的內(nèi)容和順序,相反,它向您提供一個記錄列表(或數(shù)組)表示所指定的條件。
? 可以使用數(shù)據(jù)視圖(DataView 對象)。數(shù)據(jù)視圖是一個對象,它作為數(shù)據(jù)表之上的層,提供經(jīng)過篩選和排序后的表內(nèi)容視圖。(還可以使用數(shù)據(jù)視圖管理器,它的行為像數(shù)據(jù)視圖集合。)數(shù)據(jù)視圖類似于數(shù)據(jù)庫中的視圖,因為它不是數(shù)據(jù)的副本。相反,它只是查看表中數(shù)據(jù)的另一種方式。
?
4.1 數(shù)據(jù)視圖概述
? 數(shù)據(jù)視圖是位于數(shù)據(jù)表之上的獨立對象
? 數(shù)據(jù)視圖是對單一數(shù)據(jù)進(jìn)行過濾和排序后的視圖
? 可以用作綁定控件的數(shù)據(jù)源
? 可以為一個數(shù)據(jù)表創(chuàng)建多個數(shù)據(jù)視圖
? 視圖數(shù)據(jù)行實際引用了數(shù)據(jù)行的DataRowView對象
?
DataRowView的屬性
?
| 屬性 | 描述 |
| DataView | 該數(shù)據(jù)行視圖所屬的數(shù)據(jù)視圖 |
| IsEdit | 表示該數(shù)據(jù)行視圖是否正在被編輯 |
| IsNew | 表示該數(shù)據(jù)行視圖是否為新建 |
| Item | 該數(shù)據(jù)行視圖中某列的值 |
| Row | 正在被查看的數(shù)據(jù)行 |
| RowVersion | 該數(shù)據(jù)行視圖的當(dāng)前版本 |
4.2 創(chuàng)建數(shù)據(jù)視圖
? 在設(shè)計時創(chuàng)建
–創(chuàng)建類型化數(shù)據(jù)集
–從工具箱“數(shù)據(jù)”中選擇“DataView”控件
? 可以使用表的DefaultView 屬性來訪問該默認(rèn)數(shù)據(jù)視圖,該數(shù)據(jù)視圖返回一個
DataView 對象。可在運行時設(shè)置默認(rèn)數(shù)據(jù)視圖的屬性。
?
4.3 數(shù)據(jù)視圖的屬性
? 數(shù)據(jù)視圖屬性
– RowFilter
– RowStateFilter
– Sort
? 使用表的Select 方法或數(shù)據(jù)視圖的RowFilter 屬性,可以篩選數(shù)據(jù)表中的記錄以便僅使想操作的記錄可用。這在要操作數(shù)據(jù)集表中記錄的不同子集時很有用。若要指定篩選器條件,可以使用與創(chuàng)建列表達(dá)式所用語法相同的表達(dá)式語法。
[參考代碼]
//RowFilter判斷
string tempid = this.lbdid.Text.ToString();
bookView.RowFilter = "id='" + tempid + "'";
if (bookView.Count==1)
{
???? Response.Write("<script langusge='javascript'>alert('。。。');</script>");
???? return;
}
//RowStateFilter判斷
DataView dwMain = (DataView)Session["cart"];
dwMain.RowFilter = this.tbCon.Text;
switch (ddlState.SelectItem.Value)
{
??? case "CurrentRows":
??????? dwMain.RowFilter = DataViewRowState.CurrentRows;
??????? break;
??? case "OriginalRows":
??????? dwMain.RowFilter = DataViewRowState.OriginalRows;
??????? break;
?? ?case "Added":
??????? dwMain.RowFilter = DataViewRowState.Added;
??????? break;
??? case "Unchanged":
??????? dwMain.RowFilter = DataViewRowState.Unchanged;
??????? break;
??? case "ModifiedCurrent":
??????? dwMain.RowFilter = DataViewRowState.ModifiedCurrent;
??????? break;
??? default:
??????? break;
}
?
數(shù)據(jù)列表達(dá)式
? 是一個字符串,可以使用任何一個普通字符串處理函數(shù)來生成
? 算術(shù)運算符:+,-,*,/,%
? 比較運算符:AND,OR,<,>,<=,>=,<>
– IN:確定指定值是否包含在一個集合中
? myExpression= “myColumnIN ( ‘a(chǎn)’,’b’,’c’)”
– Like:用通配符進(jìn)行模糊匹配
? 函數(shù)
4.4 數(shù)據(jù)視圖的方法
?
| 方法 | 描述 |
| AddNew | 向數(shù)據(jù)視圖中添加一個新的數(shù)據(jù)行視圖 |
| Delete | 從數(shù)據(jù)視圖中刪除一個數(shù)據(jù)行視圖 |
| Find | 根據(jù)指定的主鍵查找包含該主鍵的數(shù)據(jù)行視圖 |
Find方法
? 按指定的排序關(guān)鍵字值在DataView 中查找行。
[參考代碼]
//向數(shù)據(jù)視圖添加行
DataRowView drv;
drv = dwMain.AddNew();
drv[".."] = "..";
//省略,不需要再Add
//刪除
dwMain.Delete(dwMain.Count - 1);
//更新
dwMain[dwMain.Count - 1]["列名"] = "";
//查找,返回檢索行數(shù)
dwMain.Find("關(guān)鍵字");
?
數(shù)據(jù)集實用技巧
? EXCEL和DataSet
[參考代碼]
//注意添加名字空間using System.Data.OleDb;
#region 從Excel導(dǎo)入DataSet
string strConn = "Provider=Microsoft.Jet.OLEDB4.0"+
???????????????? "Data Source="+strFillName+";"+
???????????????? "Extended Properties =Excel8.0";
OleDbDataAdapter ExcelDa = new OleDbDataAdapter("select * from [sheet1$]", strnConn);
DataSet ExcelDs = new DataSet();
try
{
??? ExcelDa.Fill(ExcelDs, "excelInfo");
}
catch(Exception err)
{
??? //進(jìn)行錯誤處理
}
#endregion
?
?
#region 從DataSet導(dǎo)入Excel
//首先要添加COM組件:添加引用-Com組件-Microsoft Excel 11.0-選擇'確認(rèn)
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
System.Data.DataTable tb = ds.Tables[0];
foreach(DataColumn col in tb.Columns)
{
??? colIndex++;
??? //獲取列名稱
??? excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in tb.Rows)
{
??? //添加數(shù)據(jù)
??? rowIndex++;
??? colIndex = 0;
??? foreach (DataColumn col in tb.Columns)
??? {
??????? colIndex++;
??????? excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
??? }
}
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(....);
excel.Quit();
excel=null;
GC.Collect();//垃圾回收
#endregion
? XML和DataSet
[參考代碼]
DataSet ds = new DataSet();
//Schelma盡量要單獨載入,否則解析效率很低
ds.ReadXml("...");
ds.WriteXml("..");
? BLOB和DataSet
?Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=905064
轉(zhuǎn)載于:https://www.cnblogs.com/ufo0303/archive/2006/08/18/480539.html
《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的数据集的使用方法和技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Scott Mitchell 的ASP.
- 下一篇: 网管日志-06.09.08