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

相关文章

Centos7 firewall和docker冲突问题及解决过程

《Centos7firewall和docker冲突问题及解决过程》本文描述了一个在CentOS7上使用firewalld和Docker容器的问题,当firewalld启动或重启时,会从iptable... 目录系统环境问题描述问题排查解决办法总结本文只是我对问题的记录,只能用作参考,不能China编程说明问题,请

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

Go语言结构体标签(Tag)的使用小结

《Go语言结构体标签(Tag)的使用小结》结构体标签Tag是Go语言中附加在结构体字段后的元数据字符串,用于提供额外的属性信息,这些信息可以通过反射在运行时读取和解析,下面就来详细的介绍一下Tag的使... 目录什么是结构体标签?基本语法常见的标签用途1.jsON 序列化/反序列化(最常用)2.数据库操作(

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺

Python在二进制文件中进行数据搜索的实战指南

《Python在二进制文件中进行数据搜索的实战指南》在二进制文件中搜索特定数据是编程中常见的任务,尤其在日志分析、程序调试和二进制数据处理中尤为重要,下面我们就来看看如何使用Python实现这一功能吧... 目录简介1. 二进制文件搜索概述2. python二进制模式文件读取(rb)2.1 二进制模式与文本

C#中checked关键字的使用小结

《C#中checked关键字的使用小结》本文主要介绍了C#中checked关键字的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录✅ 为什么需要checked? 问题:整数溢出是“静默China编程”的(默认)checked的三种用

SpringBoot全局异常拦截与自定义错误页面实现过程解读

《SpringBoot全局异常拦截与自定义错误页面实现过程解读》本文介绍了SpringBoot中全局异常拦截与自定义错误页面的实现方法,包括异常的分类、SpringBoot默认异常处理机制、全局异常拦... 目录一、引言二、Spring Boot异常处理基础2.1 异常的分类2.2 Spring Boot默

C#中预处理器指令的使用小结

《C#中预处理器指令的使用小结》本文主要介绍了C#中预处理器指令的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 第 1 名:#if/#else/#elif/#endif✅用途:条件编译(绝对最常用!) 典型场景: 示例

SpringBoo WebFlux+MongoDB实现非阻塞API过程

《SpringBooWebFlux+MongoDB实现非阻塞API过程》本文介绍了如何使用SpringBootWebFlux和MongoDB实现非阻塞API,通过响应式编程提高系统的吞吐量和响应性能... 目录一、引言二、响应式编程基础2.1 响应式编程概念2.2 响应式编程的优势2.3 响应式编程相关技术