Tải bản đầy đủ - 0 (trang)
Chapter 11: Databases and ADO.NET: The Spy Database

Chapter 11: Databases and ADO.NET: The Spy Database

Tải bản đầy đủ - 0trang

Chapter11:DatabasesandADO.NET:TheSpy

Database



Overview

AsyouhavegrownmoreexperiencedinC#,youhaveseenthe

importanceofdataforcreatinginterestingprograms.Oftenyouwill

findthatyouhavealargeamountofdatathathasalreadybeen

generatedinsomesortofdatabasemanagementpackage,suchas

Access,Oracle,orSQLServer.C#providesasetofclassesfor

workingwithexistingdatabases.Italsoprovidesveryhandytoolsfor

creatingadatabase.Inthischapteryouwillgetexposuretothe

complexandpowerfulworldofdatamanipulationandlearnto

Buildasimpledatabaseusingthebuilt-indatamanagement

tools

Createaformthatusesdataconnections,dataadapters,and

datasetstoaccessdata

Usebasicdatanormalizationprinciplestodesignmulti-table

databases

Buildrelationshipsandviewsformanagingcomplex

databases

ConnecttovarioustypesofdatabasesandrawXMLdataasa

datasource

Updatedataonthefly

ANoteabouttheCD-ROM

Thischapterfocusesontechniquesforbuildingdatabasesonyour

ownserver.TheexamplesinthischapteroutlinehowIdesigneda

datasystemonmyownserver,buttheprogramsontheCD-ROM

willnotrununlessyoubuildthedatabasesonyourmachine.Inthe

eventthatyoudon’twanttobuildthedatabase,Ihaveincludedan

Accessdatabase(spy.mdb)ontheCD-ROMthatyoucanconnect

tousingthetechniquesoutlinedinthesection,“WorkingwithOther



Databases.”











IntroducingtheSpyMasterProgram

Toillustratethedatabasegenerationfeatures,youwillbuildasimple

databaseusedtomanageyourinternationalnetworkofspies(youdo

haveaninternationalspynetwork,don’tyou?).

TheSpyMasterprogrammainscreenisillustratedinFigure11.1.



Figure11.1:Fromthis

mainscreenyoucanachieveworlddominance(orprotectitfrom

evil).

Themainscreenfeaturesfamiliarbuttonsthattakeyoutoother

forms.Toviewyouragents,clickonthe(whatelse?)ViewAgents

button.YouwillseetheformshowninFigure11.2.





Figure11.2:Alltheinformationregardingaspyisavailableonthis

form.

Youcanchooseanyagentinyourrosterfromthedrop-downlistatthe

topoftheform.

Becauseyouarethespymasteryoualsocanchangespyinformation

atwill,lettingslipthedogsofinternationalintrigue,asshowninFigure

11.3.





Figure11.3:Theeditagentsscreenissimilartotheviewagents

screen,exceptthatnowyoucanchangesomeofthe

data.

Youcanchangealotfromtheeditagentscreen,butyoucan’tchange

everythingfromthisscreen,becausedesigningdaringmissionsof

dangerandcreatingspecialtiesaredifferentthanmanipulatingyour



pawnsonthegrandchesstable(evillaughter).Thereisaseparate

formforeditingmissions,showninFigure11.4.





Figure11.4:Heretheusercanaddandmodifyassignmentsina

grid.

Finally,youcanaddtothespecialtiesyouragentscanpursue.





Figure11.5:Theusercanaddandmodifyspecialties.Younever

knowwhenexplosivesanddoilieswillbeneededona

mission.









CreatingaSimpleDatabase

Itpaystostartsmall.You’llbeginwithamoremodestversionofthe

spydatabase.Althoughyoucanwriteprogramstogeneratea

database,it’softeneasiertouseadatabasemanagementsystemto

createtheoriginaldatabase.Microsoftincludesaverypowerful

subsetoftheSQLServerdatabaseprogramaspartofthevisual

studioeditor.IfyouhaveanyexperiencewithMicrosoftaccess(or

prettymuchanyotherrelationaldatabasepackage)you’llfindthe

toolsfordatadesigntobeprettystandard.

Hint NotallinstallationsofVisualStudio.NETincludetheSQL

