DataTable.Select 方法

DataTable.Select方法返回获取所有DataRow对象的数组。Select()有四个重载方法.

 名称说明
公共方法 由 XNA Framework 提供支持Select ()获取所有 DataRow 对象的数组。
公共方法 由 XNA Framework 提供支持Select(String)按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。
公共方法 由 XNA Framework 提供支持Select(String, String)获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow  对象的数组。
公共方法 由 XNA Framework 提供支持Select(String, String, DataViewRowState)获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow  对象的数组。

 

1.Select()示例:

 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.Select(string filterExpression)示例:

 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.Select(string filterExpression,string sort)示例:

    private void GetRowsByFilter()
   {
       DataTable table = DataSet1.Tables["Orders"];

       // Presuming the DataTable has a column named Date.
       string expression = "Date > '1/1/00'";

       // Sort descending by column named CompanyName.
       string sortOrder = "CompanyName DESC";
       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][0]);
       }
   }
4.Select(string filterExpression,string sort, DataViewRowState recordStates)示例:

 

     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();
        }
    }

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值