六十五:在TableAdapters中創(chuàng)建新的存儲(chǔ)過程_第1頁
六十五:在TableAdapters中創(chuàng)建新的存儲(chǔ)過程_第2頁
六十五:在TableAdapters中創(chuàng)建新的存儲(chǔ)過程_第3頁
六十五:在TableAdapters中創(chuàng)建新的存儲(chǔ)過程_第4頁
六十五:在TableAdapters中創(chuàng)建新的存儲(chǔ)過程_第5頁
已閱讀5頁,還剩33頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、在ASP.NET 2.0中操作數(shù)據(jù)之六十五:在TableAdapters中創(chuàng)建新的存儲(chǔ)過程作者:heker2007 字體:增加 減小 類型:轉(zhuǎn)載 時(shí)間:2016-05-18 我要評(píng)論本文主要講解使用TableAdapter設(shè)置向?qū)ё詣?dòng)創(chuàng)建增刪改查的存儲(chǔ)過程,雖然自動(dòng)創(chuàng)建存儲(chǔ)過程可以節(jié)省時(shí)間,但他們會(huì)包含一些無用的參數(shù),下節(jié)我們會(huì)介紹TableAdapter使用現(xiàn)有的存儲(chǔ)過程。導(dǎo)言:本教程的Data Access Layer (DAL)使用的是類型化的數(shù)據(jù)集(Typed DataSets).就像我們?cè)诘谝徽聞?chuàng)建一個(gè)數(shù)據(jù)訪問層里探討的一樣,該類型化的數(shù)據(jù)集由強(qiáng)類型的DataTa

2、ble和TableAdapter構(gòu)成。DataTable描繪的是系統(tǒng)里的邏輯實(shí)體而TableAdapter引用相關(guān)數(shù)據(jù)庫執(zhí)行數(shù)據(jù)訪問,包括對(duì)DataTable填充數(shù)據(jù)、執(zhí)行返回標(biāo)量數(shù)據(jù)(scalar data)的請(qǐng)求、添加,更新,刪除數(shù)據(jù)庫里的記錄等.TableAdapter執(zhí)行的SQL命令要么是某個(gè)特定的SQL statements,比如SELECT columnList FROM TableName;要么是存儲(chǔ)過程.本教程前面部分的TableAdapter使用的是SQL statements.不過很多開發(fā)者和數(shù)據(jù)庫管理員基于安全、便于維護(hù)等方面的考慮,偏愛使用存儲(chǔ)過程;不過也有的人出于靈活

3、性的考慮偏愛使用SQL statement.就我自己而言,我也偏向于存儲(chǔ)過程.在前面的文章,出于簡化的目的我選用的是SQL statements.當(dāng)定義一個(gè)新TableAdapter或添加新方法時(shí),使用TableAdapter的設(shè)置向?qū)?,我們可以很容易的?chuàng)建新的或使用現(xiàn)有的存儲(chǔ)過程.在本文,我們將考察如何使用設(shè)置向?qū)ё詣?dòng)的生產(chǎn)存儲(chǔ)過程。在下一章我們考察如何設(shè)置TableAdapter的方法使用現(xiàn)有的或手動(dòng)創(chuàng)建存儲(chǔ)過程.注意:關(guān)于討論到底使用存儲(chǔ)過程還是使用SQL statements的問題,可參考Rob Howard的博客文章Don't Use Stored Procedures Ye