Serverextensions.Ifyoudonothavethisfunctionality,you

canfollowthesamestepsoutlinedinthischapterin

MicrosoftAccessornearlyanyothermoderndatabase

program,andthenyoucanusethestepsoutlinedinthe

“WorkingwithOtherDatabases”sectionlaterinthischapter

toconnecttothedatabase.Nearlyeverymoderndata

packagewillhavesomevariationofthetoolsIdescribein

thissection,althoughtheexacttechniquesforaccessing

thesetoolsvaries.



AccessingtheDataServer

ThetoolbartabontheleftsideoftheIDEhasbeenhidingsome

importantsecretsfromyou.TheServerExplorertabatthebottom-left

ofthetoolbarbringstofocusanentirelynewsetoftoolsyoumight

neverhaveseenbefore.Beingtheinternationalspymasteryouare,

youcanmasterthesenewtoolsquickly.Theserverexplorerenables

youtoexaminemanykindsoftoolsavailableontheserverorservers

yourcomputeriscurrentlyconnectedto.Thetermserverisusedto

describeacomputerthatprovidesinformation,andalsotothe

softwareonthecomputerthatdistributesinformation.Youmaybe

familiarwithnetworkservers,whichprovideafoundationforalocal

areanetwork,orWebservers,whichiswhereWebpagesarestored

andaccessed.Therearemanyothertypesofserversavailableas

well,includingdataservers,whichareusedtomanagedatabases.



WhenyouinstalledVisualStudio,asimplifiedversionofSQLServer,

Microsoft’spowerfuldataserver,wasalsoinstalledonyourmachine.

YoucanusethisrelationaldatabasepackagewithintheVisualStudio

IDEtodevelopyourowndatabases,andintegratethemintoyour

programs.



CreatingaNewDatabase

Ifyoulookattheserverstag,you’llseealistofserversyouare

connectedto.Asadefault,onlyyourownmachinenameislisted.

Hint WhenIinstalledWindows2000onmyworkmachine,Iwent

withthedefaultmachinenamethatMicrosoftsuggested.

Thatwasareallybadidea,becauseIcanhardlyeventype

thatmonster,letalonerememberit.Inthefollowingcode

andscreenshots,replaceandy-mpecr6vc86withyour

ownmachine’sname.Ifyouhaven’tyetinstalledWindows

2000orXP,makesureyouchooseamachinenamethat’s

easiertorememberthanmine.

Myserverislisted,soIclickedonthecorrespondingplussigntosee

thevariouspartsoftheserverthatareavailable.SQLserversare

nearthebottomofthelist.SQL(StructuredQueryLanguage)isthe

nameofapowerfuldatabasemanipulationlanguage.Yougetabrief

introductiontoitinthischapter.IfyouexpandtheSQLServersitem,

youseeyourmachinenameindicatedagain.Finally,youseealistof

theactualdatabasesregisteredtoyourmachine.Evenifyouhaven’t

attachedanydatabasesyet,therewillstillbeafewplacedthereas

prototypes.Right-clickonyourmachinenameunderSQLServersand

youaregivenanopportunitytocreateanewdatabase.Doso,and

nameit“SimpleSpy.”Whenyouarefinished,theIDEaddsthe

SimpleSpydatabaseandseveralrelatedelementstoyourserverlist.

ItshouldlooklikeFigure11.6.



Figure11.6:Thehierarchy

treeontheleft-handsideshowsmymachinewithmymachine

listedasanSQLServer.TheSimpleSpydatabaseislistedasan

elementofmySQLServer.



AddingaTable

Databasesuseaconstructcalledatableasthecoreentityforstoring

data.Atableholdsinformationaboutaspecificelement.Eachtable

consistsofanumberoffields,whicheachhaveavalueandatype.To

makeanewtableforthesimplespydatabase,right-clickonTables

undersimpleSpyintheserverexplorertab,andchooseAddNew

Table.ThemainpanelofyoureditorchangessoitlookslikeFigure

11.7.





Figure11.7:AtableeditorreminiscentofMicrosoftAccesspops



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 11: Databases and ADO.NET: The Spy Database

Tải bản đầy đủ ngay(0 tr)

×