Вы находитесь на странице: 1из 2

Connecting to Access DataBase is somewhat simple comparing to other databases.

Here
the Connection way.
Dim MyConn as OleDbConnection
Dim ConnStr as String
ConnStr=""Proider=Microso!t."et.O#$DB.%.&'Data
So(rce=C)*+netp(b*wwwroot*MyApp*MyDataBase.mdb"
MyConn= new OleDbConnection,ConnStr-
+! ( hae database Protected with .serid add ".serid=////'Password=/////" to the
aboe string
Once Connection is Opened other opearations are same as common OleDb operations
0o Ma1e it aailable !or more than one page yo( better place it in 2lobal.asa/ eents li1e
onBeign3e4(est or onAc4(ire3e4(eststate
better place is onPre3e4(estHandler5 So that by the time page processing starts the
connection will be ready to (se. we can write C(stom Controls to sere the same.
!or e/ample i! yo( write a c(stom control to ret(rn the connection
it will be better to get connection !rom the contol instead o! writing in each page.yo(r
c(stom control code goes li1e this
678 Control #ang(age="b" 79
6Script r(nat="Serer"9
P(blic :(nction 2etConnection,- as S4lConnection
Dim S4lCon as new S4lConnection,"Data So(rce=////'.serid=///'password=////"-
S4lCon.Open
ret(rn S4lCon
end :(nction
6;Script9
<o( m(st sae this !ile with .ASC= e/tension . and so it becomes c(stom control. Once
yo( register this control in yo(r asp page yo( will be allowed to get the connection
to (se this control in yo(r Asp page register lii1e this
a!ter the page directie (se
678 register 0agPre!i/="My0ag" 0ag>ame="Ctrl" src=".serControl0est.Asc/" 79
when yo( want to (se the control in the code
6My0ag)Ctrl id="MyConnection" r(nat="Serer" ;9
Here onwards connection is aailable to yo(r code li1e
Dim #ocalConn as S4lConnection
#ocalConn=MyConnection.2etConnection,-
So yo( can (se connection ob?ect
0his model is more (se!(l to aoid writing code againa and again !or !ile openings and
Database connection ss etc. +! yo( write properly with parameters yo( can get the
Data3eader or DataSets easily !rom the (ser controls itsel!.
And one more way to (se the onetime connection in!ormation is writing in Con!ig(ration
!iles.
we hae two types o! con!ig(ration !iles. One is Machine.Con!ig and Another is
@eb.Con!ig. @eb.Con!ig is dedicated to indiid(al applications where in
Machine.Con!ig is the common !or a machine. So better (se @eb.Con!ig !or connection
in!ormation.
+n web.Con!ig
6con!ig(ration9
6appsettings9
6add 1ey="DS>" al(e="Data So(rce=localhost'.ser id=sa' password=////" ;9
6add 1ey="S4lAry" Bal(e="Select C !rom C(stomers" ;9
6appsettings9
6con!ig(ration9
sae the web.con!ig !ile in applications root then goto yo(r page where yo( can e/tract
the al(e as
dim StrConn as String= con!ig(ration.appsettings,"DS>"-
dim StrS4l as String=con!ig(ration.appsettings,"S4lAry"-
>ow connection string and S4l A(ery are aaialble in the page. So writing in the
con!ig(ration !ile is better !or readonly in!ormation.
@hen comes to ASM= !iles it is the e/tension !or @eb serices. <o( can write the same
!(nctionality and more in webserices. + can compare this web serices are ?(st li1e
stored proced(res in o(r database which seres a partic(lr p(rpose.+ts lenghty to e/pain
here. So i! yo( need more on @ebSeices yo( better search in Asp:ree.com.