Using cn As New SqlClient.SqlConnection
cn.ConnectionString = "Data Source=xxxxx;Initial Catalog=northwind;user ID=sa;password=xxxxxxxx"
cn.Open()
Using cmd As New SqlClient.SqlCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = RichTextBox1.Text
cmd.Connection = cn
Using dr As SqlClient.SqlDataReader = cmd.ExecuteReader
Dim lColumn01 As Integer = dr.GetOrdinal("CategoryID")
Dim lColumn02 As Integer = dr.GetOrdinal("CategoryName")
Dim lColumn03 As Integer = dr.GetOrdinal("Description")
Dim lCount As Integer = dr.FieldCount - 1
Dim Values(lCount) As Object
Dim lRead As Boolean = False
Dim lElement As XElement
While dr.Read
dr.GetValues(Values)
If lRead = False Then
lElement = New XElement("Categorias", _
New XElement("Categoria", New XElement("CategoryID", Values(lColumn01)), _
New XElement("CategoryName", Values(lColumn02)), _
New XElement("Description", Values(lColumn03))))
Else
lElement.Add(New XElement("Categorias", _
New XElement("Categoria", New XElement("CategoryID", Values(lColumn01)), _
New XElement("CategoryName", Values(lColumn02)), _
New XElement("Description", Values(lColumn03)))))
End If
lRead = True
End While
lElement.Save(txtUrl.Text)
MsgBox("Datos Grabados en " & txtUrl.Text)
End Using
End Using
End Using
para leer los datos del archivo XML he usado Linq to XML para mostrar los datos en el datagridview
Dim lCategoriaXml As XDocument = XDocument.Load(txtUrl.Text)
Dim lCategorias = From lcategoria In lCategoriaXml.Descendants("Categoria") _
Select CategoryID = lcategoria.Element("CategoryID").Value, _
CategoryName = lcategoria.Element("CategoryName").Value, _
Description = lcategoria.Element("Description").Value
DataGridView1.DataSource = lCategorias.ToList
puedes descargar el código del ejemplo aquí.
No hay comentarios.:
Publicar un comentario