How do I read data (or records) from database using data reader?
Thanks.
Printable View
How do I read data (or records) from database using data reader?
Thanks.
- 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()
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.
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.
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
:notworthy
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"))