using System;
using System.Data;
using System.Data.SqlClient;
//DataView的MSDN参考网站:http://msdn.microsoft.com/en-us/library/8sd1cd0a.aspx
namespace Chapter13
{
class DataViews
{
static void Main(string[] args)
{
// connection string
string connString = @"
server = .;
integrated security = true;
database = northwind
";
// query
string sql = @"
select
contactname,
country
from
customers
";
// create connection
SqlConnection conn = null;
try
{
conn = new SqlConnection(connString);
// Create data adapter
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql, conn);
// create and fill dataset
DataSet ds = new DataSet();
da.Fill(ds, "customers");
// get data table reference
DataTable dt = ds.Tables["customers"];
// create data view
DataView dv = new DataView(
dt,
"country = 'Germany'",
"country",
DataViewRowState.CurrentRows
);
// display data from data view
foreach (DataRowView drv in dv)
{
for (int i = 0; i < dv.Table.Columns.Count; i++)
Console.Write(drv[i] + "\t");
Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine("Error: " + e);
}
finally
{
// close connection
conn.Close();
}
}
}
}
//所有代码来自书籍《Begining C# Databases From Novice to Professional》