日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET2.0 ObjectDataSource的使用详解(1)

發布時間:2025/3/13 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET2.0 ObjectDataSource的使用详解(1) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本系列文章將介紹ObjectDataSource的使用,為了內容的完成性,所以雖然簡單,但是還是發到首頁,不知道行不行
本系列文章主要參考MSDN,ASP.NET快速入門和ASP.NET的文章整理而成,將由淺入深說明ObjectDataSource的使用,僅供參考,不保證內容100%的爭取


1
SqlDataSourceObjectDataSource控件的比較

ASP.NET2.0提供了SqlDataSource數據源控件,后者支持用于指定連接字符串、SQL 語句或存儲過程的屬性,用以查詢或修改數據庫。但是,SqlDataSource 控件存在一個問題:該控件的缺點在于它迫使您將用戶界面層與業務邏輯層混合在一起。然而隨著應用程序規模的擴大,您會越來越感覺到混合多個層的做法是不可取的。 生成嚴格意義上的多層 Web 應用程序時,您應該具有清晰的用戶界面層、業務邏輯層和數據訪問層。僅僅由于 SqlDataSource 控件的強制而在用戶界面層引用 SQL 語句或存儲過程是不可取的。

SqlDataSourceObjectDataSource的選擇,從這某中意義上說,前者適合大多數小規模的個人或業余站點,而對于較大規模的企業級應用程序,在應用程序的呈現頁中直接存儲 SQL 語句可能很快就會變得無法維護。這些應用程序通常需要用中間層數據訪問層或業務組件構成的封裝性更好的數據模型。所以使用 ObjectDataSource 控件是一種較為明智和通用的做法。

?

2 ObjectDataSource的概述

ObjectDataSource 控件對象模型類似于 SqlDataSource 控件。ObjectDataSource 公開一個 TypeName 屬性(而不是 ConnectionString 屬性),該屬性指定要實例化來執行數據操作的對象類型(類名)。類似于 SqlDataSource 的命令屬性,ObjectDataSource 控件支持諸如 SelectMethodUpdateMethodInsertMethod 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);

}

也就是,通常是在業務邏輯訪問層定義對數據庫里記錄的操作,上面就定義了GetRecordsUpdateRecordDeleteRecordInsertRecord四個方法來讀取、更新、刪除和插入數據庫里的數據,這些方法基本上是根據SQL里的SelectUpdateDeleteInsert語句而定義。

和上面方法相對應, ObjectDataSource提供了四個屬性來設置該控件引用的數據處理,可以按照如下方式關聯到該類型,代碼如下

? <asp:ObjectDataSource TypeName="MyDataLayer"?? runat="server"

???? SelectMethod="GetRecords"

UpdateMethod="UpdateRecord"?

? ???DeleteMethod="DeleteRecord"

InsertMethod="InsertRecord"

/>

??? 這里的SelectMethon設置為MyDataBllLayer里的GetRecords()方法,在使用時需要注意ObjectDataSource旨在以聲明的方式簡化數據的開發,所以這里設置SelectMethod的值為GetRecords而不是GetRecords()

同樣依次類推,UpdateMethodDeleteMethodInsertMethod分別對應的是UpdateRecord

DeleteRecordInsertRecord方法。

??? 在上面GetRecords()的定義時,讀者可以看到該方法返回的類型是DataView,由于ObjectDataSource將來需要作為綁定控件的數據來源,所以它的返回類型必須如下的返回類型之一:

IenumerableDataTableDataViewDataSet或者Object

???? 除此以外,ObjectDataSource還有一個重要的屬性TypeNameObjectDataSource控件使用反射技術來從來從業務邏輯程序層的類對象調用相應的方法,所以TypeName的屬性值就是用來標識該控件工作時使用的類名稱,下面通過Simple_ObjectDataSource.aspx來說明ObjectDataSource的基本使用。

1)建立數據業務邏輯層

?? 為了方裝業務邏輯我建立了ProductDAL.cs文件。在該文件里定義了GetProduct方法獲取產品列表,UpdateProduct方法更新產品記錄,DeleteProduct刪除產品記錄,為了便于共享,將該文件放置在App_Code目錄下,完整代碼如1-1

?using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration;

using System.Data;

using System.Data.Common;

using System.Data.SqlClient;

using System.Web;

?

/// <summary>

/// Summary description for ProductBLL

/// </summary>