4、t?( Bouma的博客文章Stored Procedures are Bad, M'Kay?(存儲(chǔ)過程基礎(chǔ)一個(gè)存儲(chǔ)過程由一系列的T-SQL statement組成,當(dāng)調(diào)用該存儲(chǔ)過程時(shí)就執(zhí)行這些T-SQL statement.存儲(chǔ)過程可以接受0到多個(gè)輸入?yún)?shù),返回標(biāo)量值、輸出參數(shù),或最常見的返回SELECT查詢值.注意:存儲(chǔ)過程Stored procedures也經(jīng)常引用為“sprocs” or “SPs”.可以使用T-SQL statement語句CREATE PROCEDURE來創(chuàng)建存儲(chǔ)過程.比如下面的T-SQL腳本創(chuàng)建了一個(gè)名為GetProductsByCategoryID的存儲(chǔ)

5、過程,它有一個(gè)名為 CategoryID的參數(shù),并且將表Products里與CategoryID值相吻合的那條記錄的ProductID, ProductName, UnitPrice,以及Discontinued值返回.?123456789CREATE PROCEDURE GetProductsByCategoryID( CategoryID int)AS SELECT ProductID, ProductName, UnitPrice, DiscontinuedFROM ProductsWHERE CategoryID = CategoryID創(chuàng)建后,我們可以用下面的代碼

6、調(diào)用它:?1EXEC GetProductsByCategory categoryID注意:在下篇文章我們將在Visual Studio IDE集成環(huán)境里創(chuàng)建存儲(chǔ)過程.不過在本文,我們將用TableAdapter向?qū)碜詣?dòng)創(chuàng)建存儲(chǔ)過程.除了返回?cái)?shù)據(jù)外,我們還可以在一個(gè)事務(wù)里用存儲(chǔ)過程執(zhí)行多條數(shù)據(jù)庫命令.比如,假如有一個(gè)名為DeleteCategory的存儲(chǔ)過程,其包含一個(gè)輸入?yún)?shù)CategoryID,并執(zhí)行2個(gè)DELETE statemets,第一個(gè)是刪除相關(guān)的products,第二個(gè)是刪除category。存儲(chǔ)過程里面的多個(gè)statements并不是自動(dòng)的封裝在一個(gè)事務(wù)里的.我們應(yīng)添加額外的

7、T-SQL commands以確保存儲(chǔ)過程里的多條數(shù)據(jù)庫命令當(dāng)成原子操作處理.我們將在后面的內(nèi)容考察如何用事務(wù)來封裝存儲(chǔ)過程的命令.當(dāng)在體系的某個(gè)層使用存儲(chǔ)過程時(shí),Data Access Layer的方法將調(diào)用某個(gè)具體的存儲(chǔ)過程而不是發(fā)出一個(gè)SQL statement命令.這樣一來我們可以發(fā)現(xiàn)、分析發(fā)出的查詢命令.并可以更清楚的看到數(shù)據(jù)庫是如何使用的.有關(guān)存儲(chǔ)過程基本原理的更多信息,可參考本文結(jié)束部分的延伸閱讀.第一步:創(chuàng)建數(shù)據(jù)訪問層高級(jí)場(chǎng)景的Web頁面在開始之前,讓我們花點(diǎn)時(shí)間創(chuàng)建本文及后面幾篇文章要用到的頁面。新建一個(gè)名為AdvancedDAL的文件夾,然后添加如下的ASP.NET頁面,記

8、得使用母版頁Site.master:Default.aspxNewSprocs.aspxExistingSprocs.aspxJOINs.aspxAddingColumns.aspxComputedColumns.aspxEncryptingConfigSections.aspxManagedFunctionsAndSprocs.aspx圖1:添加相關(guān)的頁面像其它文件夾一樣,Default.aspx頁面將列出本部分的內(nèi)容,記得SectionLevelTutorialListing.ascx用戶控件提供了該功能。因此,將其從解決資源管理器里拖放到Default.aspx頁面.圖2:將Sectio

9、nLevelTutorialListing.ascx用戶控件拖到Default.aspx頁面最后,將這些頁面添加到Web.sitemap文件里。特別的,把下面的代碼放在“Working with Batched Data”<siteMapNode>標(biāo)簽后面:?1234567891011121314151617181920212223242526272829303132<siteMapNode url="/AdvancedDAL/Default.aspx" title="Advanced DAL Scenarios" 

10、description="Explore a number of advanced Data Access Layer scenarios.">   <siteMapNode url="/AdvancedDAL/NewSprocs.aspx" title="Creating New Stored Procedures for TableAdapters" description="Learn how to have the TableAdapter wizard

