Solved

DataSet using Code

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

Expert Comment

by:Dhaest
Comment Utility
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
Comment Utility
A complete example to connect to the database, fill a dataset and loop through that dataset
(console-applicatoin)
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)

        Next

 

        Console.Write(vbCrLf)

 

        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

            Console.Write(vbCrLf)

        Next

    End Sub

End Class

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

9 Experts available now in Live!

Get 1:1 Help Now