如何將資料集映射儲存到 XSD 架構檔案
此樣本闡釋如何將內部資料集 (DataSet) 映射儲存到 XML 結構定義語言 (XSD) 架構檔案中。此樣本通過使用資料集上的關係方法產生映射以建立表和列。然後,該樣本將這些映射的 XSD 架構表示形式寫到檔案中。
| |
| VB SaveDataSetMapXSDSchema.aspx |
[運行樣本] | [查看原始碼] |
DataSet 和 XmlDataDocument 類都表示記憶體中的資料緩衝。DataSet 提供面向關係的導航和編輯方法,而 XmlDataDocument 提供 XML 導航和編輯方法。
此樣本從 XmlDataDocument 擷取 DataSet 屬性,使用該屬性產生一組表和列,然後填充這些表和列。然後,該樣本寫出內部產生的架構。
下列範例程式碼產生兩個表,一個用於人,另一個用於寵物。該樣本使用 ID 作為每個表的主關鍵字,並建立人和他們的寵物之間的關係表。為此,該樣本首先建立 XmlDataDocument 的一個執行個體,然後將與 XmlDataDocument 關聯的資料集傳遞給 LoadDataSet 方法。
XmlDataDocument datadoc = new XmlDataDocument(); LoadDataSet(datadoc.DataSet);
Dim datadoc As XmlDataDocument = New XmlDataDocument() LoadDataSet(datadoc.DataSet) |
| C# |
VB |
|
LoadDataSet 方法使用關係資料載入資料集。
// Load a DataSet with relational data private void LoadDataSet(DataSet dataset) { try { Console.WriteLine("Loading the DataSet ..."); // Set DataSet name dataset.DataSetName = "PersonPet"; // Create tables for people and pets DataTable people = new DataTable("Person"); DataTable pets = new DataTable("Pet"); // Set up the columns in the Tables DataColumn personname = new DataColumn ("Name", typeof(String)); DataColumn personAge = new DataColumn ("Age", typeof(Int32)); DataColumn petname = new DataColumn ("Name", typeof(String)); DataColumn pettype = new DataColumn ("Type", typeof(String)); // Add columns to person table DataColumn id = people.Columns.Add("ID", typeof(Int32)); id.AutoIncrement = true; people.PrimaryKey = new DataColumn[] {id}; people.Columns.Add (personname); people.Columns.Add (personAge); // Add columns to pet table id = pets.Columns.Add("ID", typeof(Int32)); id.AutoIncrement = true; pets.PrimaryKey = new DataColumn[] {id}; id.AutoIncrement = true; DataColumn ownerid = pets.Columns.Add("OwnerID", typeof(Int32)); DataColumn[] foreignkey = new DataColumn[] {ownerid}; pets.Columns.Add (petname); pets.Columns.Add (pettype); // Add tables to the DataSet dataset.Tables.Add (people); dataset.Tables.Add (pets); // Add people DataRow mark = people.NewRow(); mark[personname] = "Mark"; mark[personAge] = 18; people.Rows.Add(mark); DataRow william = people.NewRow(); william[personname] = "William"; william[personAge] = 12; people.Rows.Add(william); DataRow james = people.NewRow(); james[personname] = "James"; james[personAge] = 7; people.Rows.Add(james); DataRow levi = people.NewRow(); levi[personname] = "Levi"; levi[personAge] = 4; people.Rows.Add(levi); // Add relationships Console.WriteLine("Creating relationships between people and pets ..."); DataRelation personpetrel = new DataRelation ("PersonPet",people.PrimaryKey, foreignkey); dataset.Relations.Add (personpetrel); // Add pets DataRow row = pets.NewRow(); row["OwnerID"] = mark["ID"]; row[petname] = "Frank"; row[pettype] = "cat"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = william["ID"]; row[petname] = "Rex"; row[pettype] = "dog"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = james["ID"]; row[petname] = "Cottontail"; row[pettype] = "rabbit"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = levi["ID"]; row[petname] = "Sid"; row[pettype] = "snake"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = levi["ID"]; row[petname] = "Tickles"; row[pettype] = "spider"; pets.Rows.Add(row); row = pets.NewRow(); row["OwnerID"] = william["ID"]; row[petname] = "Tweetie"; row[pettype] = "canary"; pets.Rows.Add(row); // commit changes dataset.AcceptChanges(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } }
' Load a DataSet with relational data private sub LoadDataSet(dataset as DataSet) try Console.WriteLine("Loading the DataSet ...") ' Set DataSet name dataset.DataSetName = "PersonPet" ' Create tables for people and pets Dim people as DataTable = new DataTable("Person") Dim pets as DataTable = new DataTable("Pet") ' Set up the columns in the Tables Dim personname as DataColumn = new DataColumn ("Name", GetType (System.String)) Dim personAge as DataColumn = new DataColumn ("Age", GetType (System.Int32)) Dim petname as DataColumn = new DataColumn ("Name", GetType (System.String)) Dim pettype as DataColumn = new DataColumn ("Type", GetType (System.String)) ' Add columns to person table Dim id as DataColumn = people.Columns.Add("ID", GetType (System.Int32)) id.AutoIncrement = true Dim primarykey as DataColumn() = new DataColumn() {id} people.PrimaryKey = primarykey people.Columns.Add (personname) people.Columns.Add (personAge) ' Add columns to pet table id = pets.Columns.Add("ID", GetType (System.Int32)) id.AutoIncrement = true pets.PrimaryKey = new DataColumn() {id} id.AutoIncrement = true Dim ownerid as DataColumn = pets.Columns.Add("OwnerID", GetType (System.Int32)) Dim foreignkey as DataColumn() = new DataColumn() {ownerid} pets.Columns.Add (petname) pets.Columns.Add (pettype) ' Add tables to the DataSet dataset.Tables.Add (people) dataset.Tables.Add (pets) ' Add people Dim mark as DataRow = people.NewRow() mark(personname) = "Mark" mark(personAge) = 18 people.Rows.Add(mark) Dim william as DataRow = people.NewRow() william(personname) = "William" william(personAge) = 12 people.Rows.Add(william) Dim james as DataRow = people.NewRow() james(personname) = "James" james(personAge) = 19 people.Rows.Add(james) Dim levi as DataRow = people.NewRow() levi(personname) = "Levi" levi(personAge) = 4 people.Rows.Add(levi) ' Add relationships Console.WriteLine("Creating relationships between people and pets ...") Dim personpetrel as DataRelation = new DataRelation ("PersonPet", primarykey, foreignkey, false) dataset.Relations.Add (personpetrel) ' Add pets Dim row as DataRow = pets.NewRow() row("OwnerID") = mark("ID") row(petname) = "Frank" row(pettype) = "cat" pets.Rows.Add(row) row = pets.NewRow() row("OwnerID") = william("ID") row(petname) = "Rex" row(pettype) = "dog" pets.Rows.Add(row) row = pets.NewRow() row("OwnerID") = james("ID") row(petname) = "Cottontail" row(pettype) = "rabbit" pets.Rows.Add(row) row = pets.NewRow() row("OwnerID") = levi("ID") row(petname) = "Sid" row(pettype) = "snake" pets.Rows.Add(row) row = pets.NewRow() row("OwnerID") = levi("ID") row(petname) = "Tickles" row(pettype) = "spider" pets.Rows.Add(row) row = pets.NewRow() row("OwnerID") = william("ID") row(petname) = "Tweetie" row(pettype) = "canary" pets.Rows.Add(row) ' commit changes dataset.AcceptChanges() catch e as Exception Console.WriteLine ("Exception: {0}", e.ToString()) end try end sub |
| C# |
VB |
|
DataSet 的 AcceptChanges 方法接受所有的更改,這些更改是自載入資料集以來或自上次調用 AcceptChanges 以來對資料集所做的更改。所有新的和修改過的行保持不變,移除已刪除的行。有關其他 DataSet 關係方法的更多資訊,請參閱如何擷取 ADO.NET 的概述
為了將架構儲存到檔案,該樣本調用 DataSet 的 WriteXmlSchema 方法,傳遞表示目標檔案的 StreamWriter 類。
StreamWriter writer = null; try { Console.WriteLine("Writing the schema to {0} ...", mySaveSchema); writer = new StreamWriter(mySaveSchema); datadoc.DataSet.WriteXmlSchema(writer); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } finally { if (writer != null) writer.Close(); }
Dim writer as StreamWriter try Console.WriteLine("Writing the schema to {0} ...", mySaveSchema) writer = new StreamWriter(mySaveSchema) datadoc.DataSet.WriteXmlSchema(writer) catch e as Exception Console.WriteLine ("Exception: {0}", e.ToString()) finally If Not writer Is Nothing writer.Close() end if end try |
| C# |
VB |
|
下列輸出顯示由 DisplayTables 方法在資料集中建立的表。有關 DisplayTables 方法的更多資訊,請參閱如何從 XML 推匯出資料集映射。該樣本將推匯出的架構寫到 PersonPet.xsd 檔案中。
Loading the DataSet ...Creating relationships between people and pets ...DataSet:PersonPet contains ...No of Tables: 2 Table content ...TableName = Person---------Columns ...ID Name AgeNumber of rows = 4Rows ...0 Mark 181 William 122 James 193 Levi 4TableName = Pet---------Columns ...ID OwnerID Name TypeNumber of rows = 6Rows ...0 0 Frank cat1 1 Rex dog2 2 Cottontail rabbit3 3 Sid snake4 3 Tickles spider5 1 Tweetie canaryPersonPetName = Mark ownsPet = Frank the catName = William ownsPet = Rex the dogPet = Tweetie the canaryName = James ownsPet = Cottontail the rabbitName = Levi ownsPet = Sid the snakePet = Tickles the spiderWriting the schema to PersonPet.xsd ...
摘要
- WriteXmlSchema 方法將資料集中的關係資料的內部結構映射儲存為 XSD 架構。
- XmlDataDocument 的 DataSet 屬性使您能夠相關地查看和管理 XML 文檔中的結構化資料