11、automatically create and use stored procedures." /> <siteMapNode url="/AdvancedDAL/ExistingSprocs.aspx" title="Using Existing Stored Procedures for TableAdapters" description="See how to plug existing stored procedures into a TableAdapter.

12、" /> <siteMapNode url="/AdvancedDAL/JOINs.aspx" title="Returning Data Using JOINs" description="Learn how to augment your DataTables to work with data returned from multiple tables via a JOIN query." /> <siteMapNode url="/A

13、dvancedDAL/AddingColumns.aspx" title="Adding DataColumns to a DataTable" description="Master adding new columns to an existing DataTable." /> <siteMapNode url="/AdvancedDAL/ComputedColumns.aspx" title="Working with Computed Columns&qu

14、ot; description="Explore how to work with computed columns when using Typed DataSets." /> <siteMapNode url="/AdvancedDAL/EncryptingConfigSections.aspx" title="Protected Connection Strings in Web.config" description="Protect your conne

15、ction string information in Web.config using encryption." /> <siteMapNode url="/AdvancedDAL/ManagedFunctionsAndSprocs.aspx" title="Creating Managed SQL Functions and Stored Procedures" description="See how to create SQL functions and stored pro

16、cedures using managed code." /></siteMapNode>更新Web.sitemap文件后,花點(diǎn)時(shí)間在瀏覽器里查看,左邊的菜單將包括本部分的內(nèi)容.圖3:網(wǎng)站地圖現(xiàn)在包含了不部分的頁面第二步:設(shè)置TableAdapter創(chuàng)建新的存儲(chǔ)過程我們?cè)?App_Code/DAL文件夾里創(chuàng)建一個(gè)類型化的DataSet,名稱為NorthwindWithSprocs.xsd.由于我們?cè)谝郧暗慕坛汤镆呀?jīng)詳細(xì)探討了創(chuàng)建細(xì)節(jié),因此我們這里一筆帶過,如果你想知道詳細(xì)的創(chuàng)建過程請(qǐng)參閱前面的第1章創(chuàng)建一個(gè)數(shù)據(jù)訪問層在DAL文件夾上右擊鼠標(biāo)選“添加新項(xiàng)”,

17、選DataSet模板,如圖4所示.圖4:新建一個(gè)名為NorthwindWithSprocs.xsd的數(shù)據(jù)集這樣將會(huì)創(chuàng)建一個(gè)新的類型化的DataSet,打開設(shè)計(jì)器,創(chuàng)建一個(gè)新的TableAdapter,展開TableAdapter設(shè)置向?qū)?向?qū)У牡谝徊绞亲屛覀冞x擇要連接的數(shù)據(jù)庫.在下拉列表里有一個(gè)連接到Northwind數(shù)據(jù)庫的連接字符串,選中它,再點(diǎn)下一步。接下來的界面讓我們選擇TableAdapter以哪種方式訪問數(shù)據(jù)庫.在以前的教程里我們選擇的是“Use SQL statements”,不過在本文我們選第二項(xiàng):“Create new stored procedures”,點(diǎn)下一步.圖5:設(shè)

18、置TableAdpater創(chuàng)建新的存儲(chǔ)過程接下來,我們要指定主查詢(main query).我們將創(chuàng)建一個(gè)存儲(chǔ)過程來包含SELECT查詢.使用下面的SELECT查詢:?1234SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, DiscontinuedFROM Products圖6:鍵入SELECT查詢注意:在名為Northwind的數(shù)據(jù)集里的ProductsTableAdapt

19、er的主查詢與上面本文定義的主查詢有所不同。那個(gè)主查詢還返回了每個(gè)產(chǎn)品的category名稱和company名稱.不過在后面的文章我們將對(duì)本文的TableAdapter添加這些相關(guān)的代碼.再點(diǎn)“Advanced Options”按鈕.我們可以指定是否讓向?qū)門ableAdapter自動(dòng)生成insert, update和delete statements;是否使用開發(fā)式并發(fā)操作(optimistic concurrency);是否完成inserts 和 update操作后刷新數(shù)據(jù)表.在默認(rèn)情況下,自動(dòng)選中“Generate Insert, Update and Delete statements

