using System;
using System.Data;
using System.Data.SqlClient;
namespace DataTableArticle
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
SqlConnection oConn = new SqlConnection();
DataTable oTable = new DataTable();
DataRow[] oRows;
string sConnectionString = "Data Source=(local);User ID=username;Password=pwd;Initial Catalog=Northwind";
try
{
oConn.ConnectionString = sConnectionString;
oConn.Open();
SqlDataAdapter oDA = new SqlDataAdapter("select CustomerID,ContactName from Customers order by ContactName ASC",oConn);
oDA.Fill(oTable);
oConn.Close();
Console.WriteLine("Start Series 1\n");
foreach(DataRow oRow in oTable.Rows)
{
Console.WriteLine(oRow["CustomerID"].ToString() + ": " + oRow["ContactName"].ToString());
}
Console.WriteLine("End Series 1\n\n");
Console.WriteLine("Start Series 2\n");
oRows = oTable.Select(null,"ContactName DESC");
foreach(DataRow oRow in oRows)
{
Console.WriteLine(oRow["CustomerID"].ToString() + ": " + oRow["ContactName"].ToString());
}
Console.WriteLine("\nEnd Series 2\n\n");
Console.WriteLine("Start Series 3\n");
oRows = oTable.Select("CustomerID in ('ALFKI','ANATR')","ContactName ASC");
foreach(DataRow oRow in oRows)
{
Console.WriteLine(oRow["CustomerID"].ToString() + ": " + oRow["ContactName"].ToString());
}
Console.WriteLine("\nEnd Series 3\n");
}
catch (Exception e) { Console.WriteLine(e.Message); }
finally { if (oConn.State == ConnectionState.Open) { oConn.Close(); } }
Console.ReadLine();
}
}
}
|