C# dataset存放多張表的實例
在C#中用同一個dataset保存從數(shù)據(jù)庫中取出的多張表:
cmd.CommandText = "select * from table1;"; NpgsqlDataAdapter da = new NpgsqlDataAdapter(cmd); //實例化一個類,它用于連接C#與數(shù)據(jù)庫,并執(zhí)行cmd語句且將結(jié)果緩存到適配器中 DataSet thedataset = new DataSet(); //實例化一個dataset,實例名為thedataset(通常被定義為ds) da.Fill(thedataset, "thetable1"); //將適配器中的內(nèi)容填充到dataset的thetable1表中, thetable1同時被建立 cmd.Parameters.Clear(); //清空cmd內(nèi)容,如果不清空下次使用時會拋出異常 cmd.CommandText = "select * from table2;"; da = new NpgsqlDataAdapter(cmd); da.Fill(thedataset, "thetable2"); //將適配器中的內(nèi)容填充到dataset的thetable2表中, thetable2同時被建立 cmd.Parameters.Clear(); //清空cmd內(nèi)容 ... int count = thedataset.Tables["thetable1"].Rows.Count; //獲取表thetable1的行數(shù) string a = thedataset.Tables["thetable2"].Rows[1][0].ToString().; //獲取表thetable2第2行第1列的值
補充:在DataSet中訪問多個表
ADO.Net模型有一個很大的優(yōu)點,就是DataSet對象可以跟蹤多個表和它們之間的關(guān)系。這表示可以在一個操作的不同程序段之間傳遞完整的相關(guān)數(shù)據(jù)集,體系結(jié)構(gòu)內(nèi)在地維護數(shù)據(jù)之間關(guān)系的完整性。
ADO.Net中的DataRelation對象用于描述DataSet中的多個DataTables對象之間的關(guān)系。每個DataSet都包含DataRelations的Relations集合,以查找和操縱相關(guān)表。DataSet的Relations屬性是一個DataRelation對象的集合,DataRelation對象表示這個DataSet之間表之間的關(guān)系。要創(chuàng)建一個新的DataRelation,可以使用Relations的Add()方法,該方法接收表示關(guān)系的字符串名和兩個DataColumn(父列后跟子列)。比如:要創(chuàng)建Customers表的CustomerID列和Orders表的CustomerID列之間的關(guān)系 ,應(yīng)使用下面的語法,把它們的關(guān)系命名為CustOrders。
DataRelation custOrderRel = ds.Relations.Add("CustOrders", ds.Tables["Customers"].Columns["CustomerID"], ds.Tables["Orders"].Columns["CustomerID"]);
為了使用有關(guān)系,需要從一張表的行進入另一張表的關(guān)聯(lián)行,這就是對關(guān)系導(dǎo)航。通常導(dǎo)航是指從一張表的父行進入另一張表的子行。那么假如給定父表中的一行,如何獲取子表中與其對應(yīng)的所有行呢?我們可以使用DataRow對象的GetChildRows()方法提取這些行。示例:一個顧客(Customers)表包含有一個或多個訂單(Orders)表,建立這兩個表之間的數(shù)據(jù)并提取數(shù)據(jù)的代碼如下。
static void Main(string[] args) { string connStr = @"Data Source=.\SQLEXPRESS; AttachDbFilename='C:\SQL Sever 2000 Sample Databases\NORTHWND.MDF';Integrated Security=True;User Instance=true"; SqlConnection conn = new SqlConnection(connStr); conn.Open(); //創(chuàng)建用于保存修改的數(shù)據(jù)的適配器 SqlDataAdapter adapter = new SqlDataAdapter("select CustomerID,CompanyName from Customers", conn); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); //創(chuàng)建數(shù)據(jù)集 DataSet ds = new DataSet(); //創(chuàng)建讀取Customers表的適配器 SqlDataAdapter custAdapter = new SqlDataAdapter("select * from Customers", conn); //創(chuàng)建讀取Orders表的適配器 SqlDataAdapter orderAdapter = new SqlDataAdapter("select * from Orders", conn); //填充兩個表的數(shù)據(jù)并放到DataSet中 custAdapter.Fill(ds, "Customers"); orderAdapter.Fill(ds, "Orders"); //創(chuàng)建兩個表之間的關(guān)系 DataRelation custOrderRel = ds.Relations.Add("CustOrders", ds.Tables["Customers"].Columns["CustomerID"], ds.Tables["Orders"].Columns["CustomerID"]); foreach (DataRow custRow in ds.Tables["Customers"].Rows) { Console.WriteLine("Customer ID: " + custRow["CustomerID"] + "\tName: " + custRow["CompanyName"]); foreach (DataRow orderRow in custRow.GetChildRows(custOrderRel)) { Console.WriteLine(" Order ID: "+orderRow["OrderID"]); } } conn.Close(); Console.ReadKey(); }
利用兩個表之間的關(guān)系訪問表中的數(shù)據(jù)的時候,我們還可以使用Linq over DataSet 。這需要導(dǎo)入System.Data.Linq命名空間。我們可以使用如下代碼代替上述代碼中的foreach部分:
var preferredCustomers = from c in Customers where c.GetChildRows("CustOrders").Length > 10 orderby c.GetChildRows("CustOrders").Length select c; Console.WriteLine("Customers with > 10 orders:"); foreach (var customer in preferredCustomers) { Console.WriteLine("{0} orders: {1} {2}, {3} {4}",customer.GetChildRows("CustOrders").Length, customer["CustomerID"],customer["CompanyName"],customer["City"],customer["Region"]); }
表之間的關(guān)系除了兩個表之間的關(guān)系,還有更復(fù)雜的多表連接。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#中把DataTable、Dataset轉(zhuǎn)Json數(shù)據(jù)
這篇文章介紹了C#中把DataTable、Dataset轉(zhuǎn)Json數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04C#使用System.Net.Mail類實現(xiàn)郵件發(fā)送
這篇文章介紹了C#使用System.Net.Mail類實現(xiàn)郵件發(fā)送的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07