比如我在一个新的DateSet里充填一个表。我如何用一个字符型变量(比如string query)来返回DateSet里的表的某一行的某一列?谢谢!

解决方案 »

  1.   

    DataSet or DataTable doesn't support string query, use DataTable's Select(..) method or DataView's RowFilter property
      

  2.   

    DataTable.Select 方法获取 DataRow 对象的数组。
    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();
        }
    }
      

  3.   

    例如:string UID=ds.Tables["tablename"].Rows[rowindex]["uid"].ToString();
    就是取表tablename里第rowindex行的uid列的值
      

  4.   

    DataSet ds=new DataSet();
    //取第一行第一列的值
    string query=ds.Tables["tablename"].Rows[0][0].ToString();