EnterpriseLibrary数据访问 使用存储过程访问数据库
2011-08-09 03:47
465 查看
演示代码下载: http://dev.mjxy.cn/a-entlib-Access-the-database-using-stored-procedures.aspx
使用存储过程访问数据库
1.配置文件
view sourceprint?
2.程序代码
view sourceprint?
使用存储过程访问数据库
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> |
view sourceprint?
01 | // TODO: Use Enterprise Library Data Block |
02 | using Microsoft.Practices.EnterpriseLibrary.Data; |
03 | using 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数据访问(2)使用存储过程访问数据库
- C#使用存储过程访问数据库
- 使用PL/SQL编写存储过程访问数据库
- 三级次数据(一):在数据库使用存储过程输出为自联表形式
- ADO.NET使用存储过程访问数据库
- 将数据放入到session域中,还是访问的时候一直使用request进行访问数据库
- SQL Server 2005 导出包含(insert into)数据的SQL脚本 (使用存储过程) 分类: 数据库
- EnterpriseLibrary数据访问(1)使用SQL直接访问数据库
- Mybatis使用存储过程访问数据库
- 【转】数据库基本知识:(十)数据操作 · 查 · (三)使用子查询访问和修改数据
- [转]SQLSERVER存储过程调用不同数据库的数据_存储过程中通过链接服务器访问远程服务器
- 在企业中使用的Silverlight:RAD数据通信用户友好的数据库访问
- 使用Enterprise Library 3.1 中的数据访问应用程序块数据库配置说明
- SQL 教程数据库包括:Oracle, Sybase, SQL Server, DB2, Access 等等,您将学到如何使用 SQL 访问和处理数据系统中的数据
- oracle_fdw的使用:从PostgreSQL中访问Oracle数据库,实现数据库数据的同步
- C#使用存储过程访问数据库
- .NET数据库编程求索之路--6.使用ADO.NET实现(三层架构篇-使用List传递数据-基于存储过程)(1)
- 使用存储过程访问DBF数据库文件
- SQL Server 2008还原数据时报错“因为数据库正在使用,所以无法获得对数据库的独占访问权”的解决方法
- SpringBoot开发详解(十) -- 使用JPA访问数据库下篇及使用Page进行数据分页