EnterpriseLibrary数据访问 使用存储过程访问数据库

2023-10-19 08:58

本文主要是介绍EnterpriseLibrary数据访问 使用存储过程访问数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

演示代码下载: http://dev.mjxy.cn/a-entlib-Access-the-database-using-stored-procedures.aspx

使用存储过程访问数据库

 

1.配置文件

view sourceprint?
01<configuration>
02  <configSections>
03    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
04  </configSections>
05  <dataConfiguration defaultDatabase="QuickStarts Instance" />
06  <connectionStrings>
07    <add name="QuickStarts Instance" connectionString="Database=EntLibQuickStarts;Server=(local);Integrated Security=SSPI;"
08      providerName="System.Data.SqlClient" />
09  </connectionStrings>
10</configuration>

2.程序代码

view sourceprint?
01// TODO: Use Enterprise Library Data Block
02using Microsoft.Practices.EnterpriseLibrary.Data;
03using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
04   
05     // TODO: Create private field for Database
06        private Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("QuickStarts Instance");
07   
08        private void MainForm_Load(object sender, System.EventArgs e)
09        {
10            this.cmbCategory.Items.Clear();
11   
12            // TODO: Use a DataReader to retrieve Categories
13            using (IDataReader rd = _db.ExecuteReader("GetCategories"))
14            {
15                while (rd.Read())
16                {
17                    Category item = new Category(
18                        rd.GetInt32(0),
19                        rd.GetString(1),
20                        rd.GetString(2)
21                        );
22                    this.cmbCategory.Items.Add(item);
23                }
24            }
25   
26            if (this.cmbCategory.Items.Count > 0)
27                this.cmbCategory.SelectedIndex = 0;
28        }
29   
30        private void cmbCategory_SelectedIndexChanged(object sender, System.EventArgs e)
31        {
32            this.dsProducts.Clear();
33   
34            Category selectedCategory = (Category) this.cmbCategory.SelectedItem;
35            if (selectedCategory == null)
36                return;
37   
38            // TODO: Retrieve Products by Category
39     //将存储过程返回的表填充到DataSet 使用表名products,selectedCategory.CategoryID是参数
40            _db.LoadDataSet("GetProductsByCategory",
41                this.dsProducts, new string[] { "products" },
42                selectedCategory.CategoryId);
43   
44        }
45   
46        private void btnSave_Click(object sender, System.EventArgs e)
47        {
48            // TODO: Use the DataSet to update the Database 
49            System.Data.Common.DbCommand insertCommand = null;
50            insertCommand = _db.GetStoredProcCommand("HOLAddProduct");
51            _db.AddInParameter(insertCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current);
52            _db.AddInParameter(insertCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
53            _db.AddInParameter(insertCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current); 
54              
55            System.Data.Common.DbCommand deleteCommand = null
56            deleteCommand = _db.GetStoredProcCommand("HOLDeleteProduct"); 
57            _db.AddInParameter(deleteCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current); 
58            _db.AddInParameter(deleteCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Original); 
59              
60            System.Data.Common.DbCommand updateCommand = null; updateCommand = _db.GetStoredProcCommand("HOLUpdateProduct"); 
61            _db.AddInParameter(updateCommand, "ProductID", DbType.Int32, "ProductID", DataRowVersion.Current); 
62            _db.AddInParameter(updateCommand, "ProductName", DbType.String, "ProductName", DataRowVersion.Current); 
63            _db.AddInParameter(updateCommand, "CategoryID", DbType.Int32, "CategoryID", DataRowVersion.Current);
64            _db.AddInParameter(updateCommand, "UnitPrice", DbType.Currency, "UnitPrice", DataRowVersion.Current);
65            _db.AddInParameter(updateCommand, "LastUpdate", DbType.DateTime, "LastUpdate", DataRowVersion.Current); 
66              
67            int rowsAffected = _db.UpdateDataSet(this.dsProducts, "Products"
68                insertCommand, updateCommand, deleteCommand, UpdateBehavior.Standard);
69   
70              
71        }

这篇关于EnterpriseLibrary数据访问 使用存储过程访问数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/238751

相关文章

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像

使用Java发送邮件到QQ邮箱的完整指南

《使用Java发送邮件到QQ邮箱的完整指南》在现代软件开发中,邮件发送功能是一个常见的需求,无论是用户注册验证、密码重置,还是系统通知,邮件都是一种重要的通信方式,本文将详细介绍如何使用Java编写程... 目录引言1. 准备工作1.1 获取QQ邮箱的SMTP授权码1.2 添加JavaMail依赖2. 实现

MyBatis与其使用方法示例详解

《MyBatis与其使用方法示例详解》MyBatis是一个支持自定义SQL的持久层框架,通过XML文件实现SQL配置和数据映射,简化了JDBC代码的编写,本文给大家介绍MyBatis与其使用方法讲解,... 目录ORM缺优分析MyBATisMyBatis的工作流程MyBatis的基本使用环境准备MyBati

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

Spring AI集成DeepSeek三步搞定Java智能应用的详细过程

《SpringAI集成DeepSeek三步搞定Java智能应用的详细过程》本文介绍了如何使用SpringAI集成DeepSeek,一个国内顶尖的多模态大模型,SpringAI提供了一套统一的接口,简... 目录DeepSeek 介绍Spring AI 是什么?Spring AI 的主要功能包括1、环境准备2

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

Java之并行流(Parallel Stream)使用详解

《Java之并行流(ParallelStream)使用详解》Java并行流(ParallelStream)通过多线程并行处理集合数据,利用Fork/Join框架加速计算,适用于大规模数据集和计算密集... 目录Java并行流(Parallel Stream)1. 核心概念与原理2. 创建并行流的方式3. 适

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Golang基于内存的键值存储缓存库go-cache

《Golang基于内存的键值存储缓存库go-cache》go-cache是一个内存中的key:valuestore/cache库,适用于单机应用程序,本文主要介绍了Golang基于内存的键值存储缓存库... 目录文档安装方法示例1示例2使用注意点优点缺点go-cache 和 Redis 缓存对比1)功能特性