显示结果
CustomerID | CompanyName | Country |
---|---|---|
WHITC | White Clover Markets | USA |
TRAIH | Trail's Head Gourmet Provisioners | USA |
THECR | The Cracker Box | USA |
THEBI | The Big Cheese | USA |
SPLIR | Split Rail Beer & Ale | USA |
SAVEA | Save-a-lot Markets | USA |
RATTC | Rattlesnake Canyon Grocery | USA |
OLDWO | Old World Delicatessen | USA |
LONEP | Lonesome Pine Restaurant | USA |
LETSS | Let's Stop N Shop | USA |
LAZYK | Lazy K Kountry Store | USA |
HUNGC | Hungry Coyote Import Store | USA |
GREAL | Great Lakes Food Market | USA |
获取 DataRow 对象的数组。
获取所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select() As DataRow()
[C#] public DataRow[] Select();
[C++] public: DataRow* Select() [];
[JScript] public function Select() : DataRow[];
按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select(String) As DataRow()
[C#] public DataRow[] Select(string);
[C++] public: DataRow* Select(String*) [];
[JScript] public function Select(String) : DataRow[];
获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select(String, String) As DataRow()
[C#] public DataRow[] Select(string, string);
[C++] public: DataRow* Select(String*, String*) [];
[JScript] public function Select(String, String) : DataRow[];
获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。
受 .NET Framework 精简版的支持。
[Visual Basic] Overloads Public Function Select(String, String, DataViewRowState) As DataRow()
[C#] public DataRow[] Select(string, string, DataViewRowState);
[C++] public: DataRow* Select(String*, String*, DataViewRowState) [];
[JScript] public function Select(String, String, DataViewRowState) : DataRow[];
[Visual Basic, C#] 以下示例使用筛选表达式和记录状态来返回 DataRow 对象的数组。
[Visual Basic, C#] 注意 此示例显示如何使用 Select 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Private Sub GetRowsByFilter()
Dim customerTable As DataTable
customerTable = new DataTable( "Customers" )
' Add columns
customerTable.Columns.Add( "id", GetType(Integer) )
customerTable.Columns.Add( "name", GetType(String) )
' Set PrimaryKey
customerTable.Columns("id").Unique = true
customerTable.PrimaryKey = new DataColumn() { customerTable.Columns("id") }
' add ten rows
Dim id As Integer
For id = 1 To 10
customerTable.Rows.Add( _
new object() { id, string.Format("customer{0}", id) } )
Next id
customerTable.AcceptChanges()
' add another ten rows
For id = 11 To 20
customerTable.Rows.Add( _
new object() { id, string.Format("customer{0}", id) } )
Next id
Dim strExpr As String
Dim strSort As String
strExpr = "id > 5"
' Sort descending by CompanyName column.
strSort = "name DESC"
' Use the Select method to find all rows matching the filter.
Dim foundRows As DataRow() = _
customerTable.Select( strExpr, strSort, DataViewRowState.Added )
PrintRows( foundRows, "filtered rows")
foundRows = customerTable.Select()
PrintRows( foundRows, "all rows")
End Sub
Private Sub PrintRows( rows() As DataRow, label As String)
Console.WriteLine( "\n{0}", label )
If rows.Length <= 0 Then
Console.WriteLine( "no rows found" )
Exit Sub
End If
Dim r As DataRow
Dim c As DataColumn
For Each r In rows
For Each c In r.Table.Columns
Console.Write( "\t {0}", r(c) )
Next c
Console.WriteLine()
Next r
End Sub
[C#]
private static void GetRowsByFilter()
{
DataTable customerTable = new DataTable( "Customers" );
// Add columns
customerTable.Columns.Add( "id", typeof(int) );
customerTable.Columns.Add( "name", typeof(string) );
// Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["id"] };
// Add ten rows
for( int id=1; id<=10; id++ )
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) } );
}
customerTable.AcceptChanges();
// Add another ten rows
for( int id=11; id<=20; id++ )
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) } );
}
string strExpr;
string strSort;
strExpr = "id > 5";
// Sort descending by column named CompanyName.
strSort = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select( strExpr, strSort, DataViewRowState.Added );
PrintRows( foundRows, "filtered rows" );
foundRows = customerTable.Select();
PrintRows( foundRows, "all rows" );
}
private static void PrintRows( DataRow[] rows, string label )
{
Console.WriteLine( "\n{0}", label );
if( rows.Length <= 0 )
{
Console.WriteLine( "no rows found" );
return;
}
foreach( DataRow r in rows )
{
foreach( DataColumn c in r.Table.Columns )
{
Console.Write( "\t {0}", r[c] );
}
Console.WriteLine();
}
}
[C++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的“语言筛选器”按钮 。