利用IBM DB2数据库建立ASP NET网站毕业论文外文翻译Word文档格式.docx
《利用IBM DB2数据库建立ASP NET网站毕业论文外文翻译Word文档格式.docx》由会员分享,可在线阅读,更多相关《利用IBM DB2数据库建立ASP NET网站毕业论文外文翻译Word文档格式.docx(18页珍藏版)》请在冰豆网上搜索。
ASP.NETisthenext-generationWebapplicationframeworkbuiltonthe.NETplatform--thesuccessortoMicrosoft'
sActiveServerPages(ASP)framework.ASP.NETallowsrapidapplicationdevelopment(RAD)ofWebapplicationsinasimilarwaytoWindowsFormapplications.CreatingWindows-styleWebapplicationsonthe.NETplatformisalsoknownasWebForms.
TobeginwithWebForms,yourcomponentsneedtobeplacedontoaWebFormand,liketypicalWindowsdevelopment,yousetthepropertiesforthecomponentandwritetheeventhandlersforyourcomponents.Inthisway,developingWebapplicationsisextremelysimple.
AccessingDB2fromBorlandC#Builder--theBorlandDataProviders(BDP)
CreatingdynamicInternetapplicationsiseasyenough,butinmostcasesyouwillwanttodevelopdata-drivenapplicationsconnectingtoyourDB2UDBdatabase.ThisiswheretheBDPcomponentscomein.Figure1showsthebasiccomponentsthatyouneedtoconnecttothedata.AllthecomponentsyouneedtoconnectthroughtoDB2UDBcanbefoundontheBDPandDataComponentspagesoftheToolPalette.
Figure1.SelectingtheBDPcomponents
DB2UDBoffersscalability,robustness,easyconfiguration,and--mypersonalfavorite--theabilitytocreatestoredproceduresinbothSQLandJava,soitisveryeasytoconnecttoitwiththeBDPcomponents.ThemaincomponentsthatyouwillusetoconnecttothedatabasearetheBdpConnectioncomponent,theBdpDataAdaptercomponent,theBdpCommand,andtheDataSetcomponent:
Figure2.TheBDPcomponentsworktogetherforyourdata.
DataSetscontainacollectionoftables,therelationshipsbetweenthetables,andotherinformationsuchastheconstraintsforthedata.EachqueryretrievedbytheBdpDataAdapterisstoredasaDataTable,anditisthisDataTableobjectthatholdsthedatafromthedatabase.Forsimplicity,wewillworkwithasingleDataSetthatcontainsoneDataTable.
TheDataSetcomponent,however,isdesignedtoworkwithoutapermanentconnectiontothedatabase.TheprocessofretrievingdatafromtheBDPcomponentsintotheDataSetistorequestthattheBdpDataAdapterconnecttothedatabase,andthenrequestthattheBdpDataAdapterfillthecontentsoftheDataSetwiththeresultsofoneormorequeries.OnceyourDataSetispopulated,youcandisconnectfromthedatabase.Wheneveryouneedtoretrieveorupdatedatafromthedatabase,yousimplyreconnecttotheBdpDataAdapter.Thatway,ifnetworkconnectivitytoDB2UDBislostyoucanstillworkwithalocalcopyofthedata,inpreparationforwhenaccesstotheserverisavailable.
StartingtobuildyourWebApplication
Let'
sstartondevelopingaWebgriddisplayingtheresultsofasingledatabasequery.First,youwillneedtocreateanewWebApplication.FromtheC#BuilderIDE,selectFile|New|OtherandthenselectanewASP.NETWebApplication.
Afteryouhaveselectedtheoptionsforyourapplication,youhaveaWebFormthatyoucanworkwith.Indesignmode,youcansetupthedisplayasifyouwereeditinganHTMLdocumentinaWYSIWYGeditor(seeFigure3).
ThemagicreallystartshappeningwhenyoustartdroppingtheASP.NETcomponentsontoyourWebform.TheASP.NETcomponentsareeasilydistinguishablefromotherelementsonthepagebecausetheynormallyhave'
asp'
paintedonthetopleftcornerofthecontrols.Tobeginworkingwithdata,placeaDataGridcomponent,aBdpConnectioncomponent,andaBdpDataAdapteronthedesigner:
Figure3.AsamplesetupforanewWebform
Toconnecttoyourdatabase,right-clicktheBdpConnectionandselectConnectionEditor.Fromthismenu,selecttheAddbuttontoaddadataconnection.Thenselectthedatabaseyouwanttoconnecttoandcreateanametorepresenttheconnectiontothedatabase.Onmysystem,IamreturningalltheentriesofatablecalledPRODUCTLISTonmyDB28.1PersonaldatabasecalledSHOPPING(seeFigure4fortheconnectionparametersfortheBdpConnection).HereistheDDLforPRODUCTLIST:
CREATETABLE"
GLENN"
."
PRODUCTLIST"
(
"
PRODUCTCODE"
VARCHAR(20)NOTNULL,
PRODUCTNAME"
VARCHAR(50)NOTNULL,
DESCRIPTION"
VARCHAR(255),
UNITPRICE"
DOUBLENOTNULL,
CATEGORYCODE"
INTEGER,
IMAGEURL"
CHAR(150))
IN"
USERSPACE1"
;
COMMENTONTABLE"
IS'
AlistofProductsintheShoppingCart'
;
ALTERTABLE"
ADDCONSTRAINT"
CC1053568050795"
PRIMARYKEY
("
);
Aftercreatingthesettings,youwillalsoneedtoprovidetheconnectionsettingsforthedatabaseconnection.Oncetheconnectionsettingsaredefined,youshouldbeabletoclickontheTestbuttonintheConnectionsEditortofindoutifyoucanconnectsuccessfullyornot.
Figure4.TheConnectionseditorformallowsyoutopre-defineyourconnectiontothedatabase
Oncethesettingsaredefinedandyoucanconnecttothedatabase,youarereadytosetuptheBdpDataAdaptercomponent.LookingbackatFigure2,youcanseetherelationshipbetweentheBdpDataAdaptercomponentandtheBdpConnectionandtheBdpCommandobjects.
TheBdpDataAdapteristhepowerbrokerforworkingwiththeBDPcomponents.TheBdpDataAdapterconnectstothedatabaseviatheBdpConnectioncomponent.TheBdpDataAdaptercomponentalsohasfourBdpCommandproperties--SelectCommand,InsertCommand,UpdateCommand,andDeleteCommand.TheseBdpCommandsareusedtoperformtheretrievalandupdateoperationsagainstthedatabase.ThecommandthatrepresentstheSelectCommandisusedtoselectallthedatausedtopopulateaDataTable,whereastheotherthreeBdpCommandsareusedtorepresentthedatabaseoperationstoinsert,update,anddeleteasingleDataTablerow.
ThebasicconceptbehindtheBDPcomponentsisthattheBdpDataAdapterwillfilltheDataSetwithcontentfromyourdatabasesothatyoucanworkwiththeDataSet.TheDataSetshouldthenbedisconnectedfromtheBDPcomponents.WhentheDataSetneedstoretrievemoredataorupdatedata,itconnectsthroughtotheBdpDataAdapterwhenneeded.NowthatyouhavethebasicideaaboutthedataconnectivitytoDB2usingtheBDPcomponents,let'
sstartconnectingyourapplicationtosomedata.
PopulatingtheDataGrid
Todisplaythispresentation,usetheDataGridcomponentthatyouplacedontheWebForm.TobeabletoworkwiththeBdpDataAdapterindesigntime,youwillneedtosetActiveontheBdpDataAdaptercomponenttoTruesothatthegridwillbeabletoretrievetheschemainformationofthequerythatyouwillbeusing.
OncetheBdpDataAdapterisactive,selecttheDataGrid'
sPropertyBuilderoption.(Youcanfindthisoptionunderthepropertieslistinginthepropertyeditor.)YouwillbepresentedwithadialogthatdefinesthedataconnectivityandalsothedisplayaspectsoftheDataGrid.
Figure7.DefiningtheDataSet,theDataMember,andtheprimarykeyfortheDataMember
ThefirstoftheDataGrid'
sConfigurationoptionsdefinestheconnectiondetailstothedata,suchaswhichDataSetyouwillbeusingandwhichDataMember(DataTableorDataView)oftheDataSetyouwanttoview.YoualsoneedtodefinewhichfieldintheDataMemberisusedasthekeyfieldforthatDataView.
Atthispoint,thisisallyouneedtosetupthedatagrid.Inmostcases,though,youwillalsowanttodefinethecolumnsthatwillbedisplayedonthegrid.ByselectingtheColumnspagefortheDataGrid(seeFigure8),youcandefinewhichcolumnsyouwantdisplayed,altertheorderofthecolumns,andcustomizetheformattingofthecolumnheaderandofthecolumndata.Inmyexample,Iamjustrenamingtheheadersforeachofthefields,andstatingthattheUNITPRICEfieldshouldbedisplayedasacurrency.
Figure8.Customizingandformattingthecolumns
Nowallthatstandsbetweenyouandadata-drivenWebpageisafewlinesofcodethattelltheWebpagetobindthedatafromtheDataSettotheDataGrid.InASP.NET,componentswithdataconnectivityarenotconnectedbydefault.Instead,youmusttellthecodethatyouwanttobindtothedata.SointheWebForm'
sPage_Loadevent,usetheDataBindmethodoftheDataGridtotellittoconnectthroughtothedata:
privatevoidPage_Load(objectsender,System.EventArgse)
{
if(!
IsPostBack){
dataGrid1.DataBind();
}
}
ASP.NETusesamechanismcalledPostBacktomanagethestateoftheWebform.ThefirsttimeaWebpageisrequested,youwillinitializethepropertiesofyourWebForm.Whentheend-userconnectswiththedata,theyworkwiththesamepropertiesthathavebeenset,sothedataonlyneedstobeboundtothegridthefirsttimethepageisloaded.
OnceyouhaveaddedthecodetothePage_Loadevent,youarereadytorunyourWebapplication.Afterrunningthepage,youwillhaveaprofessional-lookingWebpagethatcontainsdynamicWebdata:
Figure9.YourDB2datadisplayedinyourWebbrowser
DataBinding--beyondthegrid
Nowthatyou'
vehadatasteofdatabinding,you'
regoing