程序,人生,思想,灵魂。

欢迎大家访问我的个人网站 萌萌的IT人

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

[原创]Enterprise Library4.1学习笔记(一)数据访问

 

Enterprise Library v4.1 是patterns & practices 小组为.NET Framework 3.5 开发一套企业库,对企业应用开发非常有帮助,也非常实用。

没事的时候研究了下,把学习过程贴出来,希望能给想要学习 Enterprise Library 的人一点帮助。

 

(一)下载/安装

下载地址:http://www.microsoft.com/downloads/details.aspx?FamilyId=1643758B-2986-47F7-B529-3E41584B6CE5&displaylang=en

下载完后,双击安装,安装过程是从“下一步”一直到“完成”,没有必要讲。

 

(二)  新建解决方案(LearnEntLib),并在解决方案下添加web应用程序项目(DataAccessApplicationBlock)。

解决方案和程序项目建好后,就可以添加数据访问组件的引用了,如下图所示:

添加完引用后,我们就开始配置数据库参数,不须要手工配置,直接打开就可以了,如下图:

新建一个连接,如下图:

配置该连接数据源。如下图:

 

选择默认数据库,如下图:

设置完了后,保存退出,再打开web.config文件,发现已经配置好了。

 

打开default.aspx,转到代码页,添加命名空间:

 

 

现在我们可以开始写代码了。

 先定义一个公共的DataBase对象,

 Database db = DatabaseFactory.CreateDatabase();

CreateDatabase()----默认的数据库。
CreateDatabase(string name)----指定名称的数据库。

 

(1)增/删/改

复制代码
//
DbCommand insertCommand = db.GetSqlStringCommand("insert into Categories(CategoryName,Description) values(@CategoryName,@Description)");
db.AddInParameter(insertCommand, 
"@CategoryName", DbType.String, "测试CategoryName");
db.AddInParameter(insertCommand, 
"@Description", DbType.String, "测试Description");
db.ExecuteNonQuery(insertCommand);

//
DbCommand deleteCommand = db.GetSqlStringCommand("delete from Categories where CategoryID=@CategoryID");
db.AddInParameter(deleteCommand, 
"@CategoryID", DbType.Int32, 16);
db.ExecuteNonQuery(deleteCommand);

//
DbCommand updateCommand = db.GetSqlStringCommand("update Categories set CategoryName=@CategoryName,Description=@Description where CategoryID=@CategoryID");
db.AddInParameter(updateCommand, 
"@CategoryName", DbType.String, "测试CategoryName1");
db.AddInParameter(updateCommand, 
"@Description", DbType.String, "测试Description1");
db.AddInParameter(updateCommand, 
"@CategoryID", DbType.Int32, 17);
db.ExecuteNonQuery(updateCommand);
复制代码

 

 

(2)一条一条数据读取。ExecuteReader

复制代码
DbCommand reader = db.GetSqlStringCommand("select CategoryID,CategoryName from Categories where CategoryID>@CategoryID");
db.AddInParameter(reader, 
"@CategoryID", DbType.Int32, 10);
using (IDataReader datareader = db.ExecuteReader(reader))
{
    
while (datareader.Read())
    {
        
//DropDownList1.Items.Add(new ListItem(datareader["CategoryName"].ToString(), datareader["CategoryID"].ToString()));
        DropDownList1.Items.Add(new ListItem(datareader.GetString(1), datareader.GetInt32(0).ToString()));
    }

    
if (DropDownList1.Items.Count > 0)
    {
        DropDownList1.SelectedIndex 
= 0;
    }
}
复制代码

 

 

(3)返回数据集。ExecuteDataSet和LoadDataSet

这两个方法都是返回数据集,不同的地方是:前者返回一个新的数据集,后者返把返回的数据填充到已存在的数据集当中。

复制代码
//ExecuteDataSet
DbCommand selectcategories = db.GetSqlStringCommand("select * from Categories where  CategoryID>@CategoryID");
db.AddInParameter(selectcategories, 
"@CategoryID", DbType.Int32, 2);
GridView1.DataSource 
= db.ExecuteDataSet(selectcategories);
GridView1.DataBind();

//LoadDataSet
DataSet ds = new DataSet();
DbCommand selectprod 
= db.GetSqlStringCommand("select * from Products where CategoryID>@CategoryID;select * from Categories");
db.AddInParameter(selectprod, 
"@CategoryID", DbType.Int32, 7);
//db.LoadDataSet(selectprod, ds, new string[] { "SelectProdues", "Categories" });
db.LoadDataSet(selectprod, ds, "Categories");

GridView1.DataSource 
= ds.Tables["Categories"];
GridView1.DataBind();
复制代码

 

 

(4)返回一行数据。ExecuteScalar

DbCommand scalar = db.GetSqlStringCommand("select count(*) from Categories where  CategoryID>@CategoryID");
db.AddInParameter(scalar, 
"@CategoryID", DbType.Int32, 10);
int count = 0;
int.TryParse(db.ExecuteScalar(scalar).ToString(), out count);
Response.Write(count);

 

 

 

(5)添加事务

就拿 '添加' 来写个例子吧,其它(删/改)也同理。如下:

复制代码
DbCommand insertCommand = db.GetSqlStringCommand("insert into Categories(CategoryName,Description) values(@CategoryName,@Description)");
db.AddInParameter(insertCommand, 
"@CategoryName", DbType.String, "测试CategoryName");
db.AddInParameter(insertCommand, 
"@Description", DbType.String, "测试Description");

DbCommand insertCommand2 
= db.GetSqlStringCommand("insert into T_Perent(PERENTNAME,PERENTINFO) values(@PERENTNAME,@PERENTINFO)");
db.AddInParameter(insertCommand2, 
"@PERENTNAME", DbType.String, "测试CategoryName");
db.AddInParameter(insertCommand2, 
"@PERENTINFO", DbType.String, "测试Description");

using (DbConnection conn = db.CreateConnection())
{
    conn.Open();
    DbTransaction tran 
= conn.BeginTransaction();
    
try
    {
        db.ExecuteNonQuery(insertCommand, tran);
        db.ExecuteNonQuery(insertCommand2, tran);
        tran.Commit();
    }
    
catch
    {
        tran.Rollback();
    }
    conn.Close();
}
复制代码

 

注:上面的方法如果要用到存贮过程,只须要把db.GetSqlStringCommand(sql语句) 改为db.GetStoredProcCommand(存贮过程名)就可以了,别的地方不用修改。

 

(三)加密连接字符串

这个在Enterprise Liberary4.1中变得很简单了。

 

 

按上图操作,保存就可以了,保存后,再打开web.config,如下:

 

已经加密了。

 

posted on   乔帮主  阅读(850)  评论(1编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示