0% found this document useful (0 votes)
50 views4 pages

XML-Thuc - Hanh - 6-Export Database To XML

This document describes two methods for converting data from a database to XML format. The first method uses Access 2003 data and shows code for exporting the data to an XML file. The second method discusses using data from a SQL Server 2005 database, including connecting to the database, filling a dataset, and writing the dataset to an XML file. It also mentions displaying the XML data in a ListView control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

XML-Thuc - Hanh - 6-Export Database To XML

This document describes two methods for converting data from a database to XML format. The first method uses Access 2003 data and shows code for exporting the data to an XML file. The second method discusses using data from a SQL Server 2005 database, including connecting to the database, filling a dataset, and writing the dataset to an XML file. It also mentions displaying the XML data in a ListView control.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

DATABASE TO XML

Co nhiu phng phap chuyn d liu t mt CSDL quan h thanh d liu XML.
Trong phn nay trinh bay phng phap chuyn d liu Access 2003, SQL Server 2005
sang d liu XML
1. S dng d liu ca CSDL Access 2003
'----------------------------------------------------------------------
' PathName :DataToXML\DataToXML\Data\SV.MDB
' SV.MDB has table SV(HoTen Char(3!"Lo# Char($%!!
'----------------------------------------------------------------------
&m#orts S'stem.Xml
&m#orts S'stem.Console
&m#orts S'stem.Data
&m#orts S'stem.Data.(leDb
Mo)*le +,#ortDataToXml
D-m ,mlDo. /s Ne0 XmlDo.*ment
D-m ,ml1-le /s Str-n2 3 4..5..5Data5S6.Xml4
Sub Main()
+,#ortToXML(!
'7ea) XML )ata
Console.8r-teL-ne(4Danh sa.h s-nh 6-en )a no# ho. #h- ta- N2an
Han2:4 9 6bCrL:!
7ea)XMLtoConsole(,ml1-le!
End Sub
Sub ExportToXML()
' B;n .<n n=m 6>n2 ?@ th*At sB )Cn2 DE- tFGn2 /D/PT+7 6H D/T/S+T
' CI ha- DJ* nKm tron2 ?hLn2 2-an tMn S'stem.Data.SNlCl-ent
D-m #ath /s Str-n2 3 4..5..5Data5S6.m)b4
D-m strSOL /s Str-n2 3 4Sele.t P :rom SV4
'Ban .an n2h-en .** ha- ?ha- n-em Data/)a#ter 6a DataSet tron2 /D(.N+T
'Qhon2 .o ?ha- n-em 7e.or)Set nhF /D( n*a" DR' lH /D(.N+TS
D-m obTDataSet /s Ne0 DataSet
D-m obTCon /s (leDbConne.t-on
D-m strCon /s Str-n2
strCon 3 4Pro6-)er3M-.roso:t.Uet.(leDb.V.WData So*r.e34 9 #ath
1
obTCon 3 Ne0 (leDbConne.t-on(strCon!
D-m obT/)a#ter /s Ne0 (leDbData/)a#ter(strSOL" obTCon!
'+,#ort )ata :rom Data/)a#ter to DataSet
obT/)a#ter.1-ll(obTDataSet!
obTCon.Close(!
'+,#ort )ata :rom DataSet to XML :-le
,mlDo..Loa)Xml(obTDataSet.XetXml(!!
,mlDo..Sa6e(,ml1-le!
Ms2Bo,(4+,#ort .om#lete)S4!
End Sub
Sub ReadXMLtoConsole(B'Val ,mlPath /s Str-n2!
' YZ. t[# XML 6H -n ra mHn h\nh" 6-[. nH' D] lHm N*en nh-J* r^-
D-m HoTen /s Str-n2
D-m Lo# /s Str-n2

D-m str /s Str-n2 3 44
D-m - /s &nte2er 3 $
,mlDo..Loa)(,mlPath!
D-m L-st /s XmlNo)eL-st 3 ,mlDo..Xet+lementsB'Ta2Name(4Table4!
1or +a.h N*t /s Xml+lement &n L-st
HoTen 3 N*t.1-rstCh-l).&nnerTe,t
Lo# 3 N*t.LastCh-l).&nnerTe,t
'Ch* ' .a.h 2he# .h*o- tron2 VB.N+T" t*on2 t*: str_3.h*o-W
str 93 -.ToStr-n2 9 4"4 9 HoTen 9 4"4 9 Lo# 9 6bCrL:
- 3 - _ $
Ne,t
Console.8r-te(str!
Console.7ea)L-ne(!
End Sub
+n) Mo)*le
2. S dng d liu ca CSDL SQL Server 2005
'----------------------------------------------------------------------
' PathName : \..\..\SV.XML (relat-6e Pathname!
'----------------------------------------------------------------------
Gia su ta xy dung mt ung dung Window voi VB.NET, Form co dang nh trinh bay. O
y co 2 iu khin:
2
Button: Execute (chuyn d liu SQL Server-~XML) va View XML (Xem d liu
cua tp XML trong ListView)
ListView : trinh bay d liu XML, y la iu khin lam trong cac bai troc.
C so d liu QLSV.MDF co table SV(HoTen, Lop, QueQuan, Diem)
D hiu ly thuyt va cach su dung i tong Adapter, Dataset : xem bai giang cua
NDTFIT
Khi to ListView khi load Form
Pr-6ate S*b For!"Load(B'Val sen)er /s S'stem.(bTe.t" B'Val e /s
S'stem.+6ent/r2s! Han)les M'Base.Loa)
L-stV-e0$.V-e0 3 V-e0.Deta-ls
L-stV-e0$.Col*mns./))(4HZ tMn4" %%" Hor-`ontal/l-2nment.Le:t!
L-stV-e0$.Col*mns./))(4La#4" b" Hor-`ontal/l-2nment.Le:t!
L-stV-e0$.Col*mns./))(4Yca .hd4" %V" Hor-`ontal/l-2nment.Le:t!
L-stV-e0$.Col*mns./))(4Y-em4" f%" Hor-`ontal/l-2nment.Le:t!
+n) S*b
c d liu trong CSDL SL Ser!er !" chu#$n sang t% SV&'(L
Quy trinh nh sau:
- Kt ni CSDL
Khai bao Connection String phu hop voi loai CSDL
Open Connection
3
- To !i t"#n$ %&apte'
Chu y hai tham s : chui kt ni va cu lnh SQL
- (i)n !*+ (,i--) &. -i/u t0 %&apte' 12o !i t"#n$ Dataset
- C3u+4n &. -i/u t'on$ Dataset san$ t/p 5ML
T36t !7n $i8n9 C:i ;3<n3 -2 =it >?+ &@n$ ;3uAi Bt ni ;3o SQL Se'1e' (37i B3C n3D)9
Pr-6ate S*b btnExe#ute"Cli#$(B'Val sen)er /s S'stem.(bTe.t" B'Val e /s
S'stem.+6ent/r2s! Han)les B*tton$.Cl-.?
D-m #ath /s Str-n2 3 4..5..5SV.XML4
D-m strCon /s Str-n2 3 4Data So*r.e3NDT\SOL+,#ressW&n-t-al
Catalo23SVW gser &D3THWPass0or)3W Tr*ste)hConne.t-on3ies4
j gser name lH TH" Dkn2 nhA# 6a- N*'Jn .la 8-n)o0s
D-m .onne.t-on /s Ne0 SNlConne.t-on(strCon!
D-m a)a#ter /s Ne0 SNlData/)a#ter
D-m )s /s Ne0 DataSet
D-m strSNl /s Str-n2
strSNl 3 4Sele.t P :rom SV4
Tr'
.onne.t-on.(#en(!
a)a#ter 3 Ne0 SNlData/)a#ter(strSNl" strCon!
a)a#ter.1-ll()s!
.onne.t-on.Close(!
)s.8r-teXml(#ath!
Ms2Bo,(4DoneS4!
Cat.h e, /s +,.e#t-on
Ms2Bo,(e.ToStr-n2!
+n) Tr'
+n) S*b
c d liu trong t% SV&'(L !" hi$n th) tr*n ListView : ban tu lam
Pr-6ate S*b btnVie%"Cli#$(B'Val sen)er /s S'stem.(bTe.t" B'Val e /s
S'stem.+6ent/r2s! Han)les btnV-e0.Cl-.?
j Tm lHm
+n) S*b

4

You might also like