C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Info: This is a container for multiple DataTables. You can use it to create XML. It is a useful abstraction for simplifying programs.
Finally: We demonstrate that the GetXml() function will print formatted XML that represents the data.
VB.NET program that uses DataSet
Module Module1
Sub Main()
' Two DataTables.
Dim table1 As DataTable = New DataTable("patients")
table1.Columns.Add("name")
table1.Columns.Add("id")
table1.Rows.Add("sam", 1)
table1.Rows.Add("mark", 2)
Dim table2 As DataTable = New DataTable("medications")
table2.Columns.Add("id")
table2.Columns.Add("medication")
table2.Rows.Add(1, "atenolol")
table2.Rows.Add(2, "amoxicillin")
' Create a DataSet. Put both tables in it.
Dim set1 As DataSet = New DataSet("office")
set1.Tables.Add(table1)
set1.Tables.Add(table2)
' Visualize DataSet.
Console.WriteLine(set1.GetXml())
End Sub
End Module
Output
<office>
<patients>
<name>sam</name>
<id>1</id>
</patients>
<patients>
<name>mark</name>
<id>2</id>
</patients>
<medications>
<id>1</id>
<medication>atenolol</medication>
</medications>
<medications>
<id>2</id>
<medication>amoxicillin</medication>
</medications>
</office>
VB.NET program that shows Using statement with DataSet
Module Module1
Sub Main()
Using set1 As DataSet = New DataSet("office")
' Use set1 here.
End Using
End Sub
End Module
Here: Look at how the Namespace "y" and the Prefix "x" appear in the output of this program.
VB.NET program that uses Namespace and Prefix
Module Module1
Sub Main()
' Create DataTable.
Dim table1 As DataTable = New DataTable("patients")
table1.Columns.Add("name")
table1.Columns.Add("id")
table1.Rows.Add("sam", 1)
' Create DataSet.
Dim set1 As DataSet = New DataSet("office")
set1.Tables.Add(table1)
set1.Namespace = "y"
set1.Prefix = "x"
' Visualize it.
Console.WriteLine(set1.GetXml())
End Sub
End Module
Output
<x:office xmlns:x="y">
<patients xmlns="y">
<name>sam</name>
<id>1</id>
</patients>
</x:office>
VB.NET program that uses DataSetName
Module Module1
Sub Main()
' Initialize DataSet.
Dim set1 As DataSet = New DataSet("office")
' Display, set, then display DataSetName.
Console.WriteLine(set1.DataSetName)
set1.DataSetName = "unknown"
Console.WriteLine(set1.DataSetName)
End Sub
End Module
Output
office
unknown
Tip: When we call Clear, the copied DataSet is not changed. The two objects are in separate memory.
VB.NET program that uses Copy and Clear
Module Module1
Sub Main()
Dim table1 = New DataTable("patients")
table1.Columns.Add("name")
table1.Columns.Add("id")
table1.Rows.Add("sam", 1)
Dim table2 = New DataTable("medications")
table2.Columns.Add("id")
table2.Columns.Add("medication")
table2.Rows.Add(1, "atenolol")
' Create a DataSet instance.
Dim set1 As DataSet = New DataSet("office")
set1.Tables.Add(table1)
set1.Tables.Add(table2)
' Copy.
Dim copy As DataSet = set1.Copy()
' Clear original DataSet instance.
set1.Clear()
Console.WriteLine("set: {0}", set1.GetXml())
Console.WriteLine("copy: {0}", copy.GetXml())
End Sub
End Module
Output
set: <office />
copy: <office>
<patients>
<name>sam</name>
<id>1</id>
</patients>
<medications>
<id>1</id>
<medication>atenolol</medication>
</medications>
</office>
VB.NET program that loops through tables
Module Module1
Sub Main()
Dim table1 As DataTable = New DataTable("patients")
table1.Columns.Add("name")
table1.Columns.Add("id")
table1.Rows.Add("sam", 1)
Dim table2 As DataTable = New DataTable("medications")
table2.Columns.Add("id")
table2.Columns.Add("medication")
table2.Rows.Add(1, "atenolol")
table2.Rows.Add(6, "trifluoperazine")
' Create the DataSet.
Dim set1 As DataSet = New DataSet("office")
set1.Tables.Add(table1)
set1.Tables.Add(table2)
' Loop over tables in the DataSet.
Dim collection As DataTableCollection = set1.Tables
For i As Integer = 0 To collection.Count - 1
' Get table.
Dim table As DataTable = collection(i)
Console.WriteLine("{0}: {1}", i, table.TableName)
Next
' First table.
Console.WriteLine("x: {0}", set1.Tables(0).TableName)
' Row count of medications table.
Console.WriteLine("y: {0}", set1.Tables("medications").Rows.Count)
End Sub
End Module
Output
0: patients
1: medications
x: patients
y: 2
GetXml: This returns the data in the DataSet as XML. The return value is of type String.
GetXmlSchema: This returns a special document that reveals the layout of the data in the DataSet.
Thus: DataSet assumes its place at the root of the .NET Framework hierarchy of data objects.