DataSet using Code

Posted on 2008-10-07
Last Modified: 2010-04-23
DataSet using Code
Question by:JPTenore
  • 2
LVL 53

Expert Comment

ID: 22657429
LVL 53

Accepted Solution

Dhaest earned 500 total points
ID: 22657434
A complete example to connect to the database, fill a dataset and loop through that dataset
Imports System

Imports System.Collections

Imports System.Data

Imports System.IO

Imports System.Xml.Serialization

Imports System.Windows.Forms

Imports System.Data.SqlClient



Public Class MainClass

    Shared Dim WithEvents con As SqlConnection


    Shared Sub Main()

        con = New SqlConnection("Server=(local)\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=SSPI") 


        ' Create the command object

        Dim str As String = "SELECT ID, FirstName, LastName FROM Employee"

        Dim cmd As New SqlCommand(str, con)


        Dim da As New SqlDataAdapter(cmd)


        Dim ds As New DataSet()


        da.Fill(ds, "Employee")


        Dim dc As DataColumn

        For Each dc In ds.Tables(0).Columns

            Console.Write("{0,15}", dc.ColumnName)





        Dim dr As DataRow

        For Each dr In ds.Tables(0).Rows

            Dim i As Integer

            For i = 1 To ds.Tables(0).Columns.Count

                Console.Write("{0,15}", dr(i - 1))

            Next i



    End Sub

End Class

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now