[原創]Enterprise Library4.1學習筆記(一)數據訪問
Enterprise Library v4.1 是patterns & practices 小組為.NET Framework 3.5 開發一套企業庫,對企業應用開發非常有幫助,也非常實用。
沒事的時候研究了下,把學習過程貼出來,希望能給想要學習 Enterprise Library 的人一點幫助。
(一)下載/安裝
下載完后,雙擊安裝,安裝過程是從“下一步”一直到“完成”,沒有必要講。
(二) 新建解決方案(LearnEntLib),并在解決方案下添加web應用程序項目(DataAccessApplicationBlock)。
解決方案和程序項目建好后,就可以添加數據訪問組件的引用了,如下圖所示:

添加完引用后,我們就開始配置數據庫參數,不須要手工配置,直接打開就可以了,如下圖:

新建一個連接,如下圖:

配置該連接數據源。如下圖:

選擇默認數據庫,如下圖:

設置完了后,保存退出,再打開web.config文件,發現已經配置好了。

打開default.aspx,轉到代碼頁,添加命名空間:

現在我們可以開始寫代碼了。
先定義一個公共的DataBase對象,
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
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
這兩個方法都是返回數據集,不同的地方是:前者返回一個新的數據集,后者返把返回的數據填充到已存在的數據集當中。
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
db.AddInParameter(scalar, "@CategoryID", DbType.Int32, 10);
int count = 0;
int.TryParse(db.ExecuteScalar(scalar).ToString(), out count);
Response.Write(count);
(5)添加事務
就拿 '添加' 來寫個例子吧,其它(刪/改)也同理。如下:
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,如下:

已經加密了。

浙公網安備 33010602011771號