Data Access Application Block 其实个人感觉相当于另一个版本的dbHelper
废话不多说,先看下如何使用:
1.引用Microsoft.Practices.EnterpriseLibrary.Data.dll(EL安装目录下的bin中就有)
2.配置web.config
<configSections>
...
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral"/>
...
这一节是系统自动加上去的,原来后面还有一截PublicKeyToken=....,不过在使用中发现系统总是报错无法加载EL程序集之类,所以到园子里搜索了下,参考前人的解决办法去掉了这一段(可能是发行时版本不一致造成的)
此外还要添加连接字符串
<connectionStrings>
<add name="Conn" connectionString="Data Source=.;Initial Catalog=Six;Persist Security Info=True;User ID=sa;Password=***" providerName="System.Data.SqlClient"/>
<add name="Conn2" connectionString="Data Source=.;Initial Catalog=dbUser;Persist Security Info=True;User ID=sa;Password=***" providerName="System.Data.SqlClient"/>
</connectionStrings>
<dataConfiguration defaultDatabase="Conn"/>
这跟常规的web开发几乎没什么不同,要说明的是<dataConfiguration defaultDatabase="Conn"/>这一节标明了默认情况下使用哪个连接字符串
3.下面可以正式使用了,示例代码:
using
System;
using
System.Data;
using
System.Data.Common;
using
Microsoft.Practices.EnterpriseLibrary.Data;
namespace
test
{
public
partial
class
_Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
Database db
=
DatabaseFactory.CreateDatabase(
"
Conn
"
);
//
查询数据
//
this.Repeater1.DataSource = db.ExecuteReader(CommandType.Text, "Select * from GoGo365");
//
调用存储过程(直接传参数的简单调用)
//
this.Repeater1.DataSource = db.ExecuteReader("up_test", "2");
//
切换数据库
db
=
DatabaseFactory.CreateDatabase(
"
Conn2
"
);
//
带输出和返回参数的存储过程调用
DbCommand cmd
=
db.GetStoredProcCommand(
"
up_Page2005
"
);
db.AddInParameter(cmd,
"
TableName
"
, DbType.String,
"
T_User
"
);
db.AddInParameter(cmd,
"
Fields
"
, DbType.String,
"
F_NickName
"
);
db.AddInParameter(cmd,
"
OrderField
"
, DbType.String,
"
F_Id
"
);
db.AddInParameter(cmd,
"
sqlWhere
"
, DbType.String,
"
F_Status=1
"
);
db.AddInParameter(cmd,
"
PageSize
"
, DbType.Int16,
50
);
db.AddInParameter(cmd,
"
PageIndex
"
, DbType.Int16,
2
);
db.AddOutParameter(cmd,
"
TotalPage
"
, DbType.Int16,
4
);
db.AddParameter(cmd,
"
RecordCount
"
, DbType.Int16, ParameterDirection.ReturnValue,
""
, DataRowVersion.Default,
0
);
this
.Repeater1.DataSource
=
db.ExecuteReader(cmd);
this
.Repeater1.DataBind();
Response.Write(
"
输出参数值:
"
+
db.GetParameterValue(cmd,
"
TotalPage
"
).ToString());
Response.Write(
"
<br />返回参数值:
"
+
db.GetParameterValue(cmd,
"
RecordCount
"
).ToString());
//
使用事务
db
=
DatabaseFactory.CreateDatabase(
"
Conn
"
);
using
(IDbConnection conn
=
db.CreateConnection())
{
conn.Open();
IDbTransaction _trans
=
conn.BeginTransaction();
try
{
DbCommand _cmd
=
db.GetSqlStringCommand(
"
Insert Into GoGo365(F_NickName) values(@NickName)
"
);
db.AddInParameter(_cmd,
"
NickName
"
, DbType.String,
"
AA
"
);
db.ExecuteNonQuery(_cmd, _trans
as
DbTransaction);
db.ExecuteNonQuery(_cmd, _trans
as
DbTransaction);
//
F_NickName字段上建有唯一索引,故第二次插入同样记录时会报错
_trans.Commit();
}
catch
{
try
{
_trans.Rollback();
//
事务提交失败时,则回滚(是否回滚成功,可查看表中有无AA的记录即可)
}
catch
{ }
}
finally
{
conn.Close();
}
}
}
}
}
基本上涉及数据库操作的东东都已经在里面了,更多功能大家自己去体验吧