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创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

MySQL数据库约束深入详解

《MySQL数据库约束深入详解》:本文主要介绍MySQL数据库约束,在MySQL数据库中,约束是用来限制进入表中的数据类型的一种技术,通过使用约束,可以确保数据的准确性、完整性和可靠性,需要的朋友... 目录一、数据库约束的概念二、约束类型三、NOT NULL 非空约束四、DEFAULT 默认值约束五、UN

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib