按照上一篇blog的步骤,配置好Enterpise library 的DAAB
<一>表示层添加书籍 WebForm1.aspx.cs 代码如下
<二>业务逻辑层BLL下的BookSystem.cs代码如下
<三>通用Common层下的Book.cs 代码如下
<四>数据访问层BookDA.cs代码如下
<五>存储过程addbook
CREATE procedure addbook
( @Name varchar(50),
@Author varchar(50),
@ISBN varchar(50),
@Description varchar(50),
@Subject varchar(50),
@ItemId INT = NULL OUTPUT )
as insert into books values(@Name,@Author,@ISBN,@Description,@Subject)
select @ItemId=@@Identity GO
<六>编译,运行出现错误提示: 安全性异常 说明: 应用程序试图执行安全策略不允许的操作。要授予此应用程序所需的权限,请与系统管理员联系,或在配置文件中更改该应用程序的信任级别。
异常详细信息: System.Security.SecurityException: 不允许所请求的注册表访问权。
解决方法:请运行你安装目录下的(我的安装目录是D:\Program Files\Microsoft Enterprise Library June 2005\src)InstallServices.bat 即可解决问题.
<一>表示层添加书籍 WebForm1.aspx.cs 代码如下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//
using BLL;
using Common;
namespace EnterPriseTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbBookNameAdd;
protected System.Web.UI.WebControls.TextBox tbAuthorAdd;
protected System.Web.UI.WebControls.TextBox tbISBNAdd;
protected System.Web.UI.WebControls.TextBox tbDescriptionAdd;
protected System.Web.UI.WebControls.TextBox tbSubjectAdd;
protected System.Web.UI.WebControls.Button btnSubmitAdd;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btnCancelAdd;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
private void btnSubmitAdd_Click(object sender, System.EventArgs e)
{
string strName=tbBookNameAdd.Text.Trim();
string strAuthor=tbAuthorAdd.Text.Trim();
string strISBN=tbISBNAdd.Text.Trim();
string strDescription=tbDescriptionAdd.Text.Trim();
string strSubject=tbSubjectAdd.Text.Trim();
//
Book book = new Book(strName,strAuthor,strISBN,strDescription,strSubject);//实例化Common下的类book
//调用业务逻辑层
//int iItemid = (new BookSystem()).AddBook(book);
//访问类BookSystem的静态方法AddBook()
String iTtemid=BookSystem.AddBook(book);
if(iTtemid!="")
{
Label1.Text="成功插入书籍,返回书号为"+iTtemid.ToString();
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//
using BLL;
using Common;
namespace EnterPriseTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tbBookNameAdd;
protected System.Web.UI.WebControls.TextBox tbAuthorAdd;
protected System.Web.UI.WebControls.TextBox tbISBNAdd;
protected System.Web.UI.WebControls.TextBox tbDescriptionAdd;
protected System.Web.UI.WebControls.TextBox tbSubjectAdd;
protected System.Web.UI.WebControls.Button btnSubmitAdd;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btnCancelAdd;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
Web 窗体设计器生成的代码
private void btnSubmitAdd_Click(object sender, System.EventArgs e)
{
string strName=tbBookNameAdd.Text.Trim();
string strAuthor=tbAuthorAdd.Text.Trim();
string strISBN=tbISBNAdd.Text.Trim();
string strDescription=tbDescriptionAdd.Text.Trim();
string strSubject=tbSubjectAdd.Text.Trim();
//
Book book = new Book(strName,strAuthor,strISBN,strDescription,strSubject);//实例化Common下的类book
//调用业务逻辑层
//int iItemid = (new BookSystem()).AddBook(book);
//访问类BookSystem的静态方法AddBook()
String iTtemid=BookSystem.AddBook(book);
if(iTtemid!="")
{
Label1.Text="成功插入书籍,返回书号为"+iTtemid.ToString();
}
}
}
}
<二>业务逻辑层BLL下的BookSystem.cs代码如下
using System;
using System.Data;
//
using Common;
using DAL;
namespace BLL
{
/// <summary>
/// BookSystem 的摘要说明。
/// </summary>
public class BookSystem
{
//实例化数据库访问层类BookDA
private static BookDA _books = new BookDA();
public BookSystem()
{
}
//静态方法
public static string AddBook(Book book)//使用Common层的类Book
{
string iRet ="";
try
{
iRet = _books.AddBook(book);
}
catch(Exception ex)
{
throw new ApplicationException(ex.ToString());
}
return iRet;
}
}
}
using System.Data;
//
using Common;
using DAL;
namespace BLL
{
/// <summary>
/// BookSystem 的摘要说明。
/// </summary>
public class BookSystem
{
//实例化数据库访问层类BookDA
private static BookDA _books = new BookDA();
public BookSystem()
{
}
//静态方法
public static string AddBook(Book book)//使用Common层的类Book
{
string iRet ="";
try
{
iRet = _books.AddBook(book);
}
catch(Exception ex)
{
throw new ApplicationException(ex.ToString());
}
return iRet;
}
}
}
<三>通用Common层下的Book.cs 代码如下
using System;
namespace Common
{
/// <summary>
/// BookData 的摘要说明。
/// </summary>
public class Book
{
public Book()
{
}
//有参数构造函数
public Book(string strName,string strAuthor,string strISBN,string strDescription,string strSubject)
{
_name = strName;
_description = strDescription;
_author = strAuthor;
_isbn = strISBN;
_subject = strSubject;
}
private string _name;
private string _description;
private string _author;
private string _isbn;
private string _subject;
public string Name
{
get {return _name;}
set {_name = value;}
}
public string Description
{
get {return _description;}
set {_description = value;}
}
public string Author
{
get {return _author;}
set {_author = value;}
}
public string ISBN
{
get {return _isbn;}
set {_isbn = value;}
}
public string Subject
{
get {return _subject;}
set {_subject = value;}
}
}
}
namespace Common
{
/// <summary>
/// BookData 的摘要说明。
/// </summary>
public class Book
{
public Book()
{
}
//有参数构造函数
public Book(string strName,string strAuthor,string strISBN,string strDescription,string strSubject)
{
_name = strName;
_description = strDescription;
_author = strAuthor;
_isbn = strISBN;
_subject = strSubject;
}
private string _name;
private string _description;
private string _author;
private string _isbn;
private string _subject;
public string Name
{
get {return _name;}
set {_name = value;}
}
public string Description
{
get {return _description;}
set {_description = value;}
}
public string Author
{
get {return _author;}
set {_author = value;}
}
public string ISBN
{
get {return _isbn;}
set {_isbn = value;}
}
public string Subject
{
get {return _subject;}
set {_subject = value;}
}
}
}
<四>数据访问层BookDA.cs代码如下
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common;
namespace DAL
{
/// <summary>
/// Book 的摘要说明。
/// </summary>
public class BookDA
{
//创建数据库实例
private Database _db = DatabaseFactory.CreateDatabase("WorkFlow");
public BookDA()
{
}
public string AddBook(Book book)//使用Common层Book类
{
string sqlCommand = "AddBook";
//获取存储过程
DBCommandWrapper dbCommandWrapper=_db.GetStoredProcCommandWrapper(sqlCommand);
//输入参数,利用Common层Book类的Name属性赋值
dbCommandWrapper.AddInParameter("@Name", DbType.String, book.Name);
//
dbCommandWrapper.AddInParameter("@Author",DbType.String,book.Author);
dbCommandWrapper.AddInParameter("@ISBN",DbType.String,book.ISBN);
dbCommandWrapper.AddInParameter("@Description",DbType.String,book.Description);
dbCommandWrapper.AddInParameter("@Subject",DbType.String,book.Subject);
//输出参数
dbCommandWrapper.AddOutParameter("@ItemId",DbType.Int16,4);
//执行
_db.ExecuteNonQuery(dbCommandWrapper);
//获取指定参数
return dbCommandWrapper.GetParameterValue("@ItemId").ToString();
}
}
}
using Microsoft.Practices.EnterpriseLibrary.Data;
using Common;
namespace DAL
{
/// <summary>
/// Book 的摘要说明。
/// </summary>
public class BookDA
{
//创建数据库实例
private Database _db = DatabaseFactory.CreateDatabase("WorkFlow");
public BookDA()
{
}
public string AddBook(Book book)//使用Common层Book类
{
string sqlCommand = "AddBook";
//获取存储过程
DBCommandWrapper dbCommandWrapper=_db.GetStoredProcCommandWrapper(sqlCommand);
//输入参数,利用Common层Book类的Name属性赋值
dbCommandWrapper.AddInParameter("@Name", DbType.String, book.Name);
//
dbCommandWrapper.AddInParameter("@Author",DbType.String,book.Author);
dbCommandWrapper.AddInParameter("@ISBN",DbType.String,book.ISBN);
dbCommandWrapper.AddInParameter("@Description",DbType.String,book.Description);
dbCommandWrapper.AddInParameter("@Subject",DbType.String,book.Subject);
//输出参数
dbCommandWrapper.AddOutParameter("@ItemId",DbType.Int16,4);
//执行
_db.ExecuteNonQuery(dbCommandWrapper);
//获取指定参数
return dbCommandWrapper.GetParameterValue("@ItemId").ToString();
}
}
}
<五>存储过程addbook
CREATE procedure addbook
( @Name varchar(50),
@Author varchar(50),
@ISBN varchar(50),
@Description varchar(50),
@Subject varchar(50),
@ItemId INT = NULL OUTPUT )
as insert into books values(@Name,@Author,@ISBN,@Description,@Subject)
select @ItemId=@@Identity GO
<六>编译,运行出现错误提示: 安全性异常 说明: 应用程序试图执行安全策略不允许的操作。要授予此应用程序所需的权限,请与系统管理员联系,或在配置文件中更改该应用程序的信任级别。
异常详细信息: System.Security.SecurityException: 不允许所请求的注册表访问权。
解决方法:请运行你安装目录下的(我的安装目录是D:\Program Files\Microsoft Enterprise Library June 2005\src)InstallServices.bat 即可解决问题.