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