Enterprise Library 企业库 示例(摘录)
http://www.cnblogs.com/terryfeng/archive/2009/10/03/1577711.html
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)"
关于缓存:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
namespace CachingBlock
{
public class MyData
{
public string Name { set; get; }
public int Age { set; get; }
public string Color { set; get; }
}
public partial class WebForm1 : System.Web.UI.Page
{
const string KEYNAME = "myDateCache";//缓存的键值
ICacheManager cacheManager;
protected void Page_Load(object sender, EventArgs e)
{
cacheManager = CacheFactory.GetCacheManager();//实例化ICachemanager
}
protected void btnWrite_Click(object sender, EventArgs e)
{
//生成要缓存的数据(实际开发中可以是从数据库查询出来的数据)
List<MyData> _list = new List<MyData>{
new MyData(){ Age=1, Color="Yellow", Name="China"},
new MyData{ Age=2,Color="Black",Name="USA"}
};
AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddSeconds(30));//指定30秒后过期
cacheManager.Add(KEYNAME, _list, CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
Response.Write("Cache写入完成," + DateTime.Now.ToString());
}
protected void btnRead_Click(object sender, EventArgs e)
{
this.R1.DataSource = GetCacheData();
this.R1.DataBind();
Response.Write("Cache加载完成," + DateTime.Now.ToString());
}
/// <summary>
/// 获取缓存数据
/// </summary>
/// <returns></returns>
public List<MyData> GetCacheData()
{
List<MyData> _cacheData = cacheManager.GetData(KEYNAME) as List<MyData>;
if (null == _cacheData)//记得一定要加此判断(因为缓存可能过期)
{
//如果缓存数据为空,则重新生成数据,并加入缓存(为检测效果,特地把Color与Name前加了一个"New")
_cacheData = new List<MyData>
{
new MyData(){ Age=1, Color="New Yellow", Name="New China"},
new MyData{ Age=2,Color="New Black",Name="New USA"}
};
AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddSeconds(30));//指定30秒后过期
cacheManager.Add(KEYNAME, _cacheData, CacheItemPriority.Normal, null, _ExpireTime);
}
return _cacheData;
}
protected void btnRemove_Click(object sender, EventArgs e)
{
cacheManager.Remove(KEYNAME);
Response.Write("Cache清空完成," + DateTime.Now.ToString());
}
}
}


浙公网安备 33010602011771号