Upload
sukuje-je
View
219
Download
0
Embed Size (px)
Citation preview
8/13/2019 Data Base Connectivity
1/25
DATA BASE CONNECTIVITY
What is ADO.NET?
ADO.NET is a part of the .NET Framework
ADO.NET consists of a set of classes used to handle data access
ADO.NET is entirely based on XM
ADO.NET has! unlike ADO! no "ecordset ob#ect
Create a Database Connection
$e are %oin% to use the Northwind database in our e&les.
First! import the '(ystem.Data.OleDb' namespace. $e need this namespace to work with MicrosoftAccess and other OE D) database pro*iders. $e will create the connection to the database in the
+a%e,oad subroutine. $e create a dbconn *ariable as a new OleDb-onnection class with aconnection strin% which identifies the OE D) pro*ider and the location of the database. Then weopen the database connection
sub Page!oaim bconnbconn=Ne# OleDb$onnection"Provier=&icroso't.(et.O!)D*.+.,-ata source=" server.mappat/"nort/#in.mb"00bconn.Open0en sub
Note:The connection strin% must be a continuous strin% without a line break/
Create a Database Command
To specify the records to retrie*e from the database! we will create a dbcomm *ariable as a newOleDb-ommand class. The OleDb-ommand class is for issuin% (0 1ueries a%ainst database tables
sub Page!oaim bconn2s3l2bcommbconn=Ne# OleDb$onnection"Provier=&icroso't.(et.O!)D*.+.,-ata source=" server.mappat/"nort/#in.mb"00
bconn.Open0s3l="S)!)$4 5 67O& customers"bcomm=Ne# OleDb$ommans3l2bconn0en sub
Create a DataReader
8/13/2019 Data Base Connectivity
2/25
The OleDbData"eader class is used to read a stream of records from a data source. A Data"eader iscreated by callin% the E&ecute"eader method of the OleDb-ommand ob#ect
sub Page!oaim bconn2s3l2bcomm2breabconn=Ne# OleDb$onnection"Provier=&icroso't.(et.O!)D*.+.,-ata source=" server.mappat/"nort/#in.mb"00bconn.Open0s3l="S)!)$4 5 67O& customers"bcomm=Ne# OleDb$ommans3l2bconn0brea=bcomm.)8ecute7eaer0en sub
Bind to a Repeater Control
Then we bind the Data"eader to a "epeater control
Example
sub Page!oaim bconn2s3l2bcomm2breabconn=Ne# OleDb$onnection"Provier=&icroso't.(et.O!)D*.+.,-ata source=" server.mappat/"nort/#in.mb"00bconn.Open0s3l="S)!)$4 5 67O& customers"bcomm=Ne# OleDb$ommans3l2bconn0brea=bcomm.)8ecute7eaer0customers.DataSource=breacustomers.Data*in0brea.$lose0bconn.$lose0en sub
8/13/2019 Data Base Connectivity
3/25
8/13/2019 Data Base Connectivity
4/25
Stanar :eaer 6or ll Pages
The master pa%e abo*e is a normal 4TM pa%e desi%ned as a template for other pa%es.
The @ Masterdirecti*e defines it as a master pa%e.
The master pa%e contains a placeholder ta%
8/13/2019 Data Base Connectivity
5/25
8/13/2019 Data Base Connectivity
6/25
Code Example:
The
8/13/2019 Data Base Connectivity
7/25
Tips 5f the (iteMap+ath displays incorrectly! most likely there is an =" error 2typo3 in theweb.sitemap file.
Code Example:
The
8/13/2019 Data Base Connectivity
8/25
The basics of ser*er side scriptin% like A(+ or +4+
5f you want to study these sub#ects first! find the tutorials on our 4ome +a%e
What is Classic A!P?
Microsofts pre*ious ser*er side scriptin% technolo%y A(+ 2Acti*e (er*er +a%es3 is now often calledclassic A(+.
A(+ B.; was the last *ersion of classic A(+.
To learn more about classic A(+! you can study our A(+ tutorial.
A!P.NET is NOT A!P
A(+.NET is the ne&t %eneration A(+! but its not an up%raded *ersion of A(+.
A(+.NET is an entirely new technolo%y for ser*er?side scriptin%. 5t was written from the %round upand is not backward compatible with classic A(+.
Cou can read more about the differences between A(+ and A(+.NET in the ne&t chapter of thistutorial.
A(+.NET is the ma#or part of the Microsofts .NET Framework.
What is A!P.NET?
A(+.NET is a ser*er side scriptin% technolo%y that enables scripts 2embedded in web pa%es3 to bee&ecuted by an 5nternet ser*er.
A(+.NET is a Microsoft Technolo%y
A(+ stands for Acti*e (er*er +a%es
A(+.NET is a pro%ram that runs inside 55(
55( 25nternet 5nformation (er*ices3 is Microsofts 5nternet ser*er
55( comes as a free component with $indows ser*ers
55( is also a part of $indows :;;; and X+ +rofessional
What is an A!P.NET #ile?
An A(+.NET file is #ust the same as an 4TM file
An A(+.NET file can contain 4TM! XM! and scripts
(cripts in an A(+.NET file are e&ecuted on the ser*er
An A(+.NET file has the file e&tension '.asp&'
(o' Does A!P.NET Wor)?
$hen a browser re1uests an 4TM file! the ser*er returns the file
$hen a browser re1uests an A(+.NET file! 55( passes the re1uest to the A(+.NET en%ine on
the ser*er
The A(+.NET en%ine reads the file! line by line! and e&ecutes the scripts in the file
http://www.w3schools.com/default.asphttp://www.w3schools.com/asp/default.asphttp://www.w3schools.com/default.asphttp://www.w3schools.com/asp/default.asp8/13/2019 Data Base Connectivity
9/25
Finally! the A(+.NET file is returned to the browser as plain 4TM
What is A!P*?
A(+> is the same as A(+.NET.
A(+> is #ust an early name used by Microsoft when they de*eloped A(+.NET.
The Microso+t .NET #rame'or)
The .NET Framework is the infrastructure for the Microsoft .NET platform.
The .NET Framework is an en*ironment for buildin%! deployin%! and runnin% $eb applications and$eb (er*ices.
Microsofts first ser*er technolo%y A(+ 2Acti*e (er*er +a%es3! was a powerful and fle&ible'pro%rammin% lan%ua%e'. )ut it was too code oriented. 5t was not an application framework and not
an enterprise de*elopment tool.
The Microsoft .NET Framework was de*eloped to sol*e this problem.
.NE) /ramewor0s 0eywords:
Easier and 1uicker pro%rammin%
"educed amount of code
Declarati*e pro%rammin% model
"icher ser*er control hierarchy with e*ents
ar%er class library
)etter support for de*elopment tools
)he .NE) /ramewor0 onsists of 1 main parts:
+ro%rammin% lan%ua%es
- 2+ronounced - sharp3
8/13/2019 Data Base Connectivity
10/25
A!P.NET ,.-
A(+.NET :.; impro*es upon A(+.NET by addin% support for se*eral new features.
Cou can read more about the differences between A(+.NET :.; and A(+.NET in the ne&t chapter of
this tutorial.
A!P.NET .-
A(+.NET B.; is not a new *ersion of A(+.NET. 5ts #ust the name for a new A(+.NET :.; framework
library with support for $indows +resentation Foundation! $indows -ommunication Foundation!$indows $orkflow Foundation and $indows -ard(pace.
A(+.NET B.; is not co*ered in this tutorial.
A!P.NET "s. A!P
,'P.NE) has better language support2 a large set of new ontrols2 3M45basedomponents2 and better user authentiation.
,'P.NE) provides inreased performane by running ompiled ode.
,'P.NE) ode is not fully ba0ward ompatible with ,'P.
Ne' in A!P.NET
)etter lan%ua%e support
+ro%rammable controls E*ent?dri*en pro%rammin%
XM?based components
=ser authentication! with accounts and roles
4i%her scalability
5ncreased performance ? -ompiled code
Easier confi%uration and deployment
Not fully A(+ compatible
/ang%age !%pport
A(+.NET uses ADO.NET.
A(+.NET supports full .
A(+.NET supports @(cript.
http://www.w3schools.com/ASPNET/aspnet_install.asphttp://www.w3schools.com/ASPNET/aspnet_intro.asp8/13/2019 Data Base Connectivity
11/25
A!P.NET Controls
A(+.NET contains a lar%e set of 4TM controls. Almost all 4TM elements on a pa%e can be definedas A(+.NET control ob#ects that can be controlled by scripts.
A(+.NET also contains a new set of ob#ect?oriented input controls! like pro%rammable list?bo&es and
*alidation controls.
A new data %rid control supports sortin%! data pa%in%! and e*erythin% you can e&pect from adataset control.
E"ent A'are Controls
All A(+.NET ob#ects on a $eb pa%e can e&pose e*ents that can be processed by A(+.NET code.
oad! -lick and -han%e e*ents handled by code makes codin% much simpler and much betteror%aniGed.
A!P.NET Components
A(+.NET components are hea*ily based on XM. ike the new AD "otator! that uses XM to store
ad*ertisement information and confi%uration.
0ser A%thentication
A(+.NET supports form?based user authentication! cookie mana%ement! and automatic redirectin%
of unauthoriGed lo%ins.
0ser Acco%nts and Roles
A(+.NET allows user accounts and roles! to %i*e each user 2with a %i*en role3 access to different
ser*er code and e&ecutables.
(igh !calabilit$
Much has been done with A(+.NET to pro*ide %reater scalability.
(er*er?to?ser*er communication has been %reatly enhanced! makin% it possible to scale anapplication o*er se*eral ser*ers. One e&le of this is the ability to run XM parsers! X(
transformations and e*en resource hun%ry session ob#ects on other ser*ers.
Compiled Code
The first re1uest for an A(+.NET pa%e on the ser*er will compile the A(+.NET code and keep a
cached copy in memory. The result of this is %reatly increased performance.
Eas$ Con+ig%ration
8/13/2019 Data Base Connectivity
12/25
-onfi%uration of A(+.NET is done with plain te&t files.
-onfi%uration files can be uploaded or chan%ed while the application is runnin%. No need to restartthe ser*er. No more metabase or re%istry puGGle.
Eas$ Deplo$ment
No more ser*er?restart to deploy or replace compiled code. A(+.NET simply redirects all new
re1uests to the new code.
Compatibilit$
A(+.NET is not fully compatible with earlier *ersions of A(+! so most of the old A(+ code will need
some chan%es to run under A(+.NET.
To o*ercome this problem! A(+.NET uses a new file e&tension '.asp&'. This will make A(+.NETapplications able to run side by side with standard A(+ applications on the same ser*er.
,'P.NE) is easy to install. 6ust follow the instrutions below.
What 1o% Need
A Windo's Comp%ter
A(+.NET is a Microsoft technolo%y. To run A(+.NET you need a computer capable of runnin%$indows.
Windo's ,--- or 2P
5f you are serious about de*elopin% A(+.NET applications you should install $indows :;;;+rofessional or $indows X+ +rofessional.
5n both cases! make sure you install the 5nternet 5nformation (er*ices 255(3 from the Add"emo*e$indows components dialo%.
!er"ice Pac)s and 0pdates
)efore A(+.NET can be installed on your computer! it is necessary to ha*e all rele*ant ser*ice packsand security updates installed.
The easiest way to do this is to acti*ate your $indows 5nternet =pdate. $hen you access the$indows =pdate pa%e! you will be instructed to install the latest ser*ice packs and all critical
security updates. For $indows :;;;! make sure you install (er*ice +ack :. 5 will also recommendthat you install 5nternet E&plorer H.
"ead the note about connection speed and download time at the bottom of this pa%e.
Remo"e 1o%r Beta &ersion
5f you ha*e a )eta *ersion of A(+.NET installed! we recommend that you completely uninstall it. Or
e*en better start with a fresh $indows :;;; or X+ installation.
8/13/2019 Data Base Connectivity
13/25
3nstall .NET
From your $indows =pdate you can now select to install the Microsoft .NET Framework.
After download! the .NET framework will install itself on your computer ? there are no options to
select for installation.
7ou should now be ready to develop your first ,'P.NE) appliation8
The .NET !o+t'are De"elopment 4it
5f you ha*e the necessary bandwidth to download o*er 7B; M)! you mi%ht consider downloadin% thefull Microsoft .NET (oftware De*elopment Iit 2(DI3.
$e fully recommend %ettin% the (DI for learnin% more about .NET and for the documentation!samples! and tools included.
Connection !peed and Do'nload Time
5f you ha*e a slow 5nternet connection! you mi%ht ha*e problems downloadin% lar%e files like theser*ice packs! the (DI and the latest *ersion of 5nternet E&plorer.
5f download speed is a problem! our best su%%estion is to %et the latest files from someone else!from a collea%ue! from a friend! or from one of the -Ds that comes with many popular computer
ma%aGines. ook for $indows :;;; (er*ice +ack :! 5nternet E&plorer H! and the Microsoft .NETFramework
(ello W!chools in A!P.NET
The simplest way to con*ert an 4TM pa%e into an A(+.NET pa%e is to copy the 4TM file to a newfile with an .aspxe&tension.
This code displays our e&le as an A(+.NET pa%e
:ello ?Sc/oolsG
5f you want to try it yourself! sa*e the code in a file called 'firstpage.aspx'! and create a link tothe file like this firstpa%e.asp&
(o' Does it Wor)?
Fundamentally an A(+.NET pa%e is #ust the same as an 4TM pa%e.
http://www.w3schools.com/ASPNET/firstpage.aspxhttp://www.w3schools.com/ASPNET/firstpage.aspx8/13/2019 Data Base Connectivity
14/25
An 4TM pa%e has the e&tension .htm. 5f a browser re1uests an 4TM pa%e from the ser*er! theser*er sends the pa%e to the browser without any modifications.
An A(+.NET pa%e has the e&tension .asp&. 5f a browser re1uests an A(+.NET pa%e! the ser*erprocesses any e&ecutable code in the pa%e! before the result is sent back to the browser.
The A(+.NET pa%e abo*e does not contain any e&ecutable code! so nothin% is e&ecuted. 5n the ne&te&les we will add some e&ecutable code to the pa%e to demonstrate the difference between
static 4TM pa%es and dynamic A(+ pa%es.
Classic A!P
Acti*e (er*er +a%es 2A(+3 has been around for se*eral years. $ith A(+! e&ecutable code can beplaced inside 4TM pa%es.
+re*ious *ersions of A(+ 2before A(+ .NET3 are often called -lassic A(+.
A(+.NET is not fully compatible with -lassic A(+! but most -lassic A(+ pa%es will work fine asA(+.NET pa%es! with only minor chan%es.
5f you want to learn more about -lassic A(+! please *isit our A(+ Tutorial.
D$namic Page in Classic A!P
To demonstrate how A(+ can display pa%es with dynamic content! we ha*e added some e&ecutable
code 2in red3 to the pre*ious e&le
:ello ?Sc/oolsG
The code inside the 8J ??J9 ta%s is e&ecuted on the ser*er.
"esponse.$rite is A(+ code for writin% somethin% to the 4TM output stream.
Now23 is a function returnin% the ser*ers current date and time.
5f you want to try it yourself! sa*e the code in a file called 'dynpage.asp'! and create a link to thefile like this dynpa%e.asp
D$namic Page in A!P .NET
This followin% code displays our e&le as an A(+.NET pa%e
http://www.w3schools.com/asp/default.asphttp://www.w3schools.com/ASPNET/dynpage.asphttp://www.w3schools.com/asp/default.asphttp://www.w3schools.com/ASPNET/dynpage.asp8/13/2019 Data Base Connectivity
15/25
:ello ?Sc/oolsG
5f you want to try it yourself! sa*e the code in a file called 'dynpage.aspx'! and create a link to thefile like this dynpa%e.asp&
A!P.NET "s Classic A!P
The pre*ious e&les didnt demonstrate any differences between A(+.NET and -lassic A(+.
As you can see from the two latest e&les there are no differences between the two A(+ and
A(+.NET pa%es.
5n the ne&t chapters you will see how ser*er controls make A(+.NET more powerful than -lassicA(+.
'erver ontrols are tags that are understood by the server.
/imitations in Classic A!P
The listin% below was copied from the pre*ious chapter
:ello ?Sc/oolsG
The code abo*e illustrates a limitation in -lassic A(+ The code block has to be placed where youwant the output to appear.
$ith -lassic A(+ it is impossible to separate e&ecutable code from the 4TM itself. This makes thepa%e difficult to read! and difficult to maintain.
A!P.NET 5 !er"er Controls
A(+.NET has sol*ed the 'spa%hetti?code' problem described abo*e with ser*er controls.
(er*er controls are ta%s that are understood by the ser*er.
There are three kinds of ser*er controls
http://www.w3schools.com/ASPNET/dynpage.aspxhttp://www.w3schools.com/ASPNET/dynpage.aspx8/13/2019 Data Base Connectivity
16/25
4TM (er*er -ontrols ? Traditional 4TM ta%s
$eb (er*er -ontrols ? New A(+.NET ta%s
Fisit ?Sc/oolsG
8/13/2019 Data Base Connectivity
17/25
5n the followin% e&le we declare a )utton ser*er control in an .asp& file. Then we create ane*ent handler for the -lick e*ent which chan%es the te&t on the button
Sub submitSource s Obect2 e s )ventrgs0button;.4e8t="Jou clicHe meG")n Sub
A!P.NET 5 &alidation !er"er Controls
8/13/2019 Data Base Connectivity
18/25
$ontrol4oFaliate="tbo8;"&inimumFalue=";"&a8imumFalue=";,,"4ype="Integer"4e8t="4/e value must be 'rom ; to ;,,G"runat="server" 1>
8/13/2019 Data Base Connectivity
19/25
8/13/2019 Data Base Connectivity
20/25
Note:The form is always submitted to the pa%e itself. 5f you specify an action attribute! it isi%nored. 5f you omit the method attribute! it will be set to method6'post' by default. Also! if you donot specify the name and id attributes! they are automatically assi%ned by A(+.NET.
Note:An .asp& pa%e can only contain ONE 8form runat6'ser*er'9 control/
5f you select *iew source in an .asp& pa%e containin% a form with no name! method! action! or id
attribute specified! you will see that A(+.NET has added these attributes to the form. 5t lookssomethin% like this
!%bmitting a #orm
A form is most often submitted by clickin% on a button. The )utton ser*er control in A(+.NET hasthe followin% format
The id attribute defines a uni1ue name for the button and the te&t attribute assi%ns a label to the
button. The on-lick e*ent handler specifies a named subroutine to e&ecute.
5n the followin% e&le we declare a )utton control in an .asp& file. A button click runs asubroutine which chan%es the te&t on the button
A!P .NET Maintaining the &ie'!tate
7ou may save a lot of oding by maintaining the *iew'tate of the ob9ets in your eb/orm.
Maintaining the &ie'!tate
$hen a form is submitted in classic A(+! all form *alues are cleared. (uppose you ha*e submitteda form with a lot of information and the ser*er comes back with an error. Cou will ha*e to %o back
to the form and correct the information. Cou click the back button! and what happens.......Aform *alues are -EA"ED! and you will ha*e to start all o*er a%ain/ The site did not maintain your
8/13/2019 Data Base Connectivity
21/25
all form *alues. 4ow comeK This is because A(+ .NET maintains your
Maintainin% the
8/13/2019 Data Base Connectivity
22/25
Sub submitsener s Obect2 e s )ventrgs0lbl;.4e8t=":ello " t8t;.4e8t "G")n Sub
)he )ext;ox ontrol is used to reate a text box where the user an input text.
The TetBo Control
The Te&t)o& control is used to create a te&t bo& where the user can input te&t.
The Te&t)o& controls attributes and properties are listed in our web controls reference pa%e.
The e&le below demonstrates some of the attributes you may use with the Te&t)o& control
Example
8/13/2019 Data Base Connectivity
23/25
4e8t*o8 #it/ /eig/t9
4e8t*o8 #it/ #it/9
)nter your name9
8/13/2019 Data Base Connectivity
24/25
Example
Sub c/angesener s Obect2 e s )ventrgs0lbl;.4e8t="Jou c/ange te8t to " t8t;.4e8t)n Sub
e may use data binding to fill lists with seletable items from an imported data soure2li0e a database2 an 3M4 file2 or a sript.
Data Binding
The followin% controls are list controls which support data bindin%
asp"adio)uttonist asp-heck)o&ist
aspDropDownist
aspistbo&
The selectable items in each of the abo*e controls are usually defined by one or more aspist5temcontrols! like this
8/13/2019 Data Base Connectivity
25/25
4owe*er! with data bindin% we may use a separate source! like a database! an XM file! or a scriptto fill the list with selectable items.
)y usin% an imported source! the data is separated from the 4TM! and any chan%es to the itemsare made in the separate data source.
5n the ne&t three chapters! we will describe how to bind data from a scripted data source.