public class ProductDAL

{

??? protected int _count = -1;

??? public ProductDAL()

??? {?? }

?

???? string?? _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

?

??? public SqlDataReader GetProduct()

??? {

??????? SqlConnection con = new SqlConnection(_connectionString);

??????? string selectString = "SELECT * FROM Products";

??????? SqlCommand cmd = new SqlCommand(selectString, con);

??????? con.Open();

?????? ?SqlDataReader dtr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

??????? return dtr;

???????? }

?

?

??? public void UpdateProduct(int productID, string productName, int categoryID, decimal price, Int16 inStore,string description)

??? {

??????? SqlConnection con = new SqlConnection(_connectionString);

??????? string updateString = "UPDATE Products set ProductName=@ProductName,CategoryID=@CategoryID,Price=@Price,InStore=@InStore,Description=@Description where ProductID=@ProductID";

??????? SqlCommand cmd = new SqlCommand(updateString, con);

??????? cmd.Parameters.AddWithValue("@ProductID",productID);

??????? cmd.Parameters.AddWithValue("@ProductName",productName);

??????? cmd.Parameters.AddWithValue("@CategoryID",categoryID);

?????? ?cmd.Parameters.AddWithValue("@Price",price);

??????? cmd.Parameters.AddWithValue("@InStore",inStore);

??????? cmd.Parameters.AddWithValue("@Description",description);

??????? con.Open();

??????? cmd.ExecuteNonQuery();

??????? con.Close();

??? }

?

?

??? public?? void DeleteProduct(int ProductId)

??????? {

??????????? SqlConnection con = new SqlConnection(_connectionString);

??????????? string deleteString = "DELETE FROM? Products? WHERE ProductID=@ProductID";

??????????? SqlCommand cmd = new SqlCommand(deleteString, con);

??????????? cmd.Parameters.AddWithValue("@ProductID", ProductId);

??????????? con.Open();

??????????? cmd.ExecuteNonQuery();

??????????? con.Close();

??????? }

}

????????? 代碼1-1 ProductDAL.cs源文件

??

?

2)建立表示層

?建立一個頁面Simple_ObjectDataSource.aspx然后將ObjectDataSource控件托方到Web窗體創,使用默認的IDVisual Stduio.NET2005為我們建立業務邏輯提供了強大的支持。選中ObjectDataSource1,在其智能配置里選擇配置數據源,彈出配置向導如圖2-1


???????????????????????????????????? 圖
2-1ObjectDataSource配置向導

?

?此時系統會枚舉已經存在的類,選擇ProductDAL,單擊“Next”,進入“Define Data Methods”頁面如圖2-2。在此頁面需要單獨設置SelectUpdate、和Delete分別如下圖



2-30
設置Select屬性對應的方法GetProduct



2-31
設置Update屬性對應的方法UpdateProduct



2-32 設置Delete屬性對應的方法DeleteProduct

?

通過上面的設置系統自動生成如下代碼如下

? <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="DeleteProduct"

??????????? SelectMethod="GetProduct" TypeName="ProductDAL" UpdateMethod="UpdateProduct">

??????????? <DeleteParameters>

??????????????? <asp:Parameter Name="ProductId" Type="Int32" />

?? ?????????</DeleteParameters>

??????????? <UpdateParameters>

??????????????? <asp:Parameter Name="productID" Type="Int32" />

??????????????? <asp:Parameter Name="productName" Type="String" />

??????????????? <asp:Parameter Name="categoryID" Type="Int32" />

??????????????? <asp:Parameter Name="price" Type="Decimal" />

??????????????? <asp:Parameter Name="inStore" Type="Int16" />

??????????????? <asp:Parameter Name="description" Type="String" />

??????????? </UpdateParameters>

??????? </asp:ObjectDataSource>

???????? 代碼2-11 Simple_ObjectDataSource.aspx部分源代碼

2-33顯示了運行結果,此時我們可以編輯或者刪除現有的產品記錄。



???????
2-33 Simple_ObjectDataSource.aspx運行結果

?

注意:如果能夠進行編輯、刪除,你需要將GridViewDataKeyNames設置為數據庫里的主鍵名。具體后面會說明。

?

?

?

?

?

?

?

?

?

???????

?

?

轉載于:https://www.cnblogs.com/mqingqing123/archive/2006/04/20/379710.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的ASP.NET2.0 ObjectDataSource的使用详解(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。