The System.Data Namespace[2]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

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

本文关键:The System.Data Namespace
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top