20、”選項(xiàng)。另外,本文不用選擇“Use optimistic concurrency”項(xiàng).當(dāng)選擇自動(dòng)創(chuàng)建存儲(chǔ)過程時(shí),“Refresh the data table”項(xiàng)將被忽略掉.不管是否選中該項(xiàng),最終的insert 和update存儲(chǔ)過程都會(huì)檢索剛添加或剛更新(just-inserted or just-updated record)的記錄,我們將在第三步看到.圖7:選中“Generate Insert, Update and Delete statements”項(xiàng)注意:當(dāng)選中“Use optimistic concurrency”項(xiàng)的時(shí)候,向?qū)?huì)在WHERE語句里添加額外的條件,當(dāng)其它列的值發(fā)生

21、改動(dòng)的話,將阻止數(shù)據(jù)更新.關(guān)于使用TableAdapter內(nèi)置的optimistic concurrency功能請(qǐng)參閱第21章實(shí)現(xiàn)開放式并發(fā)輸入SELECT主查詢并選取“Generate Insert, Update and Delete statements”項(xiàng)后,點(diǎn)下一步,接下來的界面,如圖8所示,讓我們?yōu)閟electing, inserting, updating, 和deleting數(shù)據(jù)的存儲(chǔ)過程命名.將這些存儲(chǔ)過程的名字改為Products_Select, Products_Insert, Products_Update, 和Products_Delete.圖8:為存儲(chǔ)過程重命名向?qū)?/p>

22、創(chuàng)建了4個(gè)存儲(chǔ)過程,點(diǎn)“Preview SQL Script”按鈕,你可以在Preview SQL Script 對(duì)話框里將腳本保存在一個(gè)文件里或復(fù)制到剪貼板.圖9:預(yù)覽生成的存儲(chǔ)過程對(duì)存儲(chǔ)過程重命名后,點(diǎn)下一步,對(duì)TableAdapter相應(yīng)的方法命名.就像使用SQL statements一樣,我們可以創(chuàng)建方法來填充一個(gè)現(xiàn)有的DataTable或返回一個(gè)新的DataTable;我們也一個(gè)指定TableAdapter是否采用DB-Direct模式來插入、更新、刪除記錄.全選這3項(xiàng),只不過將Return a DataTable方法重命名為GetProducts,如圖10所示:圖10:將方法重命名

23、為Fill 和GetProducts點(diǎn)Next總覽向?qū)?zhí)行的步驟.點(diǎn)Finish按鈕完成設(shè)置.一旦向?qū)ЫY(jié)束后,將返回DataSet設(shè)計(jì)器,它此時(shí)將包括ProductsDataTable.圖11:DataSet設(shè)計(jì)器將顯示剛剛添加的ProductsDataTable第三步:考察剛剛創(chuàng)建的存儲(chǔ)過程我們?cè)诘诙嚼镉孟驅(qū)?chuàng)建了選擇、插入、更新、刪除數(shù)據(jù)的存儲(chǔ)過程.這些存儲(chǔ)過程可以通過Visual Studio查看或修改.打開服務(wù)器資源管理器,點(diǎn)到數(shù)據(jù)庫的存儲(chǔ)過程文件夾。如圖12所示,Northwind數(shù)據(jù)庫包含了4個(gè)新的存儲(chǔ)過程,Products_Delete, Products_Insert, P

