Enterprise Library 2.0 Hands On Lab 翻译(2):数据访问程序块(二)
练习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两个程序集的引用,可以参考练习一。
2.在窗体中加入如下私有域,后面将会在多个地方用到该数据库实例。
3.在MainForm_Load方法中加入如下代码
{
this.cmbCategory.Items.Clear();
// TODO: Use a DataReader to retrieve Categories
using (IDataReader dataReader = _db.ExecuteReader("GetCategories"))
{
// Processing code
while (dataReader.Read())
{
Category item = new Category(
dataReader.GetInt32(0),
dataReader.GetString(1),
dataReader.GetString(2));
this.cmbCategory.Items.Add(item);
}
}
if (this.cmbCategory.Items.Count > 0)
this.cmbCategory.SelectedIndex = 0;
}
重载的方法Database.ExecuteReader,有一个字符串类型的参数,通过它来指定存储过程的名称,在这里我们不用做任何数据库连接方面的管理,但是在DataReader使用完毕后释放很重要,这些都会由上面的代码来完成,当DataReader释放后,数据库连接也将被关闭。
4.在cmbCategory_SelectedIndexChanged方法中加入如下代码,它将根据我们选择的类别来读取对应的Product的集合。
{
this.dsProducts.Clear();
Category selectedCategory = (Category)this.cmbCategory.SelectedItem;
if (selectedCategory == null)
return;
// TODO: Retrieve Products by Category
_db.LoadDataSet(
"GetProductsByCategory",
this.dsProducts,
new string[] { "Products" },
selectedCategory.CategoryId);
}
在数据访问应用程序块中Database类提供了两个关于DataSet的方法ExecuteDataSet和LoadDataSet。ExecuteDataSet返回一个新的DataSet而LoadDataSet则返回一个已经存在的DataSet。
第五步 实现数据的更新
在btnSave_Click方法中加入如下代码,将会把所有的任何改动更新到数据库中去。
{
// 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 翻译(2):数据访问程序块(二)相关推荐
- Enterprise Library 2.0 Hands On Lab 翻译(1):数据访问程序块(一)
练习一:使用数据访问程序块执行静态SQL语句<?XML:NAMESPACE PREFIX = O /> 该练习示范了如何使用数据访问程序块进行最基本的数据访问,另外还有如何去配制程序块,提 ...
- Enterprise Library 2.0 Hands On Lab 翻译(3):数据访问程序块(三)
练习3:加密数据库连接信息 通过该练习,你将学会如何去加密数据库连接信息. 第一步 打开DataEx3.sln项目,默认的安装路径应该为C:\Program Files\Microsoft Enter ...
- Enterprise Library 2.0 Hands On Lab 翻译(12):安全应用程序块(一)
练习1:应用程序安全性<?XML:NAMESPACE PREFIX = O /> 通过该练习将在一个已经存在的应用程序中添加认证和基于角色的授权. 第一步 打BugSmak.sln项目,默 ...
- Enterprise Library 2.0 Hands On Lab 翻译(14):加密应用程序块(一)
练习1:加解密字符串 通过本练习将学习通过加密来保护信息,在这里创建一个类似于IM的聊天应用程序,加密通信过程中的信息. 第一步 打BugSmak.sln项目,默认的安装路径应该为C:\Program ...
- Enterprise Library 5.0发布
Microsoft Enterprise Library 5.0是一套可重用的应用程序块,帮助开发人员进行企业应用开发.包括:Caching Block.Cryptography Block.Data ...
- Microsoft Enterprise Library 简介与请大家下载Microsoft Enterprise Library 5.0体验微软最新技术应用于企业信息平台
什么是Enterprise Library Enterprise Library是一组应用程序块(Application Block)的集合.他们是可重用的软件组件,被设计用来帮助开发者面对常 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- Enterprise Library 2.0 技巧(3):记录ASP.NET站点中未处理的异常
这篇文章不能算是Enterprise Library 2.0的一个技巧,只是Logging Application Block的一个简单应用而已,在这里我们使用Logging Application ...
- Microsoft Enterprise Library 4.0 for Visual Studio 2008
Microsoft Enterprise Library 4.0 for Visual Studio 2008已经发布,可以下载看看 转载于:https://www.cnblogs.com/rover ...
- Enterprise Library 3.0 体验(3):使用配置文件的Validation Application Block
摘要:Enterprise Library 3.0 January 2007 CTP版发布了,这次发布的版对于Validation Application Block有了很大的改进,包括对配置工具的支 ...
最新文章
- 技术大佬们都推荐的vim学习指南来了,值得收藏!
- access mysql 同步_使ACCESS数据库保持同步
- 编程理论:多态,继承,和开闭原则
- 动态加载laydate 失效_Java对象的内存布局+反射的原理+动态代理+ 并发和锁+文末彩蛋...
- HDU - 4461 The Power of Xiangqi
- Ext.MessageBox.Show使用Progress
- 通俗地解释脏读、不可重复读、幻读
- windows文件路径大于MAX_PATH
- Mac 运行goland出现can‘t load package: package xxxx is not in GOROOT问题排查
- 查看CUDA和cuDNN的版本号
- python 协程 多线程_python进阶之多线程(简单介绍协程)
- Zabbix监控Oracle 连接数
- MS sql server 基础知识回顾(二)-表连接和子查询
- DR模式 + keepalived
- Spring 框架学习—控制反转(IOC)
- DMCTF writeup
- GIS开发:QGIS编辑矢量数据
- 经常说的ROI是什么,怎么计算
- 网络请求未知错误 CLEARTEXT communication to XX not permitted by network security policy 解决方案
- The Old Man and The Sea
热门文章
- 【NLP】揭秘马尔可夫模型神秘面纱系列文章(四)
- hadoop2.2完全分布式最新高可靠安装文档
- 【PRML 学习笔记】第二章 - 概率分布 (Probability Distributions)
- 【动态主席树】ZOJ 2112【树状数组+主席树】
- mysql卸载报错1606_MySQL5卸载及重装出错问题记录
- 深入 Composer autoload
- django xadmin 安装和使用
- 需求文档2_The Battle of Polytopia
- Arcgis Android - HelloWorld
- [DELPHI] 使用mod函数换行