bDataAdapter and DataSet.
The procedure shown in this section, XMLGet, first creates a connection
string and a SQL string. It passes these two strings to the constructor of
the OleDbDataAdapter object. The code then calls the OleDbDataAdapter
object's Fill method in order to fill the DataSet object with data.
NOTE
You don't need to type System.Data.OleDb.OleDbDataAdapter here because VB
.NET automatically includes an Imports statement that imports the
System.Data namespace.
The Fill method is responsible for creating an implicit OleDbConnection
object using the supplied connection string. Next, the Fill method creates
an OleDbCommand object and uses the SELECT statement to read the data from
the data source. The Fill method reads the data and fills one table within
the DataSet object. The code closes the collection, leaving you with just
the filled DataSet object.
The DataSet object's GetXml method returns its entire set of data as an XML
stream. Your code can handle the XML in any way it requires梩he following
code snippet places the XML into a text box on the sample page:
Private Sub XMLGet()
Dim strConn As String
Dim strSQL As String
Dim da As OleDb.OleDbDataAdapter
Dim ds As DataSet
strConn = "Provider=sqloledb;Data
Source=(local);Initial Catalog=Northwind;User ID=sa"
strSQL = "SELECT ProductID, ProductName FROM Products"
da = New OleDb.OleDbDataAdapter(strSQL, strConn)
ds = New DataSet()
da.Fill(ds)
txtXML.Text = ds.GetXml()
End Sub
If you know that you are only going to be using SQL Server, you might
choose to use the SqlClient namespace. The classes in this namespace are
specific for SQL Server and will give you a little better performance when
working with SQL Server. The following code snippet provides the same
functionality, using the SqlClient namespace classes:
Private Sub XMLGetSqlClient()
Dim strConn As String
Dim strSQL As String
Dim da As SqlClient.SqlDataAdapter
Dim ds As DataSet
strConn = "Server=(local);Database=Northwind;" _
"User ID=sa"
strSQL = "SELECT ProductID, ProductName FROM Products"
da = New SqlClient.SqlDataAdapter(strSQL, strConn)
ds = New DataSet()
da.Fill(ds)
txtXML.Text = ds.GetXml()
End Sub