image

一,下载并安装好Enterprise Library 4.1

二,新建一个Web应用程序

三,右键点击Web.Config 文件 使用 Edit Enterprise Library Configuration 可以编辑Web.Config,可以看到数据访问程序块是默认存在的。

image

可以新建连接字符串,可以新建指定更多的

image

我建立了两个字符串

image

image

和两个Custom Provider  分别是SQL 和Oracle

image

可以设置加密和默认连接字符串

 

四,添加引用(注意:这里有可能你也会出现和我一样的问题,就是编译出错,所以我引用的组件位置是官方示例程序的两个组件,程序编辑才通过)

image 

编译通过但运行还是不行,就打开Web.Config 删除报错位置的“, PublicKeyToken=31bf3856ad364e35” 成为下面这样,不报错了

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral" />

 

五,编写代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;

namespace DataAccessBlock
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //查询数据
            Database db = DatabaseFactory.CreateDatabase();
            this.GridView1.DataSource = db.ExecuteReader(CommandType.Text, "SELECT * FROM Projects");
            this.GridView1.DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //调用存储过程(直接传参数的简单调用)
            Database db = DatabaseFactory.CreateDatabase("Conn2");
            this.GridView1.DataSource = db.ExecuteReader("GetUser", "admin");
            this.GridView1.DataBind();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Database db = DatabaseFactory.CreateDatabase("Conn1");

            //带输出和返回参数的存储过程调用
            DbCommand cmd = db.GetStoredProcCommand("addRole");
            db.AddInParameter(cmd, "Id", DbType.Int32, DateTime.Now.Millisecond);
            db.AddInParameter(cmd, "Name", DbType.String, "技术总监");
            db.AddOutParameter(cmd, "newLastChanged", DbType.Int16, 4);
            db.AddParameter(cmd, "RecordCount", DbType.Int16, ParameterDirection.ReturnValue, "", DataRowVersion.Default, 0);
            db.ExecuteNonQuery(cmd);
            Response.Write("输出参数值:" + db.GetParameterValue(cmd, "newLastChanged").ToString());
            Response.Write("<br />返回参数值:" + db.GetParameterValue(cmd, "RecordCount").ToString());

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
             //使用事务
            Database db = DatabaseFactory.CreateDatabase("Conn1");
            using (IDbConnection conn = db.CreateConnection())
            {
                conn.Open();
                IDbTransaction _trans = conn.BeginTransaction();
                try
                {
                    DbCommand _cmd = db.GetSqlStringCommand("Insert Into Roles(Id,Name) values(@Id,@Name)");
                    db.AddInParameter(_cmd, "Id", DbType.Int32, 45);
                    db.AddInParameter(_cmd, "Name", DbType.String, "UI设计");
                    db.ExecuteNonQuery(_cmd, _trans as DbTransaction);
                    db.ExecuteNonQuery(_cmd, _trans as DbTransaction);//字段上建有唯一索引,故第二次插入同样记录时会报错
                    _trans.Commit();
                }
                catch
                {
                    try
                    {
                        _trans.Rollback();//事务提交失败时,则回滚(是否回滚成功,可查看表中有无AA的记录即可)
                    }
                    catch { }
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
}

 

 

 

 

六,添加生成事件脚本,复制Config,没有Config会报错

copy "$(ProjectDir)\*.config" "$(TargetDir)"

image
示例源码下载:EL41Sample.rar
Enterprise Library 4.1 目录:Enterprise Library 4.1 快速使用图文笔记 目录

posted on 2009-10-04 17:30  冯瑞涛  阅读(2056)  评论(7编辑  收藏  举报