Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Nachdem Sie Daten aus einer Datenquelle abgerufen und eine DataTable mit den 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 DataTable aus der DataView erstellen müssen, können Sie mit der ToTable-Methode alle Zeilen und Spalten oder eine Teilmenge der Daten in eine neue DataTable 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 Sie eine DataTable, die analog zum DataView-Schlüsselwort in Transact-SQL nur unterschiedliche Zeilen von dem
DISTINCT
enthält.
Beispiel
Im folgenden Beispiel einer Konsolenanwendung wird eine DataTable erstellt, die Daten aus der Tabelle Person.Contact in der AdventureWorks-Beispieldatenbank enthält. Anschließend wird im Beispiel eine sortierte und gefilterte DataView auf Grundlage der DataTable erstellt. Nach dem Anzeigen des Inhalts der DataTable und der DataView wird im Beispiel aus der DataTable eine neue DataView erstellt. 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();
}