Dataset data filtering and sorting

Source: Internet
Author: User

Code

// Create a data adapter first,

Sqldataadapter da = new sqldataadapter ();

Da. selectcommand = new sqlcommand (SQL, Conn );

// Create and fill a dataset

Dataset DS = new dataset ();

Da. Fill (DS, "MERs ");

// Each query returns a dataset, and each result is saved in a separate able. The first display name is MERs, and the second expression name is customers1by default.

// Obtain the Ables object set from the tables attribute of the dataset

Datatablecollection DTC = Ds. tables;

When the first table is displayed:

String FL = "Country = 'Germany '"; // SQL where

String SRT = "companyName DESC"; // SQL DESC


UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
UsingSystem. Data;
UsingSystem. Data. sqlclient;

Namespace Leleapplication1
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Sqlconnection Conn =   New Sqlconnection ( " Server = zhuobin; uid = sa; Pwd = zhuobin; database = northwind " );
String Sql1 =   @" Select * from MERs " ; // Notice: Add a space after the string sql1
String Sql2 =   @" Select * from products where unitprice <10 " ;
String SQL = Sql1 + Sql2;
Try
{

Sqldataadapter da =   New Sqldataadapter ();
Da. selectcommand =   New Sqlcommand (SQL, Conn );
Dataset DS =   New Dataset ();

Da. Fill (DS, " MERs " );
// Console. writeline ("I love you! ");

// Get the table collections
Datatablecollection DTC = DS. tables; // The tables collections of the DS
// Display data from the first table
Console. writeline ( " Result from the customers tables: " );

Console. writeline ( " CompanyName " . Padright ( 20 ), " Contactname " . Padright ( 23 ) + " \ N " );
// Set display Filter

String FL =   " Country = 'Germany' " ;
// Set sort
String SRT =   " CompanyName " ;
// Display the filtered and sorted data
Foreach (Datarow row In DTC [ " MERs " ]. Select (FL, SRT ))
{
Console. writeline ( " {0} \ t {1} " , Row [ " CompanyName " ]. Tostring (). padright ( 25 ), Row [ " Contactname " ]. Tostring ());
}
// Display the data from the second table
Console. writeline ( " \ N ----------------------------------------------------- " );
Console. writeline ( " The result from products: " );
Console. writeline ( " Productname " . Padright ( 20 ), " Uintprice " . Padright ( 21 ));
// Display Data
Foreach (Datarow row In DTC [ 1 ]. Rows)
{
Console. writeline ( " {0} \ t {1} " , Row [ " Productname " ]. Tostring (). padright ( 25 ), Row [ " Unitprice " ]. Tostring ());
}

}
Catch (Sqlexception ex)
{
Console. writeline ( " The error {0} " , Ex. Message );
}
Finally
{
Conn. Close ();
}
Console. Readline ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.