本系列文章將介紹ObjectDataSource的使用,為了內容的完成性,所以雖然簡單,但是還是發到首頁,不知道行不行
本系列文章主要參考MSDN,ASP.NET快速入門和ASP.NET的文章整理而成,將由淺入深說明ObjectDataSource的使用,僅供參考,不保證內容100%的爭取
1 SqlDataSource和ObjectDataSource控制項的比較
ASP.NET2.0提供了SqlDataSource資料來源控制項,後者支援用於指定連接字串、SQL 陳述式或預存程序的屬性,用以查詢或修改資料庫。但是,SqlDataSource 控制項存在一個問題:該控制項的缺點在於它迫使您將使用者介面層與商務邏輯層混合在一起。然而隨著應用程式規模的擴大,您會越來越感覺到混合多個層的做法是不可取的。 產生嚴格意義上的多層 Web 應用程式時,您應該具有清晰的使用者介面層、商務邏輯層和資料訪問層。僅僅由於 SqlDataSource 控制項的強制而在使用者介面層引用 SQL 陳述式或預存程序是不可取的。
SqlDataSource和ObjectDataSource的選擇,從這某中意義上說,前者適合大多數小規模的個人或業餘網站,而對於較大規模的企業級應用程式,在應用程式的呈現頁中直接儲存 SQL 陳述式可能很快就會變得無法維護。這些應用程式通常需要用中介層資料訪問層或業務組件構成的封裝性更好的資料模型。所以使用 ObjectDataSource 控制項是一種較為明智和通用的做法。
2 ObjectDataSource的概述
ObjectDataSource 控制項物件模型類似於 SqlDataSource 控制項。ObjectDataSource 公開一個 TypeName 屬性(而不是 ConnectionString 屬性),該屬性指定要執行個體化來執行資料操作的物件類型(類名)。類似於 SqlDataSource 的命令屬性,ObjectDataSource 控制項支援諸如 SelectMethod、UpdateMethod、InsertMethod 和 DeleteMethod 的屬性,用於指定要調用來執行這些資料操作的關聯類別型的方法。本節介紹一些方法,用於構建資料訪問層和商務邏輯層組件並通過 ObjectDataSource 控制項公開這些組件。 下面是該控制項的聲明方式:
<asp:ObjectDataSource
CacheDuration="string|Infinite" CacheExpirationPolicy="Absolute|Sliding"
CacheKeyDependency="string"
ConflictDetection="OverwriteChanges|CompareAllValues"
ConvertNullToDBNull="True|False" DataObjectTypeName="string"
DeleteMethod="string" EnableCaching="True|False"
EnablePaging="True|False" EnableTheming="True|False"
EnableViewState="True|False" FilterExpression="string"
ID="string" InsertMethod="string"
MaximumRowsParameterName="string"
OldValuesParameterFormatString="string"
OnDataBinding="DataBinding event handler"
OnDeleted="Deleted event handler" OnDeleting="Deleting event handler"
OnDisposed="Disposed event handler" OnFiltering="Filtering event handler"
OnInit="Init event handler" OnInserted="Inserted event handler"
OnInserting="Inserting event handler" OnLoad="Load event handler"
OnObjectCreated="ObjectCreated event handler"
OnObjectCreating="ObjectCreating event handler"
OnObjectDisposing="ObjectDisposing event handler"
OnPreRender="PreRender event handler" OnSelected="Selected event handler"
OnSelecting="Selecting event handler" OnUnload="Unload event handler"
OnUpdated="Updated event handler" OnUpdating="Updating event handler"
runat="server" SelectCountMethod="string"
SelectMethod="string" SortParameterName="string"
SqlCacheDependency="string" StartRowIndexParameterName="string"
TypeName="string" UpdateMethod="string"
>
<DeleteParameters>
<asp:ControlParameter ControlID="string"
ConvertEmptyStringToNull="True|False"
DefaultValue="string"
Direction="Input|Output|InputOutput|ReturnValue"
Name="string"
PropertyName="string"
Size="integer"
Type="Empty|Object|DBNull|Boolean|Char|SByte|
Byte|Int16|UInt16|Int32|UInt32|Int64|UInt64|
Single|Double|Decimal|DateTime|String"
/>
<asp:CookieParameter CookieName="string" />
<asp:FormParameter FormField="string" />
<asp:Parameter Name="string" />
<asp:ProfileParameter PropertyName="string" />
<asp:QueryStringParameter QueryStringField="string" />
<asp:SessionParameter SessionField="string" />
</DeleteParameters>
<FilterParameters>... ...</FilterParameters>
<InsertParameters>... ...</InsertParameters>
<SelectParameters>... ...</SelectParameters>
<UpdateParameters>... ...</UpdateParameters>
</asp:ObjectDataSource>
3綁定到資料訪問層
資料訪問層組件封裝 ADO.NET 代碼以通過 SQL 命令查詢和修改資料庫。它通常提煉建立 ADO.NET 串連和命令的詳細資料,並通過可使用適當的參數調用的方法公開這些詳細資料。典型的資料訪問層組件可按如下方式公開:
public class MyDataBllLayer {
public DataView GetRecords();
public int UpdateRecord(int recordID, String recordData);
public int DeleteRecord(int recordID);
public int InsertRecord(int recordID, String recordData);
}
也就是,通常是在商務邏輯訪問層定義對資料庫裡記錄的操作,上面就定義了GetRecords、UpdateRecord、DeleteRecord和InsertRecord四個方法來讀取、更新、刪除和插入資料庫裡的資料,這些方法基本上是根據SQL裡的Select、Update、Delete和Insert語句而定義。
和上面方法相對應, ObjectDataSource提供了四個屬性來設定該控制項引用的資料處理,可以按照如下方式關聯到該類型,代碼如下
<asp:ObjectDataSource TypeName="MyDataLayer" runat="server"
SelectMethod="GetRecords"
UpdateMethod="UpdateRecord"
DeleteMethod="DeleteRecord"
InsertMethod="InsertRecord"
/>
這裡的SelectMethon設定為MyDataBllLayer裡的GetRecords()方法,在使用時需要注意ObjectDataSource旨在以聲明的方式簡化資料的開發,所以這裡設定SelectMethod的值為GetRecords而不是GetRecords()。
同樣依次類推,UpdateMethod、DeleteMethod、InsertMethod分別對應的是UpdateRecord
、DeleteRecord、InsertRecord方法。
在上面GetRecords()的定義時,讀者可以看到該方法返回的類型是DataView,由於ObjectDataSource將來需要作為繫結控制項的資料來源,所以它的傳回型別必須如下的傳回型別之一:
Ienumerable、DataTable、DataView、DataSet或者Object。
除此以外,ObjectDataSource還有一個重要的屬性TypeName,ObjectDataSource