Search code examples
sql-serverxmlvb.netado.netsqlxml

Use SQLXML with VB.NET


I'm new in .NET programming and Visual Basic and I have no much idea about it. I have the next code:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim cnn As New SqlConnection("Server=adserver;uid=sa;pwd=1234;dat abase=empatic")
        Dim da As New SqlDataAdapter("select * from producte for xml path('producte'),     root('Productes')", cnn)
        Dim ds As New DataSet        
        da.Fill(ds)

        DataGridView1.DataSource = ds.Tables(0)
        Dim a As String = ds.Tables(0).ToString

    End Sub
End Class

What this code does is connect to a SQL Server and execute a query. This query is generated in a single XML line in a GridViewer. My question is: How I can do to get the information when in VB, show me the xml-shaped rather than a single line.

The ultimate purpose of all this is that the VB to connect to SQL Server and clicking a button the program show me the xml I generated with the query.


Solution

  • Do you want the data as XML? If not, just remove the for xml path... from your SQL statement. For instance, you could just do this:

    Dim da As New SqlDataAdapter("select * from producte", cnn)
    

    If you do want it as XML, then there are several different options available to you for parsing the XML. Here are the most common options: