You are on page 1of 13

v8nL1 Pow Lo connecL Lo SCL Server

1hls Loplc ls a parL of v8nL1 SCL uaLabase 8aslcs 1uLorlal serles Cn Lhls arLlcle you wllllearn how Lo
connecL Lo any verslon of SCL server uslng v8nL1 lor programmersdaLabase programmlng ls a musL lf
your worklng on daLa orlenLed companles 8efore you can lnserL updaLe or query records from
daLabase you musL flrsL know how Lo connecL Lo lL and Lhls ls my ob[ecLlve for Lhls arLlcle AL Lhe end of
Lhls arLlcle you should be able Lo know how easy Lo connecL Lo SCL server


noLe l assumed LhaL you already lnsLalled your SCL Server and vlsual SLudlo nL1

Pere ls Lhe sLepbysLep procedure Lo connecL Lo SCL server
1 CreaLe your v8nL1 pro[ecL
2 lnclude Lhe followlng namespaces

lmporLs SysLemuaLa
lmporLs SysLemuaLaSqlCllenL

1he SysLemuaLa namespace provldes access Lo classes LhaL represenL Lhe AuCnL1 archlLecLure whlle
Lhe SysLemuaLaSqlCllenL namespace ls LhenL1 lramework uaLa rovlder for SCL Server
3 ueclare and lnsLanLlaLe your SCLConnecLlon ob[ecL as shown below

ulm con As new SqlConnecLlon

SCLConnecLlon class represenLs an open connecLlon Lo a SCL Server daLabase
4 ass Lhe SCL connecLlon sLrlng Lo ConnecLlonSLrlng properLy of your SqlConnecLlon ob[ecL

conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy lnfo1rueuser
lusaassword12343678

1he connecLlonsLrlng value usually conLalns Lhe followlng
uaLa Source physlcal server hosLname
lnlLlal CaLalog your daLabase name
user lu SCL username use Lo connecL Lo Lhe server
assword SCL usernames password

Cn Lhls sample l am uslng an SCL Server 2003 lor Lhe connecLlonsLrlng for oLher SCL verslon you can
geL lL from here hLLp//wwwconnecLlonsLrlngscom/

3 LasL sLep ls Lo lnvoke Lhe Cpen meLhod of Lhe connecLlon ob[ecL

conCpen()


1he comp|ete samp|e sourcecode

lmporLs SysLemuaLaSqlCllenL
lmporLs SysLemuaLa

rlvaLe Sub ConnecL1oSCL()
ulm con As new SqlConnecLlon
ulm cmd As new SqlCommand

conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy
lnfo1rueuser lusaassword12343678
conCpen()
Lnd Sub


1o capLure lf Lhe connecLlon was successful or noL [usL Lweak Lhe above code

lmporLs SysLemuaLaSqlCllenL
lmporLs SysLemuaLa

rlvaLe Sub ConnecL1oSCL()
ulm con As new SqlConnecLlon
ulm cmd As new SqlCommand
1ry
conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy
lnfo1rueuser lusaassword12343678
conCpen()
CaLch ex As LxcepLlon
Message8oxShow(Lrror whlle connecLlng Lo SCL Server exMessage) llnally
conClose() WheLher Lhere ls error or noL Close Lhe connecLlon
Lnd 1ry
Lnd Sub
v8nL1 Pow Lo SLLLC1 records from SCL uaLabase
1hls Loplc ls a parL of v8nL1 SCL uaLabase 8aslcs 1uLorlal serles Cn Lhls arLlcle you wllllearn how Lo
reLrleve records from an SCL uaLabase server uslng v8nL1 uaLa reLrleval ls a common Lask of buslness
appllcaLlons lL can be use for vlew and search purposes AL Lhe end of Lhls arLlcle you should be able Lo
know how easy Lo query or SLLLC1 recordsfrom SCL server


noLe l assumed LhaL you already lnsLalled your SCL Server and vlsual SLudlo nL1

Pere ls Lhe sLepbysLep procedure Lo query from SCL server
1 CreaLe your v8nL1 pro[ecL
2 lnclude Lhe followlng namespaces

lmporLs SysLemuaLa
lmporLs SysLemuaLaSqlCllenL

1he SysLemuaLa namespace provldes access Lo classes LhaL represenL Lhe AuCnL1 archlLecLure whlle
Lhe SysLemuaLaSqlCllenL namespace ls LhenL1 lramework uaLa rovlder for SCL Server

3 ueclare and lnsLanLlaLe your SCLConnecLlon ob[ecL and Command ob[ecL as shown below

ulm con As new SqlConnecLlon
ulm cmd As new SqlCommand

4 ass Lhe SCL connecLlon sLrlng Lo ConnecLlonSLrlng properLy of your SqlConnecLlon ob[ecL

conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy lnfo1rueuser
lusaassword12343678


3 lnvoke Lhe Cpen MeLhod Lo connecL Lo SCL Server

conCpen()

6 SeL Lhe connecLlon of Lhe command ob[ecL

cmdConnecLlon con

7 ass Lhe SLLLC1 SCL sLaLemenL Lo Lhe command ob[ecL commandLexL as shown below

cmdCommand1exL SLLLC1 fleld1 fleld2 l8CM Lablename C8uL8 8? sorLfleld

8 use Lhe LxecuLe8eader() meLhod Lo run Lhe SLLLC1 SCL

ulm lrd As SqluaLa8eader cmdLxecuLe8eader()

*xecotekeoJet() 5eoJs tbe commooJ1ext to tbe coooectloo ooJ bollJs o 5pluotokeoJet
* 5pluotokeoJet ltovlJes o meoos of teoJloq o fotwotJooly stteom of tows ftom o
5Ol 5etvet Jotobose

9 Loop Lhrough Lhe SqluaLa8eader

Whlle lrd8ead()
uo someLhlng
Lnd Whlle


1he fu|| samp|e source code of Se|ect|ng records from SL Database

ulm con As new SqlConnecLlon
ulm cmd As new SqlCommand
1ry
conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy
lnfo1rueuser lusaassword12343678
conCpen()
cmdConnecLlon con
cmdCommand1exL SLLLC1 fleld1 fleld2 l8CM Lablename C8uL8 8? sorLfleld
ulm lrd As SqluaLa8eader cmdLxecuLe8eader()

Whlle lrd8ead()
uo someLhlng here
Lnd Whlle

CaLch ex As LxcepLlon
Message8oxShow(Lrror whlle reLrlevlng records on Lable exMessage Load 8ecords)
llnally
conClose()
Lnd 1ry





















VB.NET: HUW TU INSERT RECURD TU SQL DATABASE
1hls Loplc ls parL of v8nL1 SCL uaLabase 8aslcs 1uLorlal serles Cn Lhls arLlcle you wlll learn how Lo
lncorporaLe SCL lnSL81 sLaLemenLs on your v8nL1 appllcaLlon An SCL lnSL81 sLaLemenL adds one or
more records Lo any slngle Lable ln a relaLlonal daLabase lor Lhls arLlcle we wlll focus parLlcularly on
MlcrosofL SCL Server



noLe l assumed LhaL you already lnsLalled your SCL Server and vlsual SLudlo nL1

Pere ls Lhe sLepbysLep procedure Lo lnSL81 record Lo SCL server
1 CreaLe your v8nL1 pro[ecL
2 lnclude Lhe followlng namespaces

lmporLs SysLemuaLa
lmporLs SysLemuaLaSqlCllenL

1he SysLemuaLa namespace provldes access Lo classes LhaL represenL Lhe AuCnL1 archlLecLure whlle
Lhe SysLemuaLaSqlCllenL namespace ls LhenL1 lramework uaLa rovlder for SCL Server

3 ueclare and lnsLanLlaLe your SCLConnecLlon ob[ecL and Command ob[ecL as shown below

ulm con As new SqlConnecLlon
ulm cmd As new SqlCommand

4 ass Lhe SCL connecLlon sLrlng Lo ConnecLlonSLrlng properLy of your SqlConnecLlon ob[ecL

conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy lnfo1rueuser
lusaassword12343678


3 lnvoke Lhe Cpen MeLhod Lo connecL Lo SCL Server

conCpen()

6 SeL Lhe connecLlon of Lhe command ob[ecL

cmdConnecLlon con

7 ass Lhe lnSL81 SCL sLaLemenL Lo Lhe command ob[ecL commandLexL as shown below

cmdCommand1exL lnSL81 ln1C Lable (fleld1 fleld2 ) vALuLS (value1 value2 )

8 use Lhe LxecuLenonCuery() meLhod Lo run lnSL81 SCL LhaL has no reLurn value

cmdLxecuLenonCuery()


lf you expecL a reLurn value use LxecuLeScalar() lnsLead A good example of LxecuLeScalar ls Lo geL Lhe
ldenLlLy column afLer lnserLlng record
(LxecuLe Scalar reLurn Lhe flrsL column of Lhe flrsL row from Lhe resulL)


1he fu|| samp|e source code of |nsert|ng record to SL Database

ulm con As new SqlConnecLlon
ulm cmd As new SqlCommand
1ry
conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy
lnfo1rueuser lusaassword12343678
conCpen()
cmdConnecLlon con
cmdCommand1exL lnSL81 ln1C Lable(fleld1 fleld2) vALuLS(value1 value2)
cmdLxecuLenonCuery()

CaLch ex As LxcepLlon
Message8oxShow(Lrror whlle lnserLlng record on Lable exMessage lnserL 8ecords)
llnally
conClose()
Lnd 1ry


v8nL1 Pow Lo uuA1L record on SCL uaLabase
1hls Loplc ls parL of v8nL1 SCL uaLabase 8aslcs 1uLorlal serles Cn Lhls arLlcle you wllllearn how Lo
lncorporaLe SCL uuA1L sLaLemenLs on your v8nL1 appllcaLlon An SCL uuA1L sLaLemenL LhaL
changes Lhe daLa of one or more records ln a Lable LlLher all Lhe rows can be updaLed or a subseL may
be chosen uslng a condlLlon lor Lhls arLlcle we wlll focus parLlcularly on updaLlng records ln
MlcrosofL SCL Server



noLe l assumed LhaL you already lnsLalled your SCL Server and vlsual SLudlo nL1

Pere ls Lhe sLepbysLep procedure Lo uuA1L record on SCL server
1 CreaLe your v8nL1 pro[ecL
2 lnclude Lhe followlng namespaces
vlew plalnprlnL?
1 mports SysLemuaLa
2 mports SysLemuaLaSqlCllenL


1he SysLemuaLa namespace provldes access Lo classes LhaL represenL Lhe AuCnL1 archlLecLure whlle
Lhe SysLemuaLaSqlCllenL namespace ls LhenL1 lramework uaLa rovlder for SCL Server

3 ueclare and lnsLanLlaLe your SCLConnecLlon ob[ecL and Command ob[ecL as shown below
vlew plalnprlnL?
1 D|m con As New SqlConnecLlon
2 D|m cmd As New SqlCommand

4 ass Lhe SCL connecLlon sLrlng Lo ConnecLlonSLrlng properLy of your SqlConnecLlon ob[ecL
vlew plalnprlnL?
1 conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal
2 CaLalog8llllngSyserslsL SecurlLy lnfo1rueuser lusaassword12343678


3 lnvoke Lhe Cpen MeLhod Lo connecL Lo SCL Server
vlew plalnprlnL?
1 conCpen()


6 SeL Lhe connecLlon of Lhe command ob[ecL
vlew plalnprlnL?
1 cmdConnecLlon con


7 ass Lhe uuA1L SCL sLaLemenL Lo Lhe command ob[ecL commandLexL as shown below
vlew plalnprlnL?
1 cmdCommand1exL uuA1L Lable SL1 fleld1 value1 fleld2 value2
2 WPL8L fleld31esL


8 use Lhe LxecuLenonCuery() meLhod Lo run Lhe uuA1L SCL sLaLemenL
vlew plalnprlnL?
1 cmdLxecuLenonCuery()


1he fu|| samp|e source code of updat|ng records on SL Database
vlew plalnprlnL?
1 D|m con As New SqlConnecLlon
2 D|m cmd As New SqlCommand
3 1ry
4 conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal
3 CaLalog8llllngSyserslsL SecurlLy lnfo1rueuser lusaassword12343678
6 conCpen()
7 cmdConnecLlon con
8 cmdCommand1exL uuA1L Lable SL1 fleld1 value1 fleld2 value2
9 WPL8L fleld31esL
10 cmdLxecuLenonCuery()
11
12 atch ex As LxcepLlon
13 Message8oxShow(Lrror whlle updaLlng record on Lable exMessage updaLe 8ecords)
14 |na||y
13 conClose()
16 nd 1ry













v8nL1 Pow Lo uLLL1L record on SCL uaLabase
1hls Loplc ls parL of v8nL1 SCL uaLabase 8aslcs 1uLorlal serles Cn Lhls arLlcle you wlll learn how Lo
lncorporaLe SCL uLLL1L sLaLemenLs on your v8nL1 appllcaLlon An SCL uLLL1L sLaLemenL removes one
or more records from a Lable A subseL may be deflned for deleLlon uslng a condlLlon oLherwlse all
records are removed lor Lhls arLlcle we wlll focus parLlcularly on updaLlng records ln MlcrosofL SCL
Server



noLe l assumed LhaL you already lnsLalled your SCL Server and vlsual SLudlo nL1

Pere ls Lhe sLepbysLep procedure Lo uuA1L record on SCL server
1 CreaLe your v8nL1 pro[ecL
2 lnclude Lhe followlng namespaces
vlew plalnprlnL?
1 mports SysLemuaLa
2 mports SysLemuaLaSqlCllenL


1he SysLemuaLa namespace provldes access Lo classes LhaL represenL Lhe AuCnL1 archlLecLure whlle
Lhe SysLemuaLaSqlCllenL namespace ls LhenL1 lramework uaLa rovlder for SCL Server

3 ueclare and lnsLanLlaLe your SCLConnecLlon ob[ecL and Command ob[ecL as shown below
vlew plalnprlnL?
1 D|m con As New SqlConnecLlon
2 D|m cmd As New SqlCommand


4 ass Lhe SCL connecLlon sLrlng Lo ConnecLlonSLrlng properLy of your SqlConnecLlon ob[ecL
vlew plalnprlnL?
1 conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal
2 CaLalog8llllngSyserslsL SecurlLy lnfo1rueuser lusaassword12343678


3 lnvoke Lhe Cpen MeLhod Lo connecL Lo SCL Server
vlew plalnprlnL?
1 conCpen()


6 SeL Lhe connecLlon of Lhe command ob[ecL
vlew plalnprlnL?
1 cmdConnecLlon con


7 ass Lhe uLLL1L SCL sLaLemenL Lo Lhe command ob[ecL commandLexL as shown below
vlew plalnprlnL?
1 cmdCommand1exL uLLL1L l8CM Lable_name WPL8L condlLlon
2 lor Lxample
3 uLLL1L l8CM cusLomer WPL8L flrsLnamelryan0911


8 use Lhe LxecuLenonCuery() meLhod Lo run Lhe uLLL1L SCL sLaLemenL
vlew plalnprlnL?
1 cmdLxecuLenonCuery()


1he fu|| samp|e source code of de|et|ng records on SL Database
vlew plalnprlnL?
1 D|m con As New SqlConnecLlon
2 D|m cmd As New SqlCommand
3 1ry
4 conConnecLlonSLrlng uaLa SourceaLlsourcelnlLlal CaLalog8llllngSyserslsL SecurlLy lnfo1r
ueuser lusaassword12343678
3 conCpen()
6 cmdConnecLlon con
7 cmdCommand1exL uLLL1L l8CM Lable_name WPL8L fleld11esL
8 cmdLxecuLenonCuery()
9
10 atch ex As LxcepLlon
11 Message8oxShow(Lrror whlle deleLlng record on Lable exMessage ueleLe 8ecords)
12 |na||y
13 conClose()
14 nd 1ry

You might also like