Erstellen einer 'DataTable' aus einer 'DataView' (ADO.NET)
Aktualisiert: November 2007
Nachdem Sie Daten aus einer Datenquelle abgerufen und eine DataTable mit Daten gefüllt haben, möchten Sie die zurückgegebenen Daten möglicherweise sortieren, filtern oder anderweitig einschränken, ohne sie erneut abrufen zu müssen. Dies wird durch die DataView-Klasse ermöglicht. Wenn Sie außerdem eine neue DataTableaus der DataView erstellen müssen, können Sie die ToTable-Methode verwenden, um alle Zeilen und Spalten oder eine Teilmenge von Daten in eine neue DataTable zu kopieren. Die ToTable-Methode stellt Überladungen für folgende Vorgänge bereit:
Erstellen einer DataTable, die Spalten enthält, bei denen es sich um eine Teilmenge der Spalten in der DataView handelt.
Erstellen einer DataTable, die, analog zum Schlüsselwort DISTINCT in Transact-SQL, nur eindeutige Zeilen aus dem DataView umfasst.
Beispiel
Im folgenden Beispiel einer Konsolenanwendung wird eine DataTableerstellt, die Daten aus der Person.Contact-Tabelle in der AdventureWorks-Beispieldatenbank enthält. Anschließend wird im Beispiel eine sortierte und gefilterte DataViewauf der Grundlage der DataTable erstellt. Nach dem Anzeigen der Inhalte der DataTableund der DataView wird im Beispiel aus der DataView eine neue DataTableerstellt. Dazu wird die ToTable-Methode aufgerufen, die nur eine Teilmenge der verfügbaren Spalten auswählt. Zum Schluss werden im Beispiel die Inhalte der neuen DataTable angezeigt.
Private Sub DemonstrateDataView()
' Retrieve a DataTable from the AdventureWorks sample database.
' connectionString is assumed to be a valid connection string.
Dim adapter As New SqlDataAdapter( _
"SELECT FirstName, LastName, EmailAddress FROM Person.Contact WHERE FirstName LIKE 'Mich%'", connectionString)
Dim table As New DataTable
adapter.Fill(table)
Console.WriteLine("Original table name: " & table.TableName)
' Print current table values.
PrintTableOrView(table, "Current Values in Table")
' Now create a DataView based on the DataTable.
' Sort and filter the data.
Dim view As DataView = table.DefaultView
view.Sort = "LastName, FirstName"
view.RowFilter = "LastName > 'M'"
PrintTableOrView(view, "Current Values in View")
' Create a new DataTable based on the DataView,
' requesting only two columns with distinct values
' in the columns.
Dim newTable As DataTable = view.ToTable("UniqueLastNames", True, "FirstName", "LastName")
PrintTableOrView(newTable, "Table created from DataView")
Console.WriteLine("New table name: " & newTable.TableName)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Sub PrintTableOrView(ByVal dv As DataView, ByVal label As String)
Dim sw As System.IO.StringWriter
Dim output As String
Dim table As DataTable = dv.Table
Console.WriteLine(label)
' Loop through each row in the view.
For Each rowView As DataRowView In dv
sw = New System.IO.StringWriter
' Loop through each column.
For Each col As DataColumn In table.Columns
' Output the value of each column's data.
sw.Write(rowView(col.ColumnName).ToString() & ", ")
Next
output = sw.ToString
' Trim off the trailing ", ", so the output looks correct.
If output.Length > 2 Then
output = output.Substring(0, output.Length - 2)
End If
' Display the row in the console window.
Console.WriteLine(output)
Next
Console.WriteLine()
End Sub
Private Sub PrintTableOrView(ByVal table As DataTable, ByVal label As String)
Dim sw As System.IO.StringWriter
Dim output As String
Console.WriteLine(label)
' Loop through each row in the table.
For Each row As DataRow In table.Rows
sw = New System.IO.StringWriter
' Loop through each column.
For Each col As DataColumn In table.Columns
' Output the value of each column's data.
sw.Write(row(col).ToString() & ", ")
Next
output = sw.ToString
' Trim off the trailing ", ", so the output looks correct.
If output.Length > 2 Then
output = output.Substring(0, output.Length - 2)
End If
' Display the row in the console window.
Console.WriteLine(output)
Next
Console.WriteLine()
End Sub
End Module
private static void DemonstrateDataView()
{
// Retrieve a DataTable from the AdventureWorks sample database.
// connectionString is assumed to be a valid connection string.
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT FirstName, LastName, EmailAddress " +
"FROM Person.Contact WHERE FirstName LIKE 'Mich%'",
GetConnectionString());
DataTable table = new DataTable();
adapter.Fill(table);
Console.WriteLine("Original table name: " + table.TableName);
// Print current table values.
PrintTableOrView(table, "Current Values in Table");
// Now create a DataView based on the DataTable.
// Sort and filter the data.
DataView view = table.DefaultView;
view.Sort = "LastName, FirstName";
view.RowFilter = "LastName > 'M'";
PrintTableOrView(view, "Current Values in View");
// Create a new DataTable based on the DataView,
// requesting only two columns with distinct values
// in the columns.
DataTable newTable = view.ToTable("UniqueLastNames",
true, "FirstName", "LastName");
PrintTableOrView(newTable, "Table created from DataView");
Console.WriteLine("New table name: " + newTable.TableName);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static void PrintTableOrView(DataView dv, string label)
{
System.IO.StringWriter sw;
string output;
DataTable table = dv.Table;
Console.WriteLine(label);
// Loop through each row in the view.
foreach (DataRowView rowView in dv)
{
sw = new System.IO.StringWriter();
// Loop through each column.
foreach (DataColumn col in table.Columns)
{
// Output the value of each column's data.
sw.Write(rowView[col.ColumnName].ToString() + ", ");
}
output = sw.ToString();
// Trim off the trailing ", ", so the output looks correct.
if (output.Length > 2)
{
output = output.Substring(0, output.Length - 2);
}
// Display the row in the console window.
Console.WriteLine(output);
}
Console.WriteLine();
}
private static void PrintTableOrView(DataTable table, string label)
{
System.IO.StringWriter sw;
string output;
Console.WriteLine(label);
// Loop through each row in the table.
foreach (DataRow row in table.Rows)
{
sw = new System.IO.StringWriter();
// Loop through each column.
foreach (DataColumn col in table.Columns)
{
// Output the value of each column's data.
sw.Write(row[col].ToString() + ", ");
}
output = sw.ToString();
// Trim off the trailing ", ", so the output looks correct.
if (output.Length > 2)
{
output = output.Substring(0, output.Length - 2);
}
// Display the row in the console window.
Console.WriteLine(output);
} //
Console.WriteLine();
}
}