ADO.NET的记忆碎片(六)
校驗(yàn)DataSet中的數(shù)據(jù)
數(shù)據(jù)庫提供了很多的機(jī)制使數(shù)據(jù)是有效的。ADO.NET的DataSet提供了許多可在數(shù)據(jù)庫系統(tǒng)中使用的相同的數(shù)據(jù)效驗(yàn)機(jī)制。一般可以將這些效驗(yàn)的機(jī)制分成兩類:列級(jí)別的限制和表級(jí)別的效限制。列級(jí)別的限制:驗(yàn)證DataColumn的屬性
DataColumn對(duì)象提供了許多能用來驗(yàn)證數(shù)據(jù)的屬性:
ReadOnly:確保數(shù)據(jù)是否讓用戶修改
AllowDBNull:一些數(shù)據(jù)庫的列是否可為Null
MaxLength:最多接受字符串的長度
Unique:是否要求該列是不可重復(fù)的
表級(jí)別的限制:DataTable類的Constraints集合
ADO.NET對(duì)象模型包括兩個(gè)類,可以用他們?cè)贒ataTable中定義約束。這兩個(gè)類是UniqueConstraints和ForeignkeyConstraint,他們都是派生于Constraints類的。DataTable公開一個(gè)Constraints屬性,使用該屬性可以添加、修改或查看DataTable上的約束。
UniqueConstraints
如果將DataColumn的Unique屬性設(shè)置為True,也就在包含該列的DataTable定義了一個(gè)唯一的約束。同時(shí)還將UniqueConstraints對(duì)象加到DataTable對(duì)象的Constraints集合中。設(shè)置DataColumn的Unique屬性比在DataTable對(duì)象的Constraints集合中創(chuàng)建一個(gè)新的UniqueConstraint要簡(jiǎn)單,但是在有些時(shí)候會(huì)需要顯示創(chuàng)建UniqueConstraint,例如在需要確保多列合并之后的值為唯一時(shí)。
Primarykey
DataTable類允許通過Primarykey屬性為DataTable定義一個(gè)主鍵,Primarykey屬性包含的是一個(gè)DataColumn對(duì)象數(shù)組,DataTable使用該數(shù)組來構(gòu)造一個(gè)UniqueConstraint,以支持
約束。
ForeignkeyConstraint
外部的約束添加到DataTable中。通常可能不需要顯示創(chuàng)建ForeignkeyConstraint。在DataSet內(nèi)的兩個(gè)DataTable對(duì)象之間創(chuàng)建DataRelation也會(huì)創(chuàng)建一個(gè)ForeignkeyConstraint。
DataRow.RowState屬性
DataSet中的一切都是一種脫機(jī)緩存,在DataSet所作的一切修改要是不提交數(shù)據(jù)庫的話,那么用途也不是很大。那么在DataSet中修改是會(huì)有記錄的,這些記錄在DataRow.RowState屬性中,DataRow.RowState使用的是一個(gè)枚舉中的值:
Unchanged? 該行未包含任何掛起更新
Detached?? ?該行不是DataTable的一個(gè)成員
Added???????? 該行已經(jīng)被添加到了DataTable中,但是不存在在數(shù)據(jù)庫中
Modified????? 該行被掛起更改
Deleted????? ?該行為掛起刪除
例子:
Unchanged? row = tb1.NewRow;row["lmf"] = "liumingfeng";
Detached?? row.Rows.Add(row);
Added????? row = tb1.Rows(0);
Modified?? row["lmf"] = "hello world";
Deleted??? row.Delete();
一般來說,DataRow有兩個(gè)版本,一個(gè)是當(dāng)前的行內(nèi)容,一個(gè)是原來存儲(chǔ)在該行的內(nèi)容。
可以這樣來查看不同的版本的內(nèi)容:
row["lmf",DataRowVersion.Current];//當(dāng)前的值
row["lmf",DataRowVersion.Original];//原來的值
創(chuàng)建DataRelation對(duì)象
在創(chuàng)建DataRelation時(shí),應(yīng)該提供一個(gè)名稱,這樣就可以在其集合中查找該對(duì)象,并且指定該關(guān)系所基于的父列和子列。DataRelation有不同的構(gòu)造函數(shù)來接受單個(gè)的DataColumn或者是DataColumn對(duì)象數(shù)組。最后增加在DataSet中的Relations屬性中,Relations屬性是DataRelation的集合。
用單個(gè)的DataColumn創(chuàng)建DataRelation:
//創(chuàng)建數(shù)據(jù)庫結(jié)構(gòu)
DataSet ds = new DataSet("MyDataSet");
DataTable tb1 = ds.Tables.Add("Customers");
tb1.Columns.Add("CustomerID",typeof(string));
tb1.Columns.Add("CustomerName",typeof(string));
tb1 = ds.Tables.Add("Orders");
tb1.Columns.Add("OrderID",typeof(int));
tb1.Columns.Add("CustomerID",typeof(string));
tb1.Columns.Add("OrderDate",typeof(DateTime));
//添加用單個(gè)的DataColumn創(chuàng)建DataRelation
DataRelation rel;
rel = new DataRelation("Customer_Orders",ds.Table["Customers"].Columns["CustomerID"],ds.Table["Orders"].Columns["CustomerID"]);
ds.Relations.Add(rel);
如果希望根據(jù)多個(gè)字段定義DataRelation,那么就要使用接受DataColumn對(duì)象數(shù)組的構(gòu)造函數(shù),代碼示例:
//創(chuàng)建數(shù)據(jù)庫結(jié)構(gòu)
DataSet ds = new DataSet("MyDataSet");
DataTable tbCustomer = ds.Tables.Add("Customers");
tbCustomer.Columns.Add("CustomerID",typeof(string));
tbCustomer.Columns.Add("CustomerName",typeof(string));
DataTable tbOrder = ds.Tables.Add("Orders");
tbOrder.Columns.Add("OrderID",typeof(int));
tbOrder.Columns.Add("CustomerID",typeof(string));
tbOrder.Columns.Add("CustomerName",typeof(string));
tbOrder.Columns.Add("OrderDate",typeof(DateTime));
//創(chuàng)建引用DataColumn對(duì)象的數(shù)組
DataColumn[] CustomerP,OrderC;
CustomerP = new DataColumn[]{tbCustomer.Columns["CustomerID"],tbCustomer.Columns["CustomerName"]};
OrderC = new DataColumn[]{tbOrder.Columns["CustomerID"],tbOrder.Columns["CustomerName"]};
//添加用單個(gè)的DataColumn創(chuàng)建DataRelation
DataRelation rel;
rel = new DataRelation("Customer_Orders",CustomerP,OrderC);
ds.Relations.Add(rel);
以上的方法都是:先創(chuàng)建一個(gè)DataRelation對(duì)象,然后再添加到DataSet中的Relations集合中。這個(gè)模式和新建的DataTable、DataColumn對(duì)象一樣的。
還可以使用語法糖:
ds.Relations.Add("Customer_Orders",ds.Table["Customers"].Columns["CustomerID"],ds.Table["Orders"].Columns["CustomerID"]);
DataRelation對(duì)象的用途
DataRelation對(duì)象屬于DataSet所有,記錄著DataTable與DataTable的關(guān)系,主要的用途是查找有關(guān)聯(lián)的DataTable中的數(shù)據(jù)。不過DataRelation不親自處理這個(gè)任務(wù),至少不是直接的處理這個(gè)任務(wù)。這個(gè)功能實(shí)際上是通過DataRow對(duì)象的GetChildRows、GetParentRow和GetParentRows方法提供的。其實(shí)在調(diào)用這幾個(gè)方法時(shí),需要DataRelation對(duì)象作為參數(shù)傳入,在這幾個(gè)方法的內(nèi)部會(huì)引用DataRelation對(duì)象來獲取表之間的關(guān)系,來查找有關(guān)的數(shù)據(jù)的。
使用GetChildRows方法:
//創(chuàng)建數(shù)據(jù)庫結(jié)構(gòu)
DataSet ds = new DataSet("MyDataSet");
DataTable tb1 = ds.Tables.Add("Customers");
tb1.Columns.Add("CustomerID",typeof(string));
tb1.Columns.Add("CustomerName",typeof(string));
tb1 = ds.Tables.Add("Orders");
tb1.Columns.Add("OrderID",typeof(int));
tb1.Columns.Add("CustomerID",typeof(string));
tb1.Columns.Add("OrderDate",typeof(DateTime));
//添加用單個(gè)的DataColumn創(chuàng)建DataRelation
DataRelation rel;
rel = new DataRelation("Customer_Orders",ds.Table["Customers"].Columns["CustomerID"],ds.Table["Orders"].Columns["CustomerID"]);
ds.Relations.Add(rel);
//填充DataSet
string strConn,strSQL;
......
SqlDataAdapter da = new SqlDataAdapter(strSQL,strConn);
da.TableMapping.Add("Table","Customers");
da.TableMapping.Add("Table1","Orders");
da.Fill(ds);
//遍歷客戶
foreach(DataRow row in ds.Tables["Customers"].Rows)
{
??? Console.WriteLine("{0}",row[0]);
??? //遍歷相關(guān)的訂單,使用GetChildRows方法
??? foreach(DataRow rowOrder in row.GetChildRows(rel))
??? {
??????? Console.WriteLine("{0}--{1}",rowOrder["OrderID"],rowOrder["OrderDate"]);
??? }
??? Console.WriteLine();
}
調(diào)用GetChildRows方法的,可將DataRelation對(duì)象作為參數(shù)傳入,也可以使用DataRelation對(duì)象的名稱作為參數(shù)傳入。
使用GetParentRow方法:
//遍歷訂單
DataRow coustomer;
foreach(DataRow row in ds.Tables["Orders"].Rows)
{
??? //定位相關(guān)的父行,查找顧客
??? coustomer = row.GetParentRow("Customer_Orders");
}
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/lmfeng/archive/2012/01/11/2319573.html
總結(jié)
以上是生活随笔為你收集整理的ADO.NET的记忆碎片(六)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [自己动手]让Editplus更好用
- 下一篇: 【转】 onNewIntent调用时机