1. Enterprise Library 企業庫介紹
是微軟模式與 實踐團隊 開的應用程序塊安裝Enterprise Library
下載 www.misrosoft.com / en - us / download / details.aspx ? id = 15104安裝後目錄結構
bin目錄包含應用程序塊
Blocks目錄包含企業庫源代碼
Lib 目錄包含所有程序塊共用的程序集
Scripts 目錄包含編譯應用程序塊的批量處理文件和需要使用的數據庫腳本-
使用步驟
1.打開安裝目錄中bin目錄下的EntLibConfig.exe可執行文件
2.點擊"Blocks "菜單,在其下拉菜單中選擇"Add Data Setting "
3.點擊"Add Data Setting" 後 出現"Database Setting" 窗口 ,編輯"Name"和"Connection String"
DataBase Setting 參數含義
name 有應用程序訪問的數據庫實例的邏輯名稱,對應Web.config 文件中ConnectionStrings 節點的Name 屬性值 。此屬性是必須的
providerName 提供程序的名稱 默認情況下是System.Data.Sqlclient
connectionString 修改爲指定數據庫的連接字符串
4.保存到Web.config 打開Web.config自動增加ConectionStrings
5.站點應用,添加企業庫的DLL文件
6.引用 using Microsoft.Practices.EnterpriseLibrary.Data;
2. Enterprise Library 企業庫實際運用
語法
大多數企業級應用都將數據存儲在關係型數據庫中
微軟企業庫中的DAAB封裝了大多數常用數據庫中需要執行的操作邏輯,最大限度地降低了自定義代碼的需求
並提供了一致的接口執行操作
DataBase 累的常用方法
ExecuteNonQuery 執行傳入的Dbcommand對象或sql語句 返回受影響的行數
ExecuteReader 執行傳入的Dbcommand對象或sql,返回支持IDdataReader接口的類的實例
ExecuteDataSet 執行傳入的Dbcommand對象或sql,返回DataSet對象
ExecuteScalar 執行傳入的Dbcommand對象或sql,返回一個obiect
AddParameter 爲Dbcommand對象傳入一個新的參數對象
AddIntParameter 爲Dbcommand對象傳入一個新的輸入參數對象
AddOutParameter 爲Dbcommand對象傳入一個新的輸出參數對象-
GetParameterValue 獲取Dbcommand對象指定名稱的參數的值
SetParameterValue 爲Dbcommand對象指定名稱的參數賦值
GetSqlStringCommand 依據傳入的SQl語句獲取一個DBcommand對象
GetStoredProcCommand 依據傳入的存儲過程獲取一個DBcommand對象
DbCommand 是ADO.NET 中已經存在的抽象類 位於System.Data.dll程序集中的System.Data.Common
pS:執行sql語句 需要轉化 CommandType.Text
實例
<form id="form1" runat="server">
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
</form>
protected void Page_Load(object sender, EventArgs e)
{
BBD();
BBD2();
}
public void BBD() {
Database db = DatabaseFactory.CreateDatabase("ConStr");
string sql = "select * from Brand";
DataSet set = db.ExecuteDataSet(CommandType.Text,sql);
DropDownList1.DataSource = set.Tables[0];
DropDownList1.DataTextField = "BdName";
DropDownList1.DataValueField = "Bdid";
DropDownList1.DataBind();
}
public void BBD2()
{
Database db = DatabaseFactory.CreateDatabase("ConStr");
string sql = "select * from Brand";
DbCommand cmd = db.GetSqlStringCommand(sql);
DataSet set = db.ExecuteDataSet(cmd);
DropDownList2.DataSource = set.Tables[0];
DropDownList2.DataTextField = "BdName";
DropDownList2.DataValueField = "Bdid";
DropDownList2.DataBind();
}
進階 調用存儲過程
public void BBD()
{
Database db = DatabaseFactory.CreateDatabase("ConStr");
DataSet set = db.ExecuteDataSet("Select_Brand");
DropDownList1.DataSource = set.Tables[0];
DropDownList1.DataTextField = "BdName";
DropDownList1.DataValueField = "Bdid";
DropDownList1.DataBind();
}
public void BBD2()
{
Database db = DatabaseFactory.CreateDatabase("ConStr");
DbCommand cmd = db.GetStoredProcCommand("Select_Brand");
DataSet set = db.ExecuteDataSet(cmd);
DropDownList2.DataSource = set.Tables[0];
DropDownList2.DataTextField = "BdName";
DropDownList2.DataValueField = "Bdid";
DropDownList2.DataBind();
}