使用 Enterprise library 2.0 DAAB
最近在学习Enterprise Library,按照 Enterprise
Library 2.0 Data Access Application Block By David Hayden的例子做了一个
另外还发现VS设计器设计好界面,先后修改InitialComponent中的代码,如果再进行界面的更改,IntialComponent中的会自动初始化,也就是不保留更改部分的代码。
所以在改好DataGridView的AutoGenerateColumns 为true,又进行界面设计会导致AutoGenerateColumns为false
下面是我的关键代码
DataSet GetOrdersByCustomerId(string customerId) 和 DataTable GetCategories()
界面处理
源文件
另外还发现VS设计器设计好界面,先后修改InitialComponent中的代码,如果再进行界面的更改,IntialComponent中的会自动初始化,也就是不保留更改部分的代码。
所以在改好DataGridView的AutoGenerateColumns 为true,又进行界面设计会导致AutoGenerateColumns为false
下面是我的关键代码
DataSet GetOrdersByCustomerId(string customerId) 和 DataTable GetCategories()
public class SampleDataAccess
{
public static DataSet GetOrdersByCustomerId(string customerId)
{
// create database from default config
Database northwind = DatabaseFactory.CreateDatabase();
// get db command from northwind database
DbCommand cmd = northwind.GetSqlStringCommand("SELECT OrderId,OrderDate FROM Orders WHERE CustomerID = @CustomerID");
// add customer id paramter to db command
northwind.AddInParameter(cmd, "@CustomerID", DbType.String, customerId);
// order dataset
DataSet dsOrders = new DataSet();
// load order data set
northwind.LoadDataSet(cmd, dsOrders, "Orders");
return dsOrders;
}
public static DataTable GetCategories()
{
Database northwind = DatabaseFactory.CreateDatabase();
DbCommand command = northwind.GetSqlStringCommand("SELECT * FROM Categories");
DataTable categories = new DataTable();
using (IDataReader dr = northwind.ExecuteReader(command))
{
categories.Load(dr);
}
return categories;
}
}
{
public static DataSet GetOrdersByCustomerId(string customerId)
{
// create database from default config
Database northwind = DatabaseFactory.CreateDatabase();
// get db command from northwind database
DbCommand cmd = northwind.GetSqlStringCommand("SELECT OrderId,OrderDate FROM Orders WHERE CustomerID = @CustomerID");
// add customer id paramter to db command
northwind.AddInParameter(cmd, "@CustomerID", DbType.String, customerId);
// order dataset
DataSet dsOrders = new DataSet();
// load order data set
northwind.LoadDataSet(cmd, dsOrders, "Orders");
return dsOrders;
}
public static DataTable GetCategories()
{
Database northwind = DatabaseFactory.CreateDatabase();
DbCommand command = northwind.GetSqlStringCommand("SELECT * FROM Categories");
DataTable categories = new DataTable();
using (IDataReader dr = northwind.ExecuteReader(command))
{
categories.Load(dr);
}
return categories;
}
}
界面处理
public partial class GetOrdersByCustomerIdSample : Form
{
public GetOrdersByCustomerIdSample()
{
InitializeComponent();
// intialize data grid view for orders
InitializeDataGridView();
}
private void btnGetOrdersByCustomerId_Click(object sender, EventArgs e)
{
string customerId = GetCustomerId();
bindingSource1.DataSource = SampleDataAccess.GetOrdersByCustomerId(customerId);
dataGridView1.DataSource = bindingSource1;
}
private void InitializeDataGridView()
{
dataGridView1.AutoGenerateColumns = true;
}
private string GetCustomerId()
{
// retrieve customer id from form
string customerId = "VINET";
return customerId;
}
}
{
public GetOrdersByCustomerIdSample()
{
InitializeComponent();
// intialize data grid view for orders
InitializeDataGridView();
}
private void btnGetOrdersByCustomerId_Click(object sender, EventArgs e)
{
string customerId = GetCustomerId();
bindingSource1.DataSource = SampleDataAccess.GetOrdersByCustomerId(customerId);
dataGridView1.DataSource = bindingSource1;
}
private void InitializeDataGridView()
{
dataGridView1.AutoGenerateColumns = true;
}
private string GetCustomerId()
{
// retrieve customer id from form
string customerId = "VINET";
return customerId;
}
}
源文件