Sie sind auf Seite 1von 21

An Introduction to

and
Visual Basic.net
Contents
AN OVERVIEW OF THE VISUAL STUDIO.NET FRAMEWOR.........................!
.net Framework Components...........................................................................................3
Web Services....................................................................................................................4
User Interface...................................................................................................................4
Data and XML.................................................................................................................4
Base Class Library...........................................................................................................4
The Common Lan!ae "!ntime....................................................................................#
System Namespace.............................................................................................................6
THE .NET IDE........................................................................................................."
VISUAL BASI#.NET..............................................................................................$
Visual Basic.net Data Types..............................................................................................9
The System.Object Class................................................................................................$%
&arbae Collection in 'is!al Basic.net.........................................................................$%
Obect Oriente! "ro#rammin# in Visual Basic.net......................................................$$
Visual Basic.net Classes % C&eckin# 'ccount Sample..................................................$$
Visual Basic.net % ' (i#& )e*el Summary....................................................................$3
The (rame)or*..............................................................................................................$4
Synta+............................................................................................................................$4
Distrib!tion of ,--lications..........................................................................................$4
Data ,ccess and XML...................................................................................................$#
A##ESSIN% DATA WITH ADO.NET...................................................................&'
+,) in Visual Basic.net..................................................................................................$-
AS(.NET AND WEB SERVI#ES.........................................................................)*
, final note on ,S..net................................................................................................../$
.eb Ser*ices...................................................................................................................../$
An o+er+ie, o- t.e Visual Studio.net Fra/e,or0
Unless yo! have been livin !nder a roc* for the -ast year0 yo! m!st have heard of .net
12dot net34 by no). What is .net5 Is it a ne) o-eratin environment5 Is it a ne)
lan!ae5 Is it a ne) )ay of develo-in distrib!ted a--lications5 The ans)er is
26es37 .net is all of these thins and more.
The .net )orld )as created by Microsoft to allo) !sers to have access to their
information0 files0 or -rorams any)here0 anytime0 and on any -latform or device.
When Microsoft introd!ced the first Windo)s o-eratin system0 it too* a--lication
develo-ment and system desin to a ne) level7 m!lti8tas*in. With each ne) version of
Windo)s0 m!lti8tas*in has been driven more to)ards distrib!ted -rocessin and .net is
the ne+t ste-.
'is!al St!dio.net has an entirely ne)0 ob9ect8oriented frame)or*. In this -a-er I )ill
introd!ce yo! to some of feat!res in 'is!al St!dio.net0 ho)ever0 I )ill foc!s mostly on
'is!al Basic.net and dra) some com-arisons to its -redecessor 'B :. The reader of this
-a-er )ill not be able to start develo-in code in 'is!al Basic.net after readin0 b!t
sho!ld have a hih8level vie) of the chanes in 'is!al Basic.net and ho) they differ
from -revio!s versions.
.net Fra/e,or0 #o/1onents
Visual Basic
Common lanaguage specification
Web Services User Interface
Data and XML
Base class library
Common language runtime
C C! """
Visual Studio "#$%
Fra/e,or02 Lan3ua3es and Tools
Fi#ure $ 0 'n o*er*iew o1 t&e .net Framework
,s sho)n in (i!re $0 'is!al Basic sits at the to- of the frame)or* 1alon )ith the other
lan!aes in 'is!al St!dio.net4. Belo) that is the Common Lan!ae S-ecification
1CLS4. This s-ecification is a set of r!les that overn the minim!m lan!ae feat!res
that m!st be s!--orted to ens!re that a lan!ae )ill intero-erate )ith other CLS8
com-liant com-onents and tools. ,s lon as a lan!ae conforms to the CLS0 it is
!aranteed to )or* )ith the CL". In this )ay0 )hen third8-arty com-ilers taret the .net
frame)or*0 as lon as they conform to the CLS0 the code is !aranteed to )or*.
6o! miht also notice that 'B is no) and 2e;!al3 or -eer of C<<0 C= and any other
lan!ae that is .net com-liant. 'is!al Basic.net shares the same variable ty-es0 arrays0
!ser8defined ty-es0 classes0 ra-hical forms0 vis!al controls and interfaces as these other
lan!aes.
.eb Ser*ices
Web Services -rovide a Web8enabled !ser interface )ith tools that incl!de vario!s
>TML controls and Web controls. (orms creatin !sin Web Services are the same as
forms created for a Windo)s a--lication. The code behind a Web form is the same as the
code behind a Windo)s form. The mar*!- lan!ae that is !sed by Web forms is still
there0 b!t the Web form a--lications enerate it for yo!.
2ser 3nter1ace
,t the same level as Web Services is the User Interface. The User Interface is )here
Windo)s forms live. It also -rovides code for dra)in to the screen0 -rintin0 renderin
te+t and dis-layin imaes.
Data an! +,)
Both Web Services and the User Interface sit on to- of the Data and XML bloc*. ,s yo!
)ill learn later in this -a-er0 XML 1or e+tensible mar*!- lan!ae4 -lays 9!st as
im-ortant of a role as data. XML is !sed to -rovide a te+t vie) of data that can be shared
bet)een services on the same .C or -assed thro!h a fire)all to a )eb server across the
co!ntry !sin S?,. 1more on S?,. a little later4.
Base Class )ibrary
The base class library 1BCL4 is !nderneath the Data and XML bloc*. This area is the
oriin for the base class of all .net -rorams. @verythin in 'is!al Basic.net is an ob9ect0
and all ob9ects oriinate from a class named System. The BCL also -rovides collections0
localiAation0 te+t ob9ects0 intero-erability )ith non8.net code and ,ctiveX controls and a
variety of other services.
T&e Common )an#ua#e 4untime
,t the base of the .net frame)or* is the common lan!ae r!ntime 1CL"4. M!ch li*e the
Bava 'irt!al Machine0 the CL" is needed to ma*e .net code r!n on any machine.
Base class library support
%&read support C'M mars&aler
%ype c&ec(er
Security engine
Class loader
#o//on Lan3ua3e Runti/e
$)ception manager
Debug engine
IL to native
components
Code
manager
*arbage
Collector
Fi#ure / 0 Common )an#ua#e 4untime ser*ices
The CL" is a set of reso!rces that any .net -roram can ta*e advantae of0 from any .net8
s!--orted lan!ae. ,ll lan!aes )ill be more e;!al in ca-ability than they ever have
before.
The CL" incl!des s!--ort for the BCL0 )here the architect!re for controls and forms
act!ally live. It is also res-onsible for manain threads and e+ce-tions 1re-lacin the
@rr ob9ect in 'B :4. &arbae collection is also handled by the CL".
The CL" ta*es code enerated by 'B and converts it to the native lan!ae of the
c!rrent -latform. Thro!h this conversion0 the maic of m!lti -latform e+ec!tion is
achieved. Co) 'B -rorammers can )rite code in 'B synta+ and the CL" is
res-onsible for convertin it to any -latform that can r!n the CL". The -rorammer is
removed several layers from the hard)are and doesnDt really need to *no) )hat -latform
hisEher code is r!nnin on.
?ne additional service of the CL" is handlin bad code. 6o! have -robably heard stories
of ro!e code that overran b!ffers and trashed a machine0 *illin all other -rorams alon
)ith it. By desin0 this cannot ha--en )ith code )ritten in 'is!al Basic.net.
S4ste/ Na/es1ace
The System names-ace incl!des all classes that re-resent the base data ty-es !sed by all
'is!al Basic.net a--lications. In .net0 all of the base data ty-es are Object, Byte, Char,
Array0 and so on. Many of these corres-ond to the data ty-e in classic 'B.
In addition to the base data ty-es0 the System names-ace contains nearly $%% classes that
-rovide f!nctionality ranin from handlin e+ce-tions and formin deleates to dealin
)ith the core r!n8time conce-ts s!ch as a--lication domains and the a!tomatic memory
manaer. The System names-ace also contains /# second8level names-aces0 listed in
Table $. Loo*in at these0 yo! can see ho) the names-ace convention loically se-arates
f!nctionality.
Table $ % Secon!ary namspaces in t&e System Namespace
Namespaces Description
System.CodeDom Contains classes that can be !sed to re-resent the elements and
str!ct!re of a so!rce code doc!ment.
System.Collections Contains interfaces and classes that define vario!s collections
of ob9ects0 s!ch as lists0 ;!e!es0 arrays0 hash tables and
dictionaries.
System.ComponentModel .rovides classes that are !sed to im-lement and license
com-onents.
System.Configuration .rovides classes that ive system r!n times0 administrative
tools0 a--lications and other cons!mers of confi!ration
information access to confi!ration information.
System.Data Consists mostly of classes that constit!te the Microsoft
,D?.net architect!re.
System.Diagnostics .rovides classes to deb! a--lications and trace the e+ec!tion
of code.
Ssytem.DirectoryServices .rovides access to ,ctive Directory from manae code.
System.Draing .rovides access to the &DI< basic ra-hics f!nctionality.
System.!nterpriseServices .rovides transaction8-rocessin f!nctionality.
System."lobali#ation .rovides localiAation information on elements s!ch as the
c!rrent c!lt!re0 formattin0 data0 and time for s-ecific locales.
System.$O .rovides ty-es that allo) synchrono!s and asynchrono!s
readin from and )ritin to data streams and files.
System.Management .rovides classes for manaer of system ob9ect and events.
System.Messaging .rovides classes to connect to messae ;!e!es on the net)or*0
send messaes to ;!e!es and receive or 2-ee*3 messaes from
;!e!es.
Namespaces Description
System.%et .rovides a sim-le -rorammin interface to many of the
-rotocols fo!nd on the net)or* today.
System.&eflection Contains classes and interfaces that -rovide a manaed vie) of
ty-es0 methods and fields0 )ith the ability to dynamically create
and invo*e ty-es.
System.&esources .rovides manaement of reso!rces0 s!ch as a reso!rce that
contains c!lt!re8s-ecific information.
System.&untime .rovides infrastr!ct!re services0
System.Security .rovides the !nderlyin str!ct!re of the .net frame)or* sec!rity
system0 incl!din interfaces0 attrib!tes0 e+ce-tions and base
classes for -ermissions.
System.Service'rocess .rovides classes to install and r!n services 1non8interface
e+ec!tables4.
System.(e)t Contains classes re-resentin ,SCII0 Unicode0 UT(8F and
UT(8G character encodinH abstract base classes for convertin
bloc*s of characters to and from bloc*s of bytesH a hel-er class
that mani-!lates and formats String ob9ects )itho!t creatin
intermediate instances of String.
System.(hreading .rovides class and interfaces that enable m!ltithreaded
-rorammin.
System.(imers .rovides t)o com-onents that raise an event on an interval or
more com-le+ sched!le.
System.*eb S!--lies classes and interfaces that enable bro)ser8server
comm!nication.
System.*indos.+orms Contains classes for creatin Windo)s8based a--lications and
classes for many controls that can be added to forms.
System.,ml Contains XML classes that -rovide standards8based s!--ort for
-rocessin XML.
T.e .net IDE
Fi#ure 3 0 T&e .net 3D5
?ne of the biest im-rovements )ith .net0 in my o-inion0 is the ne) common ID@. ,s
yo! can see in (i!re I0 the ID@ contains a little of all the different classic 'is!al St!dio
lan!aes. There is an o!t-!t )indo) similar to the one in C<<0 the -ro-erties )indo)
similar to the one in 'BH b!t best of all are the 2slide8in3 ca-abilities of the different
e+-lorer )indo)s.
There is no) a server e+-lorer that slides8in from the left side of the ID@. This e+-lorer
allo)s the develo-er to have access to o-eratin services0 data connections0 and best of
all0 SJL server. I cannot tell yo! ho) many times IDve )ished that I co!ld chane a
stored -roced!re or table desin )itho!t havin to load SJL @nter-rise Manaer to do
so. With the .net ID@0 all of the tools are fo!nd riht in the same a--lication. With the
!se of the 2th!mb8tac*30 yo! can minimiAe e+-lorers to ain more s-ace for yo!r code
)indo) if needed.
Visual Basic.net
Co) itDs time to ta*e a loo* at 'is!al Basic.net0 the main foc!s of this -a-er. I am oin
to ass!me that the reader is already at least familiar )ith )ritin a--lications !sin 'B :
or earlier. The intent of this -a-er is to -oint o!t )hat is ne) in 'is!al Basic.net and not
ho) to develo- a--lications !sin 'is!al Basic.net.
Visual Basic.net Data T41es
Before )e can et into the 'is!al Basic.net code0 )e need to start by e+-lorin the
chanes to data ty-es. >o-ef!lly0 yo! are not a -rorammer that insisted on !sin the
-ariant data ty-e consistently in yo!r codeKif yo! did0 yo! )ill have a more diffic!lt
time ad9!stin to the ne) data ty-es in 'is!al Basic.net. The -ariant data ty-e no loner
e+ists in 'is!al Basic.net. 1,nother data ty-e that did not ma*e the transition is the
Currency ty-e.4
The -rimitive data ty-es still e+ist 1$nteger, Boolean, .ong, etc.40 the main difference no)
is that these data ty-es are all str!ct!re ty-es in the System names-ace and are referred to
as -alue (ypes.
'al!e ty-es are al)ays accessed directly. In fact0 yo! canDt create a reference to a val!e
ty-e. ,nd !nli*e reference ty-es0 settin a val!e ty-e to C!ll is not -ossible. 'al!e ty-es
al)ays hold a val!e0 even if one hasnDt been assined yet. When a val!e ty-e variable is
dimensioned0 itDs initialiAed to a val!e re-resentative of its ty-e. (or e+am-le0 if yo!
dimension a variable to $nteger0 the 'is!al Basic.net com-iler a!tomatically initialiAes
the variable to %.
Consider the follo)in 'B : variable declaration7
Dim intValue As Integer
This code is still valid in 'is!al Basic.net0 ho)ever0 yo! can also do the follo)in in
'is!al Basic.net7
Dim intValue As Integer = 5
Bein able to dimension and set the val!e of a variable at the same time is a m!ch
)elcome chane.
(or yo! Bava !sers o!t there0 there are also some ne) feat!res to the val!e ty-es yo! are
already familiar )ith7 formattin and conversion. In 'B :0 the (ormat14 f!nction and
f!nctions li*e CStr14 or CInt14 -rovided !s )ith the means of convertin bet)een data
ty-es. Co)0 each data ty-e has its o)n b!ilt in methods to handle conversion0 e;!ality
chec*s 1thereDs even a ToStrin14 method in these str!ct!res.4
, final note on data ty-es7 'is!al Basic.net is a ty-e8safe lan!ae 1yo! can only access a
variable thro!h the ty-e associated )ith that variable4. We sho!ld be !sed to !sin
Option !)plicit in 'B code to re;!ire variable declaration0 b!t 'is!al Basic.net has a ne)
directive called Option Strict. This ne) directive -revents yo! from tryin to over)rite
data )ith a different data ty-e and sho!ld al)ays be incl!ded at the to- of yo!r class and
form code.
T&e System.Object Class
The System.Object class is m!ch li*e the Object class in Bava. The System.Object class is
the s!-er class of all classes the in .net frame)or*LitDs the only class in .net that does
not inherit from any other ob9ect ty-e. ,ll ob9ects m!st either e+-licitly or im-licitly
declare s!--ort for e+actly one other ob9ect ty-e. In .net it is im-ossible to !se a class
that does not inherit from System.Object.
There are si+ methods inherited by all classes that come from the System.Object class0 see
Table I belo).
Table 3 M 3n&erite! Methods from System.Object
Namespaces 'ccess Description
!/uals .!blic Ta*es another ob9ect as a -arameter and ret!rns a
Boolean that indicates )hether the t)o ob9ects are e;!al.
"et0ashCode .!blic "et!rns an inteer hash code that re-resents the ob9ectDs
val!e. This code is !s!ally !sed as a *ey )hen the ob9ect
is added to a collection. T)o identical ob9ects sho!ld
enerate the same code.
+inali#e .rotected The CL" calls an ob9ectDs +inali#e method to notify the
ob9ect that the ob9ect is abo!t to be destroyed. This
method really does nothin and is overridden by the
class.
MemberiseClone .rotected Creates a shallo) co-y of the ob9ect.
"et(ype .!blic "et!rns an instance of System.(ype0 )hich is !sed to et
information abo!t the ob9ect thro!h metadata.
(oString .!blic "et!rns a strin re-resentation of the ob9ect.
6arba#e Collection in Visual Basic.net
In 'B :0 )hen yo! )ere finished !sin an ob9ect0 yo! sim-ly set it to nothin and the
ob9ect )o!ld be released. This is no loner the case )ith 'is!al Basic.net. In 'is!al
Basic.net0 )hen a variable loses sco-e0 the CL" destroys the ob9ect and removes it from
the stac* a!tomatically for yo!.
O56ect Oriented (ro3ra//in3 in Visual Basic.net
Many -eo-le have as*ed for this for a lon time and no) Microsoft has delivered. ,s
yo! may have !essed by no)0 the ma9or difference in 'is!al Basic.net is that it is no)
tr!ly ob9ect8oriented.
Inheritance is one of the f!ndamental as-ects of ob9ect8oriented -rorammin. Thro!h
inheritance0 yo! can derive classes from other classes that have already been )ritten.
@ven a Windo)s form is no) inherited0 9!st loo* at the code at that beins each form7
Public Class Form1
Inherits System.Windos.Forms.Form
,s yo! may *no)0 )hen a class inherits from a base class0 it inherits the -ro-erties and
methods of that class 1or its members4. 6o! can then !se or add these members in yo!r
o)n class in )hatever )ay yo! need.
'is!al Basic.net co!ldnDt be tr!ly ob9ect8oriented )itho!t -olymor-hism0 )hich it of
co!rse has. Usin the *indos.+orms.+orm class an e+am-le7 yo! create t)o Windo)s
forms in yo!r a--lication0 they both have a Close14 method0 b!t )hat the code does in
each form can be different 1an !s!ally is4. The -oint is that each form inherits from the
*indos.+orms.+orm class0 b!t each has its o)n 2form3 1no -!n intended4. ,fter all0 the
)ord -olymor-hism literally means 2many forms3.
?ne other chane in 'is!al Basic.net that relates to ??. is method overloadin. 'B
al)ays had a 2sort of3 )ay of overloadin f!nctions or methods by !sin the Optional
*ey)ord in the f!nctionEmethod declaration. In 'is!al Basic.net 1aain0 m!ch li*e in
Bava40 )hen yo! )rite a definition for a methodEf!nction0 yo! can overload it )ith the
different -arameters as needed.
Visual Basic.net #lasses 7 #.ec0in3 Account Sa/1le
The follo)in is a short e+am-le of a class )ritten in 'is!al Basic.net. ,fter the code0 I
)ill e+amine the feat!res of the class.
Co!e 1or t&e C&eckin# class7
!"tion Strict !n
Im"orts System
#ames"ace Accounts
Public Class Chec$ing
Pri%ate m&strAcct#ame As String = ''
Pri%ate m&strAcct#umber As String = ''
Sub #e()
*y+ase.#e()
,nd Sub
Sub #e(+yVal strAcct#ame As String- +yVal strAcct#umber As
String)
*y+ase.#e()
m&strAcct#ame = strAcct#ame
m&strAcct#umber = strAcct#umber
,nd Sub
!%erridable Function Seriali.e() As +oolean
I/ ((m&strAcct#ame 01 '') And (m&strAcct#umber 01 '')) &
2hen
3eturn 2rue
,lse
3eturn False
,nd I/
,nd Function
43egion 'Account Pro"erties'
Pro"erty Account#ame() As String
5et
Account#ame = m&strAcct#ame
,nd 5et
Set(+yVal Value As String)
m&strAcct#ame = Value
,nd Set
,nd Pro"erty
Pro"erty Account#umber() As String
5et
Account#umber = m&strAcct#umber
,nd 5et
Set(+yVal Value As String)
m&strAcct#umber = Value
,nd Set
,nd Pro"erty
4,nd 3egion
,nd Class
,nd #ames"ace
58aminin# t&e C&eckin# class an! 'ccounts Namespace7
Option Strict On M To reiterate0 this statement chec*s for -ossible -roblems in the
code that )o!ld try to over)rite different data ty-es 1for e+am-le if7 intI N strSomethin4.
Imports System M This im-orts the classes in the System Cames-ace.
Namespace Accounts 1 %amespaces are !sed to ro!- related ty-es into a
hierarchical cateoriAation. WeDve already e+-lored the System names-ace0
IDve chosen to !se ,cco!nts for my names-ace. Later0 I may )ant to add a
Savins acco!nt or a Money Mar*et acco!nt class to the ,cco!nts names-ace
that )o!ld have different -ro-erties.
Public Class Checking 2 Beins the definition of the class.
Ce+t0 I declare -rivate memory variables that assist me in data hidin. While these
variables )ill hold the act!al val!e of the ,cco!nt Came and ,cco!nt C!mber0 the
cons!mer of this class can only access these val!es by !sin the -!blic methods.
Sub New()8 This is the defa!lt constr!ctor for the class. 6o! can see the I have
overridden the Ce)14 constr!ctor )ith one that )ill also allo) the cons!mer to -ass
initial val!es to the class d!rin constr!ction.
MyBaseNew()6 This m!st be the first line of code in the constr!ctor methods. This
calls the base8class constr!ctor that the class is derived from 1in this case it is System4.
O!erri"able #unction Seriali$e() M Beca!se 'is!al Basic.net methods are
not overridable by defa!lt yo! m!st e+-licity !se the *ey)ord Overridable for methods
yo! )ant cons!mers to be able to override.
Property AccountName()1 The traditional LetESet methods have been re-laced in
'is!al Basic.net )ith &etESet methods instead that are 2)ra--ed3 inside of the 'roperty
bloc*.
To act!ally im-lement this class in yo!r 'is!al Basic.net code yo! )o!ld assin the class
to a variable !sin the follo)in code7
Dim clsChec$ing As #e Accounts.Chec$ing()
This calls the defa!lt constr!ctor for the class. Then to set the -ro-erties of the class0 yo!
co!ld do the follo)in7
With clsChec$ing
.Account#ame = 7AmSouth Chec$ing Account8
.Account#umber = 7596111:9::18
,nd With
Visual Basic.net 7 A Hi3. Le+el Su//ar4
,s yo! can -robably tell by no)0 there are a lot of thins that are ne) )ith 'is!al
Basic.net0 b!t there are also some thins that havenDt chaned. ?ne co!ld easily )rite a
boo* on this to-ic0 b!t )ith this -a-er )e )ill *ee- it on a hih levelKso letDs revie)
some of the ma9or chanes in 'is!al Basic.net.
T&e Framework
There are many oals of the .net frame)or*0 ho)ever0 the main oal0 and benefit0 is that
of the CL". With the CL"0 ob9ects that are )ritten in C<<0 C= and 'is!al Basic.net can
all be !sed in any -ro9ect that is tareted for the .net frame)or*. If yo! are familiar )ith
C?M0 a s!b9ect I havenDt to!ched on in this -a-er0 itDs time to foret )hat yo! *no)K
C?M no loner e+ists in .net 1there is bac*)ard com-atibility for ,ctiveX controls and
DLLs0 ho)ever0 I )o!ld e+-ect this to o a)ay in f!t!re releases of .net4.
The (rame)or* is meant to be the 2reat e;!aliAer3. Co loner do )e0 as -rorammers0
have to )orry abo!t the !nderlyin architect!reEhard)are )eDre r!nnin onH if the .C
meets the minim!m re;!irements for the .net (rame)or*0 then yo!r 'is!al Basic.net
a--lication sho!ld r!n )itho!t -roblem on that .C.
Synta8
M!ch of the synta+ in 'is!al Basic.net has remained the same0 ho)ever0 as IDve already
mentioned0 everythin in 'is!al Basic.net is no) an ob9ect0 even the data ty-es. 6o! can
still !se data ty-es m!ch the same as before0 ho)ever0 since they are no) ob9ects0 the
-rorammer has reater control and fle+ibility )hen mani-!latin them.
'is!al Basic.net is ty-e8safe. Since the -ariant data ty-e no loner e+ists0 yo! cannot
sim-ly assin the val!e of an $nteger to a String for e+am-leH this )ill ca!se a r!ntime
error. Usin the Option Strict statement at the beinnin of yo!r code )ill hel- eliminate
these ty-es of errors beca!se the com-iler )ill catch them.
Distribution o1 'pplications
This is a ma9or0 and very m!ch )elcome chane0 in 'is!al Basic.net. Today0 )hen yo!
create a 'B : a--lication that !ses C?M com-onents0 yo! have to remember to al)ays
-ac*ae the C?M com-onents )ith yo!r a--lication. (!rthermore0 yo!Dd better ma*e
s!re yo! are -ac*ain the riht version of the com-onents or yo! )ill et tra--ed in
DLL >ellO
With 'is!al Basic.net0 yo!r taret a!dience for the a--lication m!st first install the .net
(rame)or* on their .C1s4. Ce+t0 yo! sim-ly create a folder and co-y yo!r a--lication
and any s!--ortin files 1hel- files0 etc.4 to that folder and yo!Dre finished. It really is
that sim-leO When the !ser )ants to !ninstall yo!r a--lication0 they sim-ly delete the
folder containin the a--lication. There is no ris* or )orry of !n8reisterin com-onents
or removin shared com-onents that ca!se other a--lications to no loner f!nction.
Data 'ccess an! +,)
I )ill tal* more abo!t ,D?.net in one of the follo)in sections0 b!t I did )ant to
mention here that data access )ith 'is!al Basic.net and ,D?.net has chaned immensely.
The &ecordSet ob9ect no loner e+ists in ,D?0 instead )e no) !se a DataSet ob9ect.
The main idea behind the DataSet ob9ect is this7 after yo! have defined all yo! need
abo!t the data so!rce0 yo! !se a DataSetCommand ob9ect to connect to the data so!rce
and retrieve the data yo! )ant then disconnect from the data so!rce. This frees !- server
reso!rces beca!se the server does not have to maintain as many conc!rrent connections.
1This -rinci-le is m!ch the same as !sin a disconnected recordset0 b!t as yo!Dll see later0
there are also other feat!res in the DataSet ob9ect that disconnected recordsets cannot
handle.4
,t the root of all data in .net is XML. I )ill e+-lain later )hy XML is !sed and yo! )ill
ho-ef!lly see the bea!ty of it. If yo! have ever develo-ed distrib!ted a--lications0 or
)anted to0 .net ives yo! the tools to do this m!ch easier than before. If yo! refer bac*
to the table containin the System Cames-ace0 yo!Dll see System.,ml at the end of the
listKi.e.0 one co!ld also say that at the root of ,LL thins in .net is XML.
Accessin3 Data ,it. ADO.net
Data access )ith 'is!al Basic has come a lon )ay in a relatively short -eriod of time.
,fter all0 Microsoft released three versions of 'is!al Basic before database access )as
ever incl!ded. In 'B I0 Microsoft introd!ced D,? 1Data ,ccess ?b9ects40 )hich !sed
the Microsoft Bet @nine to connect to local databases. 6o! co!ld !se D,? to connect to
databases on a server0 b!t the -erformance )as -oor beca!se D,? )as o-timiAed for
local access.
(ollo)in D,? came "D? 1"emote Data ?b9ects4 and then finally ,D? 1,ctiveX Data
?b9ects4. These access technoloies )ere desined )ith clientEserver in mind0 b!t )ith
the move a)ay from a clientEserver to an n8tier a--roach to system desin0 somethin
ne) )as neededH enter ,D?.net.
,s mentioned earlier0 the &ecordset ob9ect no loner e+ists in ,D?.net. The s!ccessor0
the DataSet ob9ect no) ives !s a loo* at all of the data. It can model data loically or
abstractly0 beca!se !nli*e the &ecordSet0 the DataSet is not a container that can hold only
ro)s of data. The DataSet can act!ally hold m!lti-le tables and the relationshi-s
bet)een them.
LetDs say for e+am-le yo! )anted to ;!ery data from t)o tables in a database that are
9oined toether. In traditional ,D?0 yo! )o!ld e+ec!te a SJL ;!ery that -laced the
res!lts in the &ecordSet ob9ect. In ,D?.net0 !sin the DataSet ob9ect0 the t)o tables
themselves are -laced in the data set and yo! then -erform the needed ;!ery after)ards.
The advantae of this method is not obvio!s0 b!t )hat if there )as some bit of detail in
one of the tables yo!r ;!ery left o!t5 6o! )o!ld then0 -ossibly0 have to create another
&ecordSet to ;!ery the additional information yo! needed.
.revio!s releases of ,D? had s!--ort for XML0 b!t it )as cr!de at best. If yo! donDt
*no) )hat XML is by no)0 I s!est yo! start s!rfin or b!y a ood boo* on XML7 itDs
here to stay.
(or several years no)0 many b!sinesses have been !sin XML to e+chane data. The
-roblem has been that both entities had to aree in advance on the format of the XML
doc!ment0 beca!se there )ere not any lan!aes that )o!ld ive yo! an easy )ay to
access XML data. Microsoft introd!ced the !se of XML in ,D? in version /.$. The
-rorammer co!ld either save a recordset to XML and vice8versaH the tro!ble )as that
Microsoft defined the format of the XML and no other -latforms had native s!--ort for
it. Than*s to WIC0 there is no) a standard XML schema for recordsets and Microsoft
!ses this standard in ,D?.net. I am not s!re if other com-anies have yet ado-ted the
standard0 b!t yo! can be ass!red they )ill soon if they havenDt already.
(i!re 4 de-icts data flo) from a data so!rce to a data cons!mer !sin ,D?.net. (irst
)e connect to and retrieve tables from a relational database. The tables are each -laced
in an in8memory DataSet ob9ect !sin DataSetCommand ob9ects. These
DataSetCommand ob9ects s-ecify the tables 1or a s!bset thereof4 that )e are interested in.
,fter the DataSet is filled0 the connection to the database is immediately closed. We can
then b!ild the relationshi- bet)een the individ!al tables )ithin the DataSet itself and
send the data0 via XML0 to any client a--lication.
Database
Data Tier
Business Tier
Data O56ect 8#lass9
DataSet
DataSetCommand DataSetCommand
Internet+Intranet
XML
,resentation %ier
Windo-s .orms
DataSet
M4A11.e:e
Web .orms
DataSet
Fi#ure 9 0 'DO.net !ata 1low
Table 4 belo) lists some of the differences bet)een ,D? and ,D?.net.
Table 9 M ' Comparison o1 'DO an! 'DO.net Features
Feature 'DO 'DO.net
Memory8resident
data
Use the "ecordset ob9ect0 )hich
loo*s li*e a sinle table.
Uses the DataSet ob9ect0 )hich can
contain one or more tables
re-resented by DataTable ob9ects.
"elationshi-s
bet)een
m!lti-le
tables
"e;!ires the B?IC ;!ery to
assemble data from m!lti-le
database tables in a sinle res!lt
table.
S!--orts the Data"elation ob9ect to
associate ro) in one DataTable
ob9ect to ro)s in another DataTable
ob9ect.
,ccessin data Scans "ecordset ro)s
se;!entially.
.ermits non8se;!ential access to
ro)s in a table. (ollo)s
relationshi-s to naviate from ro)s
in one table to corres-ondin ro)s in
another table.
Disconnected access .rovided by the "ecordset
ob9ect0 b!t enerally s!--orts
connected access re-resented
by the Connection ob9ect.
Comm!nicates )ith a database
)ith call to an ?L@ DB
-rovider.
Comm!nicates to a database )ith
standardiAed calls to a
DataSetCommand ob9ect.
.rorammability Uses the connection ob9ect to
transit commands.
Uses the strictly ty-ed -rorammin
characteristic of XML. Data is self8
describin. Underlyin data
constr!cts s!ch as tables and ro)s do
not a--ear in the XML0 ma*in code
easier to read and to )rite.
Sharin disconnected
data
bet)een
tiers or
com-on
ents
Uses C?M marshalin to
transmit a disconnected
recordset. S!--orts only those
data ty-es defined by the C?M
standard.
Transmits a DataSet )ith an XML
file. The XML format -laces no
restrictions on data ty-es and
re;!ires no ty-e conversion.
Transmittin data
thro!h
fire)alls
.roblematic beca!se fire)alls
are ty-ically confi!red to
-revent system8level re;!ests
s!ch as C?M marshalin of
binary ob9ects.
Co -roblem beca!se the ,D?.net
DataSet ob9ect !ses te+t8based XML0
)hich can -ass thro!h fire)alls.
Scalability Database loc*s and active
database connections for lon
d!rations contend for limited
reso!rces.
Disconnected access to database data
limits contention for limited database
reso!rces.
;ML in Visual Basic.net
6o! cannot do anythin in 'is!al Basic.net )itho!t XML bein involved some)here. It
may not be on the s!rface0 b!t it is there in the !nderlyin data. Chances are that if yo!
are only develo-in Windo)s a--lications for !se on a sinle .C0 yo! )ill not !se XML
very oftenH b!t if yo! )ant to ;!ery or !-date data from a remote data so!rce 1on the
Internet or Intranet40 yo! )ill need to !nderstand )hat XML is and )hat it can do. ,nd
yo! )ill0 of co!rse0 !se ,D?.net as the mechanism to do this.
AS(.net and We5 Ser+ices
Web (orms 1the controls and classes .net -rovides for b!ildin Web -aes4 and Web
Services 1-rorammable a--lication loic accessible by standard Internet -rotocols4 are
-art of the frame)or* for Internet f!nctionality incl!ded in 'is!al Basic.net and are
*no)n !nder the !mbrella of ,S..net. If yo! have )or*ed )ith ,S. 1,ctive Server
.aes4 before0 yo! )ill be s!r-rised at ho) m!ch different and more efficient ,S..net is.
,S..net encom-asses a com-letely ne) -rorammin ob9ect model. It re-laces the 'B :
WebClasses and D>TML -aes. Cot only that0 b!t the ,S..net -rorammin model is
also more consistent and easier to !se.
Some of the *ey feat!res of ,S..net are7
)an#ua#e 3n!epen!ence. ,S..net allo)s yo! to !se com-iled lan!aes0
-rovidin better -erformance and cross8lan!ae com-atibility.
Simpli1ie! !e*elopment. ,S..net ma*es even the richest -aes
straihtfor)ard and easy to )rite.
Separation o1 co!e an! content. @ach Web (orm has a code mod!le )ith
the same name b!t )ith the e+tension .vb. This so8called code behind the
page contains the -roram loic code0 )hile the Web (orm contains the vis!al
com-onents.
3mpro*e! scalability. Ce) session8state feat!res ma*e it easy to create Web
(orms that )or* on Web server farms.
Support 1or multiple clients. ,S..net controls can a!tomatically detect the
client and o-timiAe themselves for a consistent loo* and feel. 6o! no loner
have to )rite se-arate code for different bro)sers.
New .eb Forms controls. The ne) controls can o!t-!t >TML I./ for
do)n8level bro)sers )hile ta*in advantae of the r!ntime libraries for
enhanced interactivity on richer clients. ?!r -rorams can no) o!t-!t to a
)hole ne) rane of -latforms s!ch as .D,s.
Ser*er0si!e processin#. ,S..net chanes each -ae into a server8side ob9ect.
More -ro-erties0 methods0 and events can be !sed )ith yo!r code to create
content dynamically. The runat13server4 attrib!te converts the >TML
element into a server8side control that is visible and therefore -rorammable
)ithin ,S..net on the server. @vents raised by Web (orm controls are
detected and the a--ro-riate code is e+ec!ted on the server in res-onse to
these events.
' 1inal note on 'S".net
,S..net is )ritten entirely in the ne) C= lan!ae. ,ll ,S..net -aes have the .as-+ file
e+tension0 )hich allo)s both .as- and .as-+ files to be r!n on the same machine !nder the
e+istin ,S. r!ntime.
We5 Ser+ices
In very eneral terms0 ,S..net -aes are for h!man interaction )ith a Web server0 and
Web services are for -rorammatic interaction )ith a Web server. Web services are a
eneral model for b!ildin a--lications that can be im-lemented for any o-eration that
s!--orts or re;!ires comm!nication over the Internet.
DC?M 1)hich stands for Distrib!ted C?M4 has been aro!nd for a lon time and is
)idely !sed today by many develo-ers. DC?M allo)s !s to distrib!te 2tiers3 or sinle
com-onents in an enter-rise sol!tionH -roblem is DC?M !ses -rotocols that can only be
shared bet)een com-!ters r!nnin Microsoft o-eratin systems.
Web services !ses a ne) technoloy called S?,. 1Sim-le ?b9ect ,ccess .rotocol40
)hich doesnDt rely on o-eratin systems or net)or* -rotocols b!t !ses XML and >TT.
to -erform remote re;!estsH thereby ma*in it !sable on non8Microsoft -latforms as )ell.
S?,. is basically a messae e+chane -rotocol that !ses >TT. and XML. >TT. is !sed
as the trans-ort beca!se it !ses -ort G% and -ractically any com-!ter )ith Internet access
is allo)ed in and o!t of a fire)all !sin -ort G%. XML is !sed for formattin the
messaes.
The *ey advantae of Web services is that is allo)s soft)are com-anies to create services
that can be mar*eted to other com-anies. LetDs say0 for e+am-le0 )eDve been as*ed to
develo- a )eb site for o!r com-any0 )hich does b!siness in different -arts of the )orld.
?bvio!sly there )ill be money conversions involved in this a--lication. In order to *ee-
!- )ith the daily e+chane rates0 )e )o!ld have to develo- o!r o)n system that !-dates
the e+chane rates daily. B!t )hat if )e instead !sed a Web Service -rovided by a ban*5
@ach time )e need an e+chane rate0 )e sim-ly ma*e a re;!est to the ban*Ds Web
Service. The benefits of the scenario are obvio!s0 b!t in addition to the obvio!s0 is the
fact that )e have a !arantee from the ban* that the rates are valid.
There are already a n!mber of b!sinesses offerin Web Services on the Internet. There is
even an Internet based reistry that can be searched for a !sable Web Service. 6o! can
imaine that some of these services )ill be chared0 )hile other may not 1)here itDs in
the best interest of the b!siness offerin the service to -rovide it free of chare4.
Some a!thors are -redictin that Web Services may very )ell become the ne) standard
for B/B interation over the Internet.