Results 1 to 6 of 6

Thread: How to use datareader in VB.Net

  1. #1
    Join Date
    Dec 2008
    Posts
    13

    How to use datareader in VB.Net

    How do I read data (or records) from database using data reader?

    Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to use datareader in VB.Net

    • To read data from the database, you first make a connection object (SqlConnection, etc) and open it.

    • Then you create a command using this connection and the command text. and execute the command with the command object’s ExecuteReader() method.

    • Now you read the individual records using this data reader. If the DataReader’s Read() method returns true then the DataReader acts as a database row (record).


    Code:
    Dim connString As String = "server=s; database=techarena;" + _
                                       "uid=sa; pwd="
            Dim conn As New SqlConnection(connString)
            Dim cmdString As String = "select * from author"
            Dim cmd As New SqlCommand(cmdString, conn)
            conn.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            While reader.Read()
                txtData.Text += reader("authorId").ToString()
                txtData.Text += ", "
                txtData.Text += reader("name").ToString()
                txtData.Text += vbCrLf
            End While
            conn.Close()

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use datareader in VB.Net

    DataReaders

    A DataReader is a lightweight object that provides read-only, forward-only data in a very fast and efficient way. Using a DataReader is efficient than using a DataAdapter but it is limited. Data access with DataReader is
    read-only, meaning, we cannot make any changes (update) to data and forward-only, which means we cannot go back to the previous record which was accessed. A DataReader requires the exclusive use of an active connection for the entire time it is in existence. We instantiate a DataReader by making a call to a Command object's ExecuteReader command. When the DataReader is first returned it is positioned before the first record of the result set. To make the first record available we need to call the Read method. If a record is available, the Read method moves the DataReader to next record and returns True. If a record is not available the Read method returns False. We use a While Loop to iterate through the records with the Read method.

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Whether to use DataReader or DataSet

    If we want high performance and going to access the data once, then the DataReader is suitable. If we need access to the same data multiple times, or if you need to create a complex relationship in memory, then DataSet is appropriate.

    Using DataReader can increase application performance and reduce system overheads because only one buffered row at a time is ever in memory. With DataReader we get as close to the raw data as possible and don’t have to go through the overheads of populating a DataSet object, which sometimes may be expensive if the DataSet contains a lot of data.

  5. #5

    Re: How to use datareader in VB.Net

    HEY I WANNA GET A DATA DROM A TABLE COLUMN IN TO A TEXT BOX OF VB.NET
    FROM SO CAN YOU HELP ME OUT IF ONCE U SAY I WILL UNDERSTAND
    SEE MY TEXTBOX NAME IS TESTBOX 1 AND I HAVE TO TAKE THE DATA FROM A TABLE NAME BALANCE AND MY COLUMN NAME IS OLDBALANCE

  6. #6
    Join Date
    Dec 2007
    Posts
    2,291

    Re: How to use datareader in VB.Net

    Quote Originally Posted by MUKESHSINGHRAJPUROHI View Post
    HEY I WANNA GET A DATA DROM A TABLE COLUMN IN TO A TEXT BOX OF VB.NET
    FROM SO CAN YOU HELP ME OUT IF ONCE U SAY I WILL UNDERSTAND
    SEE MY TEXTBOX NAME IS TESTBOX 1 AND I HAVE TO TAKE THE DATA FROM A TABLE NAME BALANCE AND MY COLUMN NAME IS OLDBALANCE
    I was able to get data from SQL into Text Box by using the below code, and hope that by using the same method you could also try to achieve the same results for your issue:

    Code:
        Me.TextBox1.Text = Convert.ToString(lrd("customer"))
                    Me.TextBox2.Text = Convert.ToString(lrd("CustName"))
                    Me.Label1.Text = Convert.ToString(lrd("Custid"))

Similar Threads

  1. Dataset vs DataReader in .Net
    By Vireshh in forum Software Development
    Replies: 3
    Last Post: 07-05-2011, 11:32 AM
  2. VB.NET Datareader to display number of rows?
    By Pooja in forum Software Development
    Replies: 3
    Last Post: 14-02-2009, 06:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,717,385,972.23192 seconds with 16 queries