Professional Documents
Culture Documents
XML Thuc Hanh 6 Export Database To XML
XML Thuc Hanh 6 Export Database To XML
XML Thuc Hanh 6 Export Database To XML
C nhiu phng php chuyn d liu t mt CSDL quan h thnh d liu XML. Trong phn ny trnh by phng php chuyn d liu Access 2003, SQL Server 2005 sang d liu XML
objCon = New OleDbConnection(strCon) Dim objAdapter As New OleDbDataAdapter(strSQL, objCon) 'Export data from DataAdapter to DataSet objAdapter.Fill(objDataSet) objCon.Close() 'Export data from DataSet to XML file xmlDoc.LoadXml(objDataSet.GetXml()) xmlDoc.Save(xmlFile) MsgBox("Export completed!") End Sub Sub ReadXMLtoConsole(ByVal xmlPath As String) ' c tp XML v in ra mn hnh, vic ny lm quen nhiu ri Dim HoTen As String Dim Lop As String Dim str As String = "" Dim i As Integer = 1 xmlDoc.Load(xmlPath) Dim List As XmlNodeList = xmlDoc.GetElementsByTagName("Table") For Each Nut As XmlElement In List HoTen = Nut.FirstChild.InnerText Lop = Nut.LastChild.InnerText 'Chu y cach ghep chuoi trong VB.NET, tuong tu: str+=chuoi; str &= i.ToString & "," & HoTen & "," & Lop & vbCrLf i = i + 1 Next Console.Write(str) Console.ReadLine() End Sub End Module
Button: Execute (chuyn d liu SQL Server->XML) v View XML (Xem d liu ca tp XML trong ListView) ListView : trnh by d liu XML, y l iu khin lm trong cc bi trc. C s d liu QLSV.MDF c table SV(HoTen, Lop, QueQuan, Diem)
hiu l thuyt v cch s dng i tng Adapter, Dataset : xem bi ging ca NDTFIT
Khi to ListView khi load Form
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListView1.View = View.Details ListView1.Columns.Add("H tn", 220, HorizontalAlignment.Left) ListView1.Columns.Add("Lp", 80, HorizontalAlignment.Left) ListView1.Columns.Add("a ch", 240, HorizontalAlignment.Left) ListView1.Columns.Add("im", 62, HorizontalAlignment.Left) End Sub
Quy trnh nh sau: - Kt ni CSDL Khai bo Connection String ph hp vi loi CSDL Open Connection
- To i tng Adapter Ch hai tham s : chui kt ni v cu lnh SQL - in y (fill) d liu t Adapter vo i tng Dataset - Chuyn d liu trong Dataset sang tp XML Tht n gin! Ci chnh l bit xy dng chui kt ni cho SQL Server (hi kh nh)!
Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim path As String = "../../SV.XML" Dim strCon As String = "Data Source=NDT\SQLExpress;Initial Catalog=SV; User ID=TH;Password=; Trusted_Connection=Yes" User name l TH, ng nhp vi quyn ca Windows Dim connection As New SqlConnection(strCon) Dim adapter As New SqlDataAdapter Dim ds As New DataSet Dim strSql As String strSql = "Select * from SV" Try connection.Open() adapter = New SqlDataAdapter(strSql, strCon) adapter.Fill(ds) connection.Close() ds.WriteXml(path) MsgBox("Done!") Catch ex As Exception MsgBox(e.ToString) End Try End Sub
Private Sub btnView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnView.Click T lm End Sub