© 2004 Microsoft Corporation. All rights reserved.
Figure 1 Form_Load Code
Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
Dim ds As DataSet
Dim rw As DataRow
Dim cl As DataColumn

Dim sSQL As String

sSQL = "select * from employees"

ds = RunSQLWithDataSet(sSQL, "Employees")

DataGrid1.DataSource = ds.Tables("Employees")

lblTraceListener.Text = Debug.Listeners.Item(0).Name

Debug.WriteLine("—————————————————" "————————")
Debug.WriteLine("                    Employee DS Data")

For Each rw In ds.Tables("Employees").Rows

Debug.Write(rw.Item("EmployeeID") & vbTab)
Debug.Write(rw.Item("LastName") & vbTab)
Debug.Write(rw.Item("FirstName") & vbTab)
Debug.Write(rw.Item("Title") & vbTab)
Debug.WriteLine(rw.Item("BirthDate"))
Next
Debug.WriteLine("—————————————————" "————————")
Figure 2 Data Hierarchy

Figure 2 Data Hierarchy Figure 3 RunSQLWithDataSet
Public Function RunSQLWithDataSet(ByVal sSQL As String, _
    Optional ByVal sTable As String = "Table1") As DataSet
        Dim oDataSet As New DataSet()
        Dim oDataAdapter As SqlClient.SqlDataAdapter

        Try

            'Create New DataAdapter 
            oDataAdapter = New _
            SqlClient.SqlDataAdapter( _
            sSQL, ConnectionString)

            'Fill Data Set from dataadapter
            oDataAdapter.Fill(oDataSet, sTable)

            'Set return value of function
            Return oDataSet
        Catch e As Exception
            Throw New Exception( _
            "An exception occured in RunSQLWithDataSet", e)
        Finally
        End Try
End Function
Figure 4 ShowDSInOutputWindow
Function ShowDSInOutputWindow(ByVal ds As DataSet, _
Optional ByVal sTable As String = "")

'Generic DataSet debug function
Dim cldebug As DataColumn
Dim tbdebug As DataTable
Dim rw As DataRow

If sTable = "" Then
    tbdebug = ds.Tables(0)
Else
    tbdebug = ds.Tables(sTable)
End If

Debug.WriteLine("—————————————————")
Debug.WriteLine(vbTab & vbTab & "DS Table: " & _
    tbdebug.ToString()) 
Dim bFirst As Boolean = True

For Each rw In tbdebug.Rows
  If bFirst Then
    For Each cldebug In tbdebug.Columns
      If cldebug.DataType.Name <> "Byte[]" Then
        Debug.Write( _
          cldebug.ColumnName & vbTab)
        End If
      Next cldebug
      Debug.WriteLine("")
        bFirst = False
    End If

  For Each cldebug In tbdebug.Columns
    If cldebug.DataType.Name <> "Byte[]" Then
      Debug.Write(rw.Item(cldebug) & vbTab)
    End If
  Next cldebug
            Debug.WriteLine("")
Next
Debug.WriteLine("—————————————————")
End Function
Figure 5 Output
————————————————————————————
DS Table: Employees ran at 3/30/2002 5:42:02 PM EmployeeID LastName FirstName Title TitleOfCourtesy BirthDate HireDate Address City Region PostalCode Country HomePhone Extension Notes ReportsTo PhotoPath
1 Davolio Nancy Sales Representative Ms. 12/8/1948 5/1/1992 507 - 20th Ave. E. Apt. 2A Seattle WA 98122 USA (206) 555-9857 5467 Education includes a BA in psychology from Colorado State University in 1970. She also completed "The Art of the Cold Call." Nancy is a member of Toastmasters International. 2 http://accweb/emmployees/davolio.bmp
2 Fuller Andrew Vice President, Sales Dr. 2/19/1952 8/14/1992 908 W. Capital Way Tacoma WA 98401 USA (206) 555-9482 3457 Andrew received his BTS commercial in 1974 and a Ph.D. in international marketing from the University of Dallas in 1981. He is fluent in French and Italian and reads German. He joined the company as a sales representative, was promoted to sales manager in January 1992 and to vice president of sales in March 1993. Andrew is a member of the Sales Management Roundtable, the Seattle Chamber of Commerce, and the Pacific Rim Importers Association. http://accweb/emmployees/fuller.bmp
3 Leverling Janet Sales Representative Ms. 8/30/1963 4/1/1992 722 Moss Bay Blvd. Kirkland WA 98033 USA (206) 555-3412 3355 Janet has a BS degree in chemistry from Boston College (1984). She has also completed a certificate program in food retailing management. Janet was hired as a sales associate in 1991 and promoted to sales representative in February 1992. 2 http://accweb/emmployees/leverling.bmp
————————————————————————————

Home
Topics
Issues and Downloads
Script Junkie
Subscribe
Submit an Article
RSS

MSDN Magazine