[轉載]DAAB 3.1使用筆記

來源:互聯網
上載者:User

相信很多人使用過Microsoft Data Application Blocks,在以前的版本中(3.0之前),都是通過SqlHelper類來實現,適用SQLServer資料庫,如果使用其他資料庫,就需要重新寫一個,很麻煩,PetShop3.0中就是另外寫了一個OraHelper來實現Oracle資料庫的操作,在3.1版中這種情況可不再有了。

可以到gotdotnet下載,具體地址不記得了。
DAAB3.1連NameSpace也改了:GotDotNet.ApplicationBlocks.Data,通過一個通用類AdoHelper實現了SqlServer, Oracle, OleDB, ODBC不同類型的串連,由AdoHelper自動選擇合適的資料庫連接類來串連,當你在不同資料庫間切換時,可以在只修改設定檔不改動代碼的情況下實現資料庫的轉換。
在設定檔中可以這樣配置:
 <configSections>
  <section name="daabProviders" type="GotDotNet.ApplicationBlocks.Data.DAABSectionHandler, GotDotNet.ApplicationBlocks.Data">
  </section>
 </configSections>

 <daabProviders>
  <daabProvider alias="misapp" assembly="GotDotNet.ApplicationBlocks.Data" type="GotDotNet.ApplicationBlocks.Data.SqlServer" />
 </daabProviders>
代碼中只要使用(vb)
    Dim adoHelper As AdoHelper = AdoHelper.CreateHelper("misapp")
即可建立AdoHelper的一個執行個體,以後如果資料庫改為Oracle,只需將設定檔中串連串相應修改,同時將daabProviders一節改為以下內容即可,如果使用的是標準sql語句,代碼中不需任何修改。
 <daabProviders>
  <daabProvider alias="misapp" assembly="GotDotNet.ApplicationBlocks.Data" type="GotDotNet.ApplicationBlocks.Data.Oracle" />
 </daabProviders>
如果不使用別名,則要在執行個體化AdoHelper時指定所的資料庫類型,如(vb):
Dim sqlHelper As AdoHelper = AdoHelper.CreateHelper(GetType(SqlServer).Assembly.FullName, GetType(SqlServer).FullName)

Dim sqlHelper As AdoHelper = AdoHelper.CreateHelper("GotDotNet.ApplicationBlocks.Data, GotDotNet.ApplicationBlocks.Data.SqlServer)
接下來的使用就很簡單了(C#,AspNetPager樣本),
  private void Page_Load(object sender, System.EventArgs e)
  {
   AdoHelper helper = AdoHelper.CreateHelper("misapp");
   
   string connString = System.Configuration.ConfigurationSettings.AppSettings["connString"];
   if(!Page.IsPostBack)
   {
    AspNetPager1.RecordCount=(int)helper.ExecuteScalar(connString,System.Data.CommandType.Text,"select count(*) from orders");
    BindData();
   }
  }

  void BindData()
  {
   AdoHelper helper = AdoHelper.CreateHelper("misapp");
   string connString = System.Configuration.ConfigurationSettings.AppSettings["connString"];
   IDataParameter[] para = new IDataParameter[2];
   para = helper.GetSpParameterSet(connString,"mypager");
   
//   mypager為擷取分頁資料的預存程序
   para[0].Value=AspNetPager1.PageSize;
   para[1].Value=AspNetPager1.CurrentPageIndex;

   DataGrid1.DataSource=helper.ExecuteReader(connString,"mypager",para);
   
   DataGrid1.DataBind();
  }
  public void AspNetPager1_PageChanged(object src, Wuqi.Webdiyer.PageChangedEventArgs e)
  {
   AspNetPager1.CurrentPageIndex=e.NewPageIndex;
   BindData();
  }
上面這段代碼不需任何修改即可改為Oracle上的應用(資料庫中的預存程序還是要改的,設定檔要改一下)

Data Access Application Block 3.1 :
http://www.gotdotnet.com/workspaces/releases/viewuploads.aspx?id=c20d12b0-af52-402b-9b7c-aaeb21d1f431

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.