24、roducts_Select, and Products_Update.圖12:可以在Stored Procedures文件夾里找到我們創(chuàng)建的4個(gè)存儲(chǔ)過程注意:如果你看不到服務(wù)器資源管理器,點(diǎn)“View”菜單,選Server Explorer項(xiàng).如果你無法找到新創(chuàng)建的存儲(chǔ)過程,右擊Stored Procedures文件夾,選“刷新”.要查看或修改某個(gè)存儲(chǔ)過程,在服務(wù)器資源管理器里雙擊其名字或右擊該存儲(chǔ)過程,選”打開“。如13顯示的是打開Products_Delete存儲(chǔ)過程的畫面.圖13:可以在Visual Studio里打開并修改存儲(chǔ)過程Products_Delete和Products_Se

25、lect存儲(chǔ)過程的內(nèi)容很好理解。比如下面的代碼構(gòu)成了Products_Insert存儲(chǔ)過程.?1234567891011121314151617181920212223ALTER PROCEDURE dbo.Products_Insert( ProductName nvarchar(40), SupplierID int, CategoryID int, QuantityPerUnit nvarchar(20), UnitPrice money, UnitsInStock smallint, UnitsOnOrder sma

26、llint, ReorderLevel smallint, Discontinued bit)AS SET NOCOUNT OFF;INSERT INTO Products (ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued)VALUES (ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice,

27、 UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued);  SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, DiscontinuedFROM ProductsWHERE (ProductID = SCOPE_IDENTITY()在TableAdapter向?qū)Ю锒x的SELECT查詢返回Product

28、s表里的列,這些列又作為存儲(chǔ)過程的輸入?yún)?shù)并運(yùn)用到INSERT statement中.緊接著的是一個(gè)SELECT查詢,返回Products表里最新添加的記錄的各列的值(包括ProductID)。當(dāng)使用Batch Update模式添加一個(gè)新記錄時(shí),刷新功能是很有用的。因?yàn)樗鼘⒆钚绿砑拥腜roductRow instances實(shí)例的ProductID屬性賦值為數(shù)據(jù)庫指派的自增值.下面的代碼說明了該功能.代碼創(chuàng)建了基于NorthwindWithSprocs數(shù)據(jù)集的ProductsTableAdapter以及ProductsDataTable。要向數(shù)據(jù)庫添加一個(gè)新的產(chǎn)品,我們要?jiǎng)?chuàng)建一個(gè)Products

29、Row instance實(shí)例,對(duì)其賦值,并調(diào)用TableAdapter的Update方法,再傳遞給ProductsDataTable.在內(nèi)部,TableAdapter的Update方法遍歷傳遞給DataTable的所有ProductsRow instance實(shí)例(在本例,只有一個(gè)。因?yàn)槲覀冎惶砑恿艘粋€(gè)產(chǎn)品),并執(zhí)行相應(yīng)的insert, update, 或delete命令。此時(shí),執(zhí)行Products_Insert存儲(chǔ)過程,其向Products表添加一條新記錄,并返回該記錄的詳細(xì)信息,然后更新ProductsRow instance實(shí)例的ProductID值。Update方法完成后,我們就可以通過

30、ProductsRow的ProductID屬性訪問新添加記錄的ProductID值了.?1234567891011121314151617181920/ Create the ProductsTableAdapter and ProductsDataTableNorthwindWithSprocsTableAdapters.ProductsTableAdapter productsAPI = new NorthwindWithSprocsTableAdapters.ProductsTableAdapter();NorthwindWithSprocs.ProductsDataTable

31、 products = new NorthwindWithSprocs.ProductsDataTable(); / Create a new ProductsRow instance and set its propertiesNorthwindWithSprocs.ProductsRow product = products.NewProductsRow();product.ProductName = "New Product"product.CategoryID = 1; / Beveragesproduct.Discontinued = fals

32、e; / Add the ProductsRow instance to the DataTableproducts.AddProductsRow(product); / Update the DataTable using the Batch Update patternproductsAPI.Update(products); / At this point, we can determine the value of the newly-added record's ProductIDint newlyAddedProductIDValue = pr

