ARTICLE

How to retrieve rows from a database table in VB.NET

Posted by Rohatash Kumar Articles | ADO.NET in VB.NET February 17, 2011
This article describes how to use ado.net to connect to a database and retrieve the rows from the database table.
Download Files:
 
Reader Level:

This article describes how to use ado.net to connect to a database and retrieve the row from the database table.To do that we use DataAdapter to retrieve the data from the database and place the data into DataSet. To fill the data into the DataSet use Fill method.

Creating connection object

To create a connection we pass the connection string as a parameter in connection object.

Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

Dim con As New SqlConnection(str)

 

The above string defines the connection string which is used to connect the database with the application.

 

Now we create a database table and insert some values in this table. Table looks like this.

 

create table logn

(

username varchar(50),

password varchar(40)

)

go

insert into logn values('monu','mohan')

go

insert into logn values('Rohatash','rohit')

go

insert into logn values('Manoj','singh')

go

select * from logn;

The table looks like this.


d2.gif
 

Table1.gif

 

DataRow

DataRow represents a row of data in a data table.

 

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,12}", dr(i - 1))

Next i

Console.WriteLine("")

 

For example

 

Imports System.Data.SqlClient

Module Module1

    Sub Main()

        Dim str As String = "Data Source=.;uid=sa;pwd=123;database=master"

        Dim con As New SqlConnection(str)

        Dim str1 As String = "SELECT * FROM logn"

        Dim cmd As New SqlCommand(str1, con)

        Dim da As New SqlDataAdapter(cmd)

        Dim ds As New DataSet()

        da.Fill(ds, "logn")

        Console.WriteLine("")

        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,12}", dr(i - 1))

            Next i

            Console.WriteLine("")

        Next

    End Sub

End Module

 

OUTPUT


d1.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Hello Rohatash, I have been going through all your articles of VB.NET, and it is really helpfull, however, I need something specific, and these articles / tutorials are not able to get me the logic clear. I need to use the OpenFileDialog to browse a file, and open it in Binary mode to extract the Header or magic number of any file, that header, should be compared to the one in my existing db table ( fileextension | fileHeader ) i want to retreive from SQL db, the same header with it's corresponding file extension value. it would be really great if you can help me for this concept. Awaiting your reply. Thanks in advance.

Posted by Nitin Kushwaha Mar 31, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor