Enterprise Library 2.0 Hands On Lab 翻译(2):数据访问程序块(二)
2006-10-06 12:17
603 查看
练习2:存储过程和使用程序块更新数据
该练习将示范如何用数据访问应用程序调用存储过程,并使用强类型的DataSet来更新数据。
第一步
打开DataEx2.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex02\begin,并编译。
第二步 在QuickStarts数据库中添加Categories数据表
运行批处理文件SetUpEx02.bat,它默认的路径安装路径为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex02\DbSetup,默认的服务器实例为(local)\SQLEXPRESS,如果需要修改,用记事本打开SetUpEx02.bat,修改为自己的德数据库服务器实例。执行后将会在数据库中创建Categories数据表和存储过程GetCategories,并会在表中插入一些数据。
第三步 回顾应用程序
在解决方案管理器中,选中MainForm.cs文件,选择 View | Designer 菜单,应用程序主要是选择一个特定的Category,它将会加载该类别下的所有产品,允许我们作一些修改并保存。
第四步 实现数据的读取
1.在解决方案管理器中选择MainForm.cs,选择View | Code 菜单命令,在代码中添加如下命名空间,在这之前请先添加对Data和Common两个程序集的引用,可以参考练习一。
using Microsoft.Practices.EnterpriseLibrary.Data;
2.在窗体中加入如下私有域,后面将会在多个地方用到该数据库实例。
private Database _db = DatabaseFactory.CreateDatabase("QuickStarts Instance");
3.在MainForm_Load方法中加入如下代码
private void MainForm_Load(object sender, System.EventArgs e)
private void cmbCategory_SelectedIndexChanged(object sender, System.EventArgs e)
private void btnSave_Click(object sender, System.EventArgs e)
{
// TODO: Use the DataSet to update the Database
System.Data.Common.DbCommand insertCommand = null;
insertCommand = _db.GetStoredProcCommand("HOLAddProduct");
_db.AddInParameter(insertCommand, "ProductName",
DbType.String, "ProductName", DataRowVersion.Current);
_db.AddInParameter(insertCommand, "CategoryID",
DbType.Int32, "CategoryID", DataRowVersion.Current);
_db.AddInParameter(insertCommand, "UnitPrice",
DbType.Currency, "UnitPrice", DataRowVersion.Current);
System.Data.Common.DbCommand deleteCommand = null;
deleteCommand = _db.GetStoredProcCommand("HOLDeleteProduct");
_db.AddInParameter(deleteCommand, "ProductID",
DbType.Int32, "ProductID", DataRowVersion.Current);
_db.AddInParameter(deleteCommand, "LastUpdate",
DbType.DateTime, "LastUpdate", DataRowVersion.Original);
System.Data.Common.DbCommand updateCommand = null;
updateCommand = _db.GetStoredProcCommand("HOLUpdateProduct");
_db.AddInParameter(updateCommand, "ProductID",
DbType.Int32, "ProductID", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "ProductName",
DbType.String, "ProductName", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "CategoryID",
DbType.Int32, "CategoryID", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "UnitPrice",
DbType.Currency, "UnitPrice", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "LastUpdate",
DbType.DateTime, "LastUpdate", DataRowVersion.Current);
int rowsAffected = _db.UpdateDataSet(
this.dsProducts,
"Products",
insertCommand,
updateCommand,
deleteCommand,
UpdateBehavior.Standard);
}
在更新一个数据库时,需要对DataTable的列和存储过程中的参数之间作一个映射,重载的方法UpdateDataSet,它通过数据访问程序块自动执行更新事务,在这里UpdateBehaviour是可以设置的,它有三种类型:Transactional,Continue,Standard。
第六步 运行应用程序
选择Debug | Start Without Debugging菜单命令并运行应用程序,在Category下拉框中选择一个类别,观察如何加载和保存数据。
更多Enterprise Library的文章请参考《Enterprise Library系列文章》
该练习将示范如何用数据访问应用程序调用存储过程,并使用强类型的DataSet来更新数据。
第一步
打开DataEx2.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex02\begin,并编译。
第二步 在QuickStarts数据库中添加Categories数据表
运行批处理文件SetUpEx02.bat,它默认的路径安装路径为C:\Program Files\Microsoft Enterprise Library January 2006\labs\cs\Data Access\exercises\ex02\DbSetup,默认的服务器实例为(local)\SQLEXPRESS,如果需要修改,用记事本打开SetUpEx02.bat,修改为自己的德数据库服务器实例。执行后将会在数据库中创建Categories数据表和存储过程GetCategories,并会在表中插入一些数据。
第三步 回顾应用程序
在解决方案管理器中,选中MainForm.cs文件,选择 View | Designer 菜单,应用程序主要是选择一个特定的Category,它将会加载该类别下的所有产品,允许我们作一些修改并保存。
第四步 实现数据的读取
1.在解决方案管理器中选择MainForm.cs,选择View | Code 菜单命令,在代码中添加如下命名空间,在这之前请先添加对Data和Common两个程序集的引用,可以参考练习一。
using Microsoft.Practices.EnterpriseLibrary.Data;
2.在窗体中加入如下私有域,后面将会在多个地方用到该数据库实例。
private Database _db = DatabaseFactory.CreateDatabase("QuickStarts Instance");
3.在MainForm_Load方法中加入如下代码
private void MainForm_Load(object sender, System.EventArgs e)
private void cmbCategory_SelectedIndexChanged(object sender, System.EventArgs e)
private void btnSave_Click(object sender, System.EventArgs e)
{
// TODO: Use the DataSet to update the Database
System.Data.Common.DbCommand insertCommand = null;
insertCommand = _db.GetStoredProcCommand("HOLAddProduct");
_db.AddInParameter(insertCommand, "ProductName",
DbType.String, "ProductName", DataRowVersion.Current);
_db.AddInParameter(insertCommand, "CategoryID",
DbType.Int32, "CategoryID", DataRowVersion.Current);
_db.AddInParameter(insertCommand, "UnitPrice",
DbType.Currency, "UnitPrice", DataRowVersion.Current);
System.Data.Common.DbCommand deleteCommand = null;
deleteCommand = _db.GetStoredProcCommand("HOLDeleteProduct");
_db.AddInParameter(deleteCommand, "ProductID",
DbType.Int32, "ProductID", DataRowVersion.Current);
_db.AddInParameter(deleteCommand, "LastUpdate",
DbType.DateTime, "LastUpdate", DataRowVersion.Original);
System.Data.Common.DbCommand updateCommand = null;
updateCommand = _db.GetStoredProcCommand("HOLUpdateProduct");
_db.AddInParameter(updateCommand, "ProductID",
DbType.Int32, "ProductID", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "ProductName",
DbType.String, "ProductName", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "CategoryID",
DbType.Int32, "CategoryID", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "UnitPrice",
DbType.Currency, "UnitPrice", DataRowVersion.Current);
_db.AddInParameter(updateCommand, "LastUpdate",
DbType.DateTime, "LastUpdate", DataRowVersion.Current);
int rowsAffected = _db.UpdateDataSet(
this.dsProducts,
"Products",
insertCommand,
updateCommand,
deleteCommand,
UpdateBehavior.Standard);
}
在更新一个数据库时,需要对DataTable的列和存储过程中的参数之间作一个映射,重载的方法UpdateDataSet,它通过数据访问程序块自动执行更新事务,在这里UpdateBehaviour是可以设置的,它有三种类型:Transactional,Continue,Standard。
第六步 运行应用程序
选择Debug | Start Without Debugging菜单命令并运行应用程序,在Category下拉框中选择一个类别,观察如何加载和保存数据。
更多Enterprise Library的文章请参考《Enterprise Library系列文章》
相关文章推荐
- Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(2):数据访问程序块(二)
- Enterprise Library 2.0 Hands On Lab 翻译(2):数据访问程序块(二)
- Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
- Enterprise Library 2.0 Hands On Lab 翻译(2):数据访问程序块(二)
- Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
- Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
- Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
- Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
- Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
- Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(13):安全应用程序块(二)
- Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
- Enterprise Library 5.0 Hands On Lab(1):数据访问程序块(一)
- Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)