本文整理汇总了C#中System.Data.DataTable.Select方法的典型用法代码示例。如果您正苦于以下问题:C# DataTable.Select方法的具体用法?C# DataTable.Select怎么用?C# DataTable.Select使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.DataTable
的用法示例。
在下文中一共展示了DataTable.Select方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRows
private void GetRows()
{
// Get the DataTable of a DataSet.
DataTable table = DataSet1.Tables["Suppliers"];
DataRow[] rows = table.Select();
// Print the value one column of each DataRow.
for(int i = 0; i < rows.Length ; i++)
{
Console.WriteLine(rows[i]["CompanyName"]);
}
}
示例2: GetRowsByFilter
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["Orders"];
// Presuming the DataTable has a column named Date.
string expression;
expression = "Date > #1/1/00#";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
示例3: Main
//引入命名空间
using System;
using System.Data;
public class A {
public static void Main() {
DataTable table = new DataTable("Orders");
table.Columns.Add("OrderID", typeof(Int32));
table.Columns.Add("OrderQuantity", typeof(Int32));
table.Columns.Add("CompanyName", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
DataRow newRow = table.NewRow();
newRow["OrderID"] = 1;
newRow["OrderQuantity"] = 3;
newRow["CompanyName"] = "NewCompanyName";
newRow["Date"] = "1979, 1, 31";
// Add the row to the rows collection.
table.Rows.Add(newRow);
DataRow newRow2 = table.NewRow();
newRow2["OrderID"] = 2;
newRow2["OrderQuantity"] = 2;
newRow2["CompanyName"] = "NewCompanyName1";
table.Rows.Add(newRow2);
DataRow newRow3 = table.NewRow();
newRow3["OrderID"] = 3;
newRow3["OrderQuantity"] = 2;
newRow3["CompanyName"] = "NewCompanyName2";
table.Rows.Add(newRow3);
// Presuming the DataTable has a column named Date.
string expression = "Date = '1/31/1979' or OrderID = 2";
// string expression = "OrderQuantity = 2 and OrderID = 2";
// Sort descending by column named CompanyName.
string sortOrder = "CompanyName ASC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for (int i = 0; i < foundRows.Length; i++)
Console.WriteLine(foundRows[i][2]);
}
}
示例4: GetRowsByFilter
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 expression;
string sortOrder;
expression = "id > 5";
// Sort descending by column named CompanyName.
sortOrder = "name DESC";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows =
customerTable.Select(expression, sortOrder,
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 row in rows)
{
foreach(DataColumn column in row.Table.Columns)
{
Console.Write("\table {0}", row[column]);
}
Console.WriteLine();
}
}