zoukankan      html  css  js  c++  java
  • DataTable的Select方法

    DataTable的Select方法

    获取 DataRow 对象的数组。

    重载列表
    名称                    说明
    Select()               获取所有 DataRow 对象的数组。
    Select(String)      
                                 按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 DataRow 对象的数组。  

    Select(String, String)                                           
                                 获取按照指定的排序顺序且与筛选条件相匹配的所有 DataRow 对象的数组。
    Select(String, String, DataViewRowState)
                                  获取与排序顺序中的筛选器以及指定的状态相匹配的所有 DataRow 对象的数组。

    示例:

    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"]);
        }
    }

    //**********************************************************************************************************

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

    //**********************************************************************************************************

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

    //**********************************************************************************************************

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

  • 相关阅读:
    泛型
    HDU 4917 Permutation
    OC本学习笔记Foundation框架NSString与NSMutableString
    HDU 5095 Linearization of the kernel functions in SVM(模拟)
    大约Java有点感悟---开发商根本上感悟学习
    Codeforces 442B Andrey and Problem(贪婪)
    mysql数据库优化课程---15、mysql优化步骤(mysql中最常用最立竿见影的优化是什么)
    mysql数据库优化课程---14、常用的sql技巧
    mysql数据库优化课程---13、mysql基础操作(mysql如何复制表)
    mysql数据库优化课程---12、mysql嵌套和链接查询(查询user表中存在的所有班级的信息?)
  • 原文地址:https://www.cnblogs.com/nianshi/p/1603740.html
Copyright © 2011-2022 走看看