33、oduct.ProductID;類似的,Products_Update存儲(chǔ)過程的UPDATE statement后面也包含一個(gè)SELECT statement,如下:?12345678910111213141516171819202122232425262728ALTER PROCEDURE dbo.Products_Update( ProductName nvarchar(40), SupplierID int, CategoryID int, QuantityPerUnit nvarchar(20), UnitPrice money,

34、60;UnitsInStock smallint, UnitsOnOrder smallint, ReorderLevel smallint, Discontinued bit, Original_ProductID int, ProductID int)AS SET NOCOUNT OFF;UPDATE ProductsSET ProductName = ProductName, SupplierID = SupplierID, CategoryID = CategoryID, QuantityPerUnit = Quan

35、tityPerUnit, UnitPrice = UnitPrice, UnitsInStock = UnitsInStock, UnitsOnOrder = UnitsOnOrder, ReorderLevel = ReorderLevel, Discontinued = DiscontinuedWHERE (ProductID = Original_ProductID);  SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPri

36、ce, UnitsInStock, UnitsOnOrder, ReorderLevel, DiscontinuedFROM ProductsWHERE (ProductID = ProductID)我們注意到該存儲(chǔ)過程有2個(gè)關(guān)于ProductID的參數(shù),即Original_ProductID 和ProductID,這樣以來我們就可以對(duì)主鍵值進(jìn)行改動(dòng)了.舉個(gè)例子:有一個(gè)employee(雇員)數(shù)據(jù)庫,每條employee記錄都用雇員的社保號(hào)碼作為其主鍵值.要想更改某條記錄的社保號(hào)碼,必須提供新的號(hào)碼以及原始號(hào)碼.不過對(duì)Products表來說用不著,因?yàn)榱蠵roductID是一個(gè)唯一標(biāo)識(shí)列(ID

37、ENTITY column),不應(yīng)對(duì)其更改.實(shí)際上,Products_Update存儲(chǔ)過程里的UPDATE statement并沒有包含ProductID列,因此,如果在UPDATE statement的WHERE字句里使用Original_ProductID的話,顯得多此一舉,而應(yīng)該使用ProductID參數(shù).當(dāng)更新某個(gè)存儲(chǔ)過程的參數(shù)時(shí),TableAdapter里所有那些調(diào)用該存儲(chǔ)過程方法都應(yīng)該進(jìn)行更新.第四步:修改存儲(chǔ)過程的參數(shù)并更新TableAdapter由于Original_ProductID參數(shù)是多余的,讓我們將其從Products_Update存儲(chǔ)過程里完全清除.打開Product

38、s_Update存儲(chǔ)過程,刪除Original_ProductID參數(shù),在UPDATE statement的WHERE字句里將Original_ProductID改為ProductID. 完成上述修改后,該存儲(chǔ)過程里的T-SQL看起來應(yīng)該和下面的差不多:?1234567891011121314151617181920212223242526ALTER PROCEDURE dbo.Products_Update( ProductName nvarchar(40), SupplierID int, CategoryID int, QuantityPerUni

39、t nvarchar(20), UnitPrice money, UnitsInStock smallint, UnitsOnOrder smallint, ReorderLevel smallint, Discontinued bit, ProductID int)AS SET NOCOUNT OFF;UPDATE Products SET ProductName = ProductName, SupplierID = SupplierID, CategoryID = CategoryID, QuantityPe

40、rUnit = QuantityPerUnit, UnitPrice = UnitPrice, UnitsInStock = UnitsInStock, UnitsOnOrder = UnitsOnOrder, ReorderLevel = ReorderLevel, Discontinued = DiscontinuedWHERE (ProductID = ProductID);  SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, Unit

