Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
String sql = @"select productname,unitprice from products where unitprice<20 and productname='Chai'";
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds,"products");
DataTable dt=ds.Tables["products"];
//display data
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("The row is "+row.ToString());
Console.WriteLine(dt.Rows.Count.ToString());
foreach (DataColumn col in dt.Columns)
{
Console.WriteLine(row[col]);//mean that rows["productname"]
Console.WriteLine("The column is "+col.ToString());//output the field!
} Console.WriteLine("".PadLeft(20,'='));
}
}
catch (SqlException ex)
{
Console.WriteLine("Error:{0}", ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=zhuobin;uid=sa;pwd=zhuobin;database=Northwind");
String sql = @"select productname,unitprice from products where unitprice<20 and productname='Chai'";
try
{
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sql,conn);
DataSet ds = new DataSet();
da.Fill(ds,"products");
DataTable dt=ds.Tables["products"];
//display data
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("The row is "+row.ToString());
Console.WriteLine(dt.Rows.Count.ToString());
foreach (DataColumn col in dt.Columns)
{
Console.WriteLine(row[col]);//mean that rows["productname"]
Console.WriteLine("The column is "+col.ToString());//output the field!
} Console.WriteLine("".PadLeft(20,'='));
}
}
catch (SqlException ex)
{
Console.WriteLine("Error:{0}", ex.Message);
}
finally
{
conn.Close();
}
Console.ReadLine();
}
}
}