ADO.NET的记忆碎片(七)
使用DataTable類的搜索和篩選功能
DataTable類公開了兩個方法:Find和Select
Find方法:可以根據(jù)主鍵來查找數(shù)據(jù)行。Select方法:更類似于篩選器,根據(jù)更靈活的搜索條件返回多個數(shù)據(jù)行
在查詢數(shù)據(jù)庫獲取信息時,假如使用如下SQL查詢:
???? select CustomerID,CompanyName,ContactName,Phone from Customers where CustomerID = 'Alen'
這個查詢是根據(jù)主鍵來查詢的,可以在ADO.NET中使用Find方法,根據(jù)主鍵值在DataTable中查找DataRow。
Find方法返回一個DataRow,就算有多個DataRow符合查詢的要求,也只會返回第一個符合要求的DataRow。
使用示類:
string strConn, strSQL;//數(shù)據(jù)庫連接字符串,和查詢語句
strConn = "....";
strSQL = "....";
SqlDataAdapter da = new SqlDataAdapter(strSQL,strConn);
DataTable tb1 = new DataTable("Customers");
da.Fill(tb1);
tb1.PrimaryKey = new DataColumn[] { tb1.Columns["CustomerID"] };
DataRow row = tb1.Rows.Find("Alen");
Console.WriteLine(row["CompanyName"]);
DataTable類的Select方法根據(jù)類似的條件查找行。
假如希望查找下面SQL語句:
???? select CustomerID,CompanyName,ContactName,Phone from Customers where Country = 'USA' and City <> 'Seattle'
這個復雜的條件查詢,我們要是假設DataTable中數(shù)據(jù)也是要完成這樣的查詢,可以使用Select方法來完成,使用實例:
SqlDataAdapter da1 = new SqlDataAdapter(strSQL, strConn);
DataTable tb2 = new DataTable("Customers");
da.Fill(tb1);
string strFilter = "Country = 'USA' and City <> 'Seattle'";
foreach (DataRow r in tb2.Select(strFilter))
{
??? Console.WriteLine(r["CompanyName"]);
}
可以看出來select方法還是很重要的,我們來做個select的用法步驟歸納:
1、先確定要篩選的目標結(jié)果
2、寫出對應的SQL語句,能在數(shù)據(jù)庫中運行,并得到想要的結(jié)果
3、把這個表的數(shù)據(jù),用SqlDataAdapter的Fill方法緩存到內(nèi)存的DataTable中
4、把SQL語句的Where部分提取出來賦值給strFilter字符串
5、調(diào)用Select方法:DataRow[] = tb2.Select(strFilter);會返回相應的結(jié)果集
?
?*有時候我們需要對結(jié)果排序,用SQL語句就很簡單:
?*升序:
?*select CustomerID,CompanyName,ContactName,Phone from Customers order by City
?*降序:
?*select CustomerID,CompanyName,ContactName,Phone from Customers order by City desc
?*重載的Select方法可以接受一個排序順序,使用實例:?
string strFilter1 = "Country = 'USA' and City <> 'Seattle'";
string strSort = "City Desc";
foreach (DataRow r in tb2.Select(strFilter1, strSort))
{
??? Console.WriteLine(r["CompanyName"]);
}
?我們來做個select的用法步驟重新總結(jié):
1、先確定要篩選的目標結(jié)果
2、寫出對應的SQL語句,能在數(shù)據(jù)庫中運行,并得到想要的結(jié)果
3、把這個表的數(shù)據(jù),用SqlDataAdapter的Fill方法緩存到內(nèi)存的DataTable中
4、把SQL語句的Where部分提取出來賦值給strFilter字符串,Order by部分提取出來給strSort字符串
5、調(diào)用Select方法:DataRow[] = tb2.Select(strFilter, strSort);會返回相應的結(jié)果集
DataView對象的出現(xiàn)
DataTable的Select方法功能強大而且很靈活,但是它還是有一些限制。
首先,這個動態(tài)的查詢效率不高。然后Windows和Web不支持綁定到DataRow數(shù)組。
于是DataView對象出現(xiàn)了,可以很好的解決這些限制
DataView對象從DataTable中返回數(shù)據(jù),數(shù)據(jù)庫中的視圖也是這個特性
DataView對象可以用來篩選、排序和搜索DataTable的數(shù)據(jù),但是他們并非SQL查詢。
不能用DataView來連接兩個DataTable對象之間的數(shù)據(jù)。
DataView對象的確支持根據(jù)動態(tài)的條件篩選行,但是他們僅能訪問一個DataTable,并且DataTable中的所有列都可以通過DataView得到
創(chuàng)建DataView對象
使用DataView來查看DataTable中的數(shù)據(jù)就必須將他和DataTable對象關聯(lián)起來,有以下兩個方法:
DataTable tb3 = new DataTable("Customers");
DataView vue;
//方法1,使用這個方法要注意一個問題,所關聯(lián)的tb3的TableName必須是自己重新指定的,默認是不行的
vue = new DataView();
vue.Table = tb3;
//方法2
vue = new DataView(tb3);
DataView對象還有一個構(gòu)造函數(shù),其簽名與DataTable對象的Select方法非常匹配,
一個復雜的構(gòu)造函數(shù)在一行代碼中設置了DataView的Table、RowFilter、Sort和RowStateFilter等屬性
使用實例:
//分別設置Table、RowFilter、Sort和RowStateFilter等屬性
vue.Table = tb3;
vue.RowFilter = strFilter1;
vue.Sort = strSort;
vue.RowStateFilter = DataViewRowState.ModifiedCurrent;
//一個復雜的構(gòu)造函數(shù)
vue = new DataView(tb3, strFilter1, strSort,DataViewRowState.ModifiedCurrent);
使用DataRowView來查看DataView中的數(shù)據(jù)
DataView對象還公開了Count屬性,可以看到DataView的行數(shù)
使用實例:
foreach (DataRowView rView in vue)
{
??? Console.WriteLine(rView["CompanyName"]);
}
在DataView中搜索數(shù)據(jù)
我們前面已經(jīng)使用了RowFilter和RowStateFilter屬性來支持搜索的,DataView還支持Find和FindRows方法搜索
Find方法使用說明:一旦設置了DataView中的Sort屬性,就可以調(diào)用它的Find方法,根據(jù)Sort屬性中所指示的列來查找。
DataView的Find方法不返回DataRow或者是DataRowView對象,他會返回的是一個整型值,該值是對應于所在行的DataView的索引。如果沒有就返回-1
Find方法使用實例:
vue = new DataView(tb3);
vue.Sort = "City";
int intIndex = vue.Find("Fran Wilson");
Console.WriteLine(vue[intIndex]["CompanyName"]);
//使用FindRows方法
vue = new DataView(tb3);
vue.Sort = "City";
DataRowView[] arows = vue.FindRows("Fran");
foreach (DataRowView rr in arows)
{
??? Console.WriteLine(rr["CompanyName"]);
}
修改DataRowView對象
用DataRowView對象修改一行數(shù)據(jù)類似于修改DataRow對象的內(nèi)容。與DataRow類一樣,DataRowView對象也公開了BeginEdit,EndEdit,CancelEdit
和Delete方法,用DataRowView對象創(chuàng)建新數(shù)據(jù)行與創(chuàng)建新DataRow有一點不同。DataView有一個AddNew方法,該方法返回一個新的DataRowView
直到DataRowView對象調(diào)用EndEdit方法,新行才被真正地創(chuàng)建到DataTable中,使用實例:
//添加一新行
DataRowView rowView = vue.AddNew();
rowView["CustomerID"] = "asdf";
rowView.EndEdit();
//修改一行
rowView.BeginEdit();
rowView["CustomerID"] = "劉明豐";
rowView.EndEdit();
//刪除一行
rowView.Delete();
使用DataView創(chuàng)建新的DataTable
使用實例
DataTable newTable = vue.ToTable("CreateNewTable");
轉(zhuǎn)載于:https://www.cnblogs.com/lmfeng/archive/2012/02/02/2335876.html
總結(jié)
以上是生活随笔為你收集整理的ADO.NET的记忆碎片(七)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 密码学-代数数论基本知识
- 下一篇: 使用ASP.NET广告控件的XML语言创