41、Price, UnitsInStock, UnitsOnOrder, ReorderLevel, DiscontinuedFROM ProductsWHERE (ProductID = ProductID)按Ctrl+S或點(diǎn)工具欄里的“保存”圖標(biāo),保存更改.此時(shí),Products_Update存儲(chǔ)過程不會(huì)執(zhí)行Original_ProductID參數(shù),但TableAdapter仍然會(huì)傳遞該參數(shù).要想查看TableAdapter傳遞給Products_Update存儲(chǔ)過程的參數(shù),你可以在設(shè)計(jì)器里選中TableAdapter,轉(zhuǎn)到屬性窗口,點(diǎn)更新命令的參數(shù)集(UpdateCommand'sP

42、arameters collection)里的橢圓型區(qū)域,這樣將轉(zhuǎn)到Parameters Collection Editor對(duì)話框,如圖14所示:圖14:對(duì)話框里列出了傳遞給Products_Update存儲(chǔ)過程的參數(shù)要?jiǎng)h除參數(shù),只需選中它,再點(diǎn)Remove按鈕.要刷新參數(shù)的話,你也可以在設(shè)計(jì)器里選中TableAdapter,點(diǎn)右鍵選“設(shè)置”,這將會(huì)開啟TableAdapter設(shè)置向?qū)?,它列出了用于select, insert, updat和delete的存儲(chǔ)過程,并列出了這些存儲(chǔ)過程的輸入?yún)?shù).如果你在Update下拉列表里選Products_Update的話,你可以看到該存儲(chǔ)過程包含的輸入

43、參數(shù)里已經(jīng)沒有包含Original_ProductID了(見圖15),點(diǎn)Finish將對(duì)TableAdapter使用的參數(shù)集自動(dòng)更新. 圖15:你可以通過使用TableAdapter的設(shè)置向?qū)硭⑿聟?shù)集第五步:添加額外的TableAdapter方法我們?cè)诘诙秸f過,當(dāng)創(chuàng)建一個(gè)新的TableAdapter時(shí),很容易自動(dòng)地生成相應(yīng)的存儲(chǔ)過程,同樣我們也可以向TableAdapter添加額外的方法.作為演示,讓我們向ProductsTableAdapter添加一個(gè)方法GetProductByProductID(productID),該方法將一個(gè)ProductID作為輸入?yún)?shù),并返回該產(chǎn)品

44、的詳細(xì)信息.在ProductsTableAdapter上點(diǎn)擊右鍵,選擇“添加查詢”.圖16:向TableAdapter添加新查詢這將開啟TableAdapter查詢?cè)O(shè)置向?qū)?。首先,向?qū)⒃儐栆院畏N方式訪問數(shù)據(jù)庫,我們將創(chuàng)建一個(gè)新的存儲(chǔ)過程,因此選“Create a new stored procedure”,再點(diǎn)Next.圖17:選中“Create a new stored procedure”項(xiàng)接下來,向?qū)г儐栁覀儓?zhí)行哪種查詢,是返回一系列行?一個(gè)標(biāo)量值?又或者執(zhí)行UPDATE, INSERT,或 DELETE statement.由于GetProductByProductID(produc

45、tID)方法將返回一行,我們選擇“SELECT which returns row”項(xiàng),再點(diǎn)Next.圖18:選擇“SELECT which returns row” 項(xiàng)接下來的界面將展示TableAdapter的主查詢,其僅僅列出了存儲(chǔ)過程的名字(也就是dbo.Products_Select).將其刪除,替換為如下的SELECT statement,它返回某個(gè)具體產(chǎn)品的所有列.?12345SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, U

46、nitsOnOrder, ReorderLevel, DiscontinuedFROM ProductsWHERE ProductID = ProductID圖19:將存儲(chǔ)過程的名字替換為一個(gè)SELECT查詢.接下來要對(duì)創(chuàng)建的存儲(chǔ)過程命名,輸入Products_SelectByProductID,點(diǎn)Next.圖20:將新存儲(chǔ)過程命名為Products_SelectByProductID最后一步將要我們對(duì)自動(dòng)生成的名字重新命名,并指定是否使用Fill a DataTable模式、是否使用Return a DataTable模式,抑或這2種模式都采用.就本文而言,都選中這2項(xiàng)并將方法重命

47、名為FillByProductID 和 GetProductByProductID.點(diǎn)Next,再點(diǎn)Finish完成設(shè)置向?qū)? 圖21:將TableAdapter的方法重命名為FillByProductID 和 GetProductByProductID完成向?qū)Ш?,TableAdapter將包含一個(gè)新的可用方法GetProductByProductID(productID),當(dāng)調(diào)用該方法時(shí),將執(zhí)行我們剛剛創(chuàng)建的Products_SelectByProductID存儲(chǔ)過程.花點(diǎn)時(shí)間在服務(wù)器資源管理器里查看該存儲(chǔ)過程,點(diǎn)Stored Procedures文件夾,并打開Products_S

48、electByProductID(如果你沒看到它,在Stored Procedures文件夾上右擊鼠標(biāo),選“刷新”).請(qǐng)注意,SelectByProductID存儲(chǔ)過程將ProductID作為輸入?yún)?shù),并執(zhí)行我們?cè)谙驅(qū)Ю镙斎氲腟ELECT Statement,如下:?123456789101112ALTER PROCEDURE dbo.Products_SelectByProductID( ProductID int)AS SET NOCOUNT ON; SELECT ProductID, ProductName, SupplierID, CategoryID,&

49、#160;QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, DiscontinuedFROM ProductsWHERE ProductID = ProductID第六步:創(chuàng)建一個(gè)業(yè)務(wù)邏輯層類在我們打算從表現(xiàn)層訪問產(chǎn)品前,我們首先需要為新添加的數(shù)據(jù)集創(chuàng)建一個(gè)BLL class,在/App_Code/BLL文件夾里創(chuàng)建一個(gè)ProductsBLLWithSprocs.cs文件,如下:?1234567891011121314151617181920212223242526272829303132333

50、435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551

51、56using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using NorthwindWithSprocsTableAdapters; System.ComponentModel.Data

52、Objectpublic class ProductsBLLWithSprocs private ProductsTableAdapter _productsAdapter = null; protected ProductsTableAdapter Adapter  get   if (_productsAdapter = null)  _productsAdapter = new ProductsTableAdapter();   return _productsAdap

53、ter;    System.ComponentModel.DataObjectMethodAttribute (System.ComponentModel.DataObjectMethodType.Select, true) public NorthwindWithSprocs.ProductsDataTable GetProducts()  return Adapter.GetProducts();    System.ComponentModel.DataObjectM

54、ethodAttribute (System.ComponentModel.DataObjectMethodType.Select, false) public NorthwindWithSprocs.ProductsDataTable GetProductByProductID(int productID)  return Adapter.GetProductByProductID(productID);    System.ComponentModel.DataObjectMethodAttribute

55、0;(System.ComponentModel.DataObjectMethodType.Insert, true) public bool AddProduct (string productName, int? supplierID, int? categoryID,  string quantityPerUnit, decimal? unitPrice, short? unitsInStock,  short? unitsOnOrder, short? reorderLevel, bool discontinued) 

56、; / Create a new ProductRow instance NorthwindWithSprocs.ProductsDataTable products =  new NorthwindWithSprocs.ProductsDataTable(); NorthwindWithSprocs.ProductsRow product = products.NewProductsRow();  product.ProductName = productName; if (supplierID = null)&

57、#160; product.SetSupplierIDNull(); else  product.SupplierID = supplierID.Value; if (categoryID = null)  product.SetCategoryIDNull(); else  product.CategoryID = categoryID.Value; if (quantityPerUnit = null)  product.SetQuantityPerUnitNu

58、ll(); else  product.QuantityPerUnit = quantityPerUnit; if (unitPrice = null)  product.SetUnitPriceNull(); else  product.UnitPrice = unitPrice.Value; if (unitsInStock = null)  product.SetUnitsInStockNull(); else  product.Units

59、InStock = unitsInStock.Value; if (unitsOnOrder = null)  product.SetUnitsOnOrderNull(); else  product.UnitsOnOrder = unitsOnOrder.Value; if (reorderLevel = null)  product.SetReorderLevelNull(); else  product.ReorderLevel = reorderLevel.Value; product.Discontinued = discontinued;  / Add the new product products.AddProductsRow(product); 

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論