本演练演示如何使用DataSet更新数据库。它假定以下存储过程已在被引用的数据库中被创建。
CREATE PROCEDURE AddProduct
(
    @ProductName nvarchar(50),
    @CategoryID int,
    @UnitPrice money
)
AS
INSERT INTO Products (ProductName, CategoryID, UnitPrice)
VALUES (@ProductName, @CategoryID, @UnitPrice)

SELECT ProductID, ProductName, CategoryID, UnitPrice
FROM Products
WHERE ProductID = SCOPE_IDENTITY()
GO

CREATE PROCEDURE DeleteProduct
(
    @ProductID int
)
AS
DELETE Products
WHERE ProductID = @ProductID
GO

CREATE PROCEDURE UpdateProduct
(
    @ProductID int,
    @ProductName nvarchar(50),
    @LastUpdate datetime
)
AS
UPDATE Products
SET ProductName = @ProductName
WHERE ProductID = @ProductID AND LastUpdate = @LastUpdate

IF @@ROWCOUNT > 0
  -- This statement is used to update the DataSet if changes are done
  -- on the updated record (identities, timestamps or triggers )
  SELECT ProductID, ProductName, CategoryID, UnitPrice
  FROM Products
  WHERE ProductID = @ProductID
GO

1、配置数据库。必要的步骤,请参阅数据访问快速入门。
2、创建Database(当你不使用统一的集成方法)。下面的代码使用工厂创建一个默认的配置数据库对象。

Database db = DatabaseFactory.CreateDatabase();

Dim db As Database = DatabaseFactory.CreateDatabase()

3、检索添加以下代码初始数据集。

DataSet productsDataSet = new DataSet(); string sqlCommand = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate From Products"; DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand); string productsTable = "Products"; // Retrieve the initial data. db.LoadDataSet(dbCommand, productsDataSet, productsTable);

Dim productsDataSet As DataSet = New DataSet Dim sqlCommand As String = "Select ProductID, ProductName, CategoryID, UnitPrice, LastUpdate From Products" Dim dbCommand As DbCommand = db.GetSqlStringCommand(sqlCommand) Dim productsTable As String = "Products" ' Retrieve the initial data. db.LoadDataSet(dbCommand, productsDataSet, productsTable)

4、修改通过添加下面的代码集。

// Get the table that will be modified. DataTable table = productsDataSet.Tables[productsTable]; // Add a new product to existing DataSet. DataRow addedRow = table.Rows.Add(new object[] {DBNull.Value, "New product", 11, 25}); // Modify an existing product. table.Rows[0]["ProductName"] = "Modified product";

' Get the table that will be modified. Dim table As DataTable = productsDataSet.Tables(productsTable) ' Add a new product to existing DataSet. Dim addedRow As DataRow = table.Rows.Add(New Object() {DBNull.Value, "New product", 11, 25}) ' Modify an existing product. table.Rows(0)("ProductName") = "Modified product"

5、以下代码显示了如何创建DbCommand对象,插入一个新的产品,删除产品,并更新DataSet中的数据。

DbCommand insertCommand = db.GetStoredProcCommand("AddProduct"); 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); DbCommand deleteCommand = db.GetStoredProcCommand("DeleteProduct"); db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current); DbCommand updateCommand = db.GetStoredProcCommand("UpdateProduct"); db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current); db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current); db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current);

Dim insertCommand As DbCommand = db.GetStoredProcCommand("AddProduct") 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) Dim deleteCommand As DbCommand = db.GetStoredProcCommand("DeleteProduct") db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current) Dim updateCommand As DbCommand = db.GetStoredProcCommand("UpdateProduct") db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current) db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current) db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current)

6、添加以下代码提交DataSet,显示如何使用DataSet中缓存本地数据集更新数据库。

int rowsAffected = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand, deleteCommand, Microsoft.Practices.EnterpriseLibrary.DataAccess.UpdateBehavior.Standard);

Dim rowsAffected As Integer = db.UpdateDataSet(productsDataSet, "Products", insertCommand, updateCommand, _ deleteCommand, Microsoft.Practices.EnterpriseLibrary.DataAccess.UpdateBehavior.Standard)

文章由唐勇(http://www.tangyong.net/)翻译自微软Enterprise Library系列英文文档,翻译得辛苦,转载请保留,谢谢!

Enterprise Library 4.1数据访问应用程序块快速入门【6】使用DataSet更新数据库相关推荐

  1. Enterprise Library 4 数据访问应用程序块

    Enterprise Library 数据访问应用程序块简化了实现常规数据访问功能的开发任务.应用程序可以在各种场景中使用此应用程序块,例如为显示而读取数据.传递数据穿过应用程序层( applicat ...

  2. Enterprise Library 缓存应用程序块快速入门

    Enterprise Library 快速入门是简单的.易于理解的应用程序块关键特性的示例,使用了一个实现了常规场景的漫游集合来说明这些特性. 如果要理解一个应用程序块,快速入门将是理想的起始点,并且 ...

  3. Hadoop教程(一):简介、大数据解决方案、介绍快速入门

    Hadoop是一个开源框架,它允许在整个集群使用简单编程模型计算机的分布式环境存储并处理大数据.它的目的是从单一的服务器到上千台机器的扩展,每一个台机都可以提供本地计算和存储. "90%的世 ...

  4. docker -v 文件夹下没有数据_详细!快速入门指南!Docker

    什 么 是 容 器? 容器只是实现隔离的一种方法.与虚拟机不同,它们不是通过模拟硬件来实现隔离,而是通过使用现有的Linux内核功能来实现隔离.在典型的Unix/Linux操作系统中,所有进程都共享相 ...

  5. 数据湖构建DLF快速入门 实验笔记

    一.登录阿里云账号,点击进入DLF控制界面 DLF控制台⻚⾯https://dlf.console.aliyun.com/cn-hangzhou/home?spm=a2c6h.13858378.0.0 ...

  6. 大屏数据可视化源码_数据可视化大屏快速入门

    O 数据可视化的好处 重要的见解往往隐藏在数据之中,它们有助于推动业务发展.但问题在于,只是凭借原始数据,无法总是洞悉真相.当看到数据以可视化形式呈现时,格局.关联和其他会心时刻便浮现出来,而单纯查看 ...

  7. python可视化库matplotlib_环境搭建 | Python 数据可视化库 Matplotlib 快速入门之一-阿里云开发者社区...

    数据挖掘基础环境安装与使用 [学习目标] 完成数据挖掘基础阶段的所有环境安装 应用jupyter notebook完成代码编写运行 库的安装 学习目标 目标 搭建好数据挖掘基础阶段的环境 应用 无 整 ...

  8. 大数据技术之Hadoop(快速入门)

    目录 第一章 Hadoop概述 1.1 什么是Hadoop 1.2  Hadoop 产生背景 Hadoop之父:Doug cutting 1.3  Hadoop 三大发行版本 Hadoop 三大发行版 ...

  9. python数据可视化神器--pyecharts 快速入门

    前言 本文首发于本人公众号[Python编程与实战] 我们都知道python上的一款可视化工具 matplotlib , 但是它是静态的.后来发现了 pyecharts 模块,简直好用到不行,可视化类 ...

最新文章

  1. 安装wdcp的方法和bug
  2. 云从科技完成B+轮超10亿元融资,多个国家基金进入
  3. 漫画:如何给女朋友解释什么是删库跑路?
  4. 游戏开发--开源软件7--xith3D(java 3D引擎)
  5. 【烙铁使用规范】—— 烙铁使用、温度测量规范
  6. optee的Share Memory介绍
  7. NumPy 基础知识·翻译完成
  8. Mac录制屏幕:自带QuickTime软件和QQ
  9. C++之文件操作探究(一):写文件——文本文件
  10. 获取 HttpServletRequest 所有参数,获取所有Httpsession中参数
  11. Xposed框架分析
  12. 【hacker的错误集】IndentationError: expected an indented block
  13. VMware清理vmdk文件
  14. 数据降维:主成分分析法(PCA)
  15. 解决gns3连接不上本地的几种情况
  16. 三菱FX5U添加新模块
  17. 高速串行计算机扩展总线标准,高速串行计算机扩展总线标准Bosch Sensortec开发出BMP384...
  18. 计算机塑性成形论文,6061铝合金的高温变形的力学性能及热塑性成形工艺研究...
  19. 计算机毕业设计java_java酒店客房入住管理系统
  20. 数商云分析:“百亿市场”大幅缩水 医疗器械产业未来如何布局

热门文章

  1. NOI2016 day1 总(xia)结(che)
  2. Windows注入与拦截(3) -- 使用钩子方式完成DLL注入
  3. HTML 制作简单的导航栏
  4. ([转载]魔百和CM201-2 M8375版安装当贝桌面方法)
  5. 如何下载NBA球员投篮信息
  6. vc 坐标系统与影射模式
  7. Altium Designer如何实现圆弧布线
  8. 嵌入式还有哪些风口值得入?
  9. mybatis里的when和otherwise用法
  10. Lucene搜索理论