10

I am new to this and had this question. Can I use SQLDataReader instead of a Recordset. I want to achieve the following result in an SQLDataReader.

Dim dbConn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sqlstr As String = "SELECT Name,Status FROM table1 WHERE id=" + item_id.Value.ToString
rs.Open(SQL, dbConn)
While Not rs.EOF
   txtName.Text = rs.Fields.Item("Name").Value
   ddlstatus.SelectedIndex = 1
   rs.MoveNext()
End While
rs.Close()
rs = Nothing
dbConn.Close()
dbConn = Nothing

Can I replace recordset with SQLDataReader and if I can can you please show me the changes in code?

3 Answers 3

21

Its highly recommend that you use the using pattern:

    Dim sConnection As String = "server=(local);uid=sa;pwd=PassWord;database=DatabaseName"
    Using Con As New SqlConnection(sConnection)
        Con.Open()
        Using Com As New SqlCommand("Select * From tablename", Con)
            Using RDR = Com.ExecuteReader()
                If RDR.HasRows Then
                    Do While RDR.Read
                        txtName.Text = RDR.Item("Name").ToString()
                    Loop
                End If
            End Using
        End Using
        Con.Close()
    End Using
2
  • Is there a C# answer for this ? Commented Feb 28, 2014 at 19:18
  • 2
    @Steam you can use Telerik Converter to convert vb.net to C# or vice versa Commented Oct 2, 2014 at 6:58
2

You will have to swap out a few things, something similar to the following.

Here is an example, you will need to modify this to meet your goal, but this shows the difference.

I also recommend using a "Using" statement to manage the connection/reader. Also, a parameterized query.

Dim sConnection As String = "server=(local);uid=sa;pwd=PassWord;database=DatabaseName"

Dim objCommand As New SqlCommand
objCommand.CommandText = "Select * From tablename"
objCommand.Connection = New SqlConnection(sConnection)
objCommand.Connection.Open()

Dim objDataReader As SqlDataReader = objCommand.ExecuteReader()

If objDataReader.HasRows Then
Do While objDataReader.Read()
Console.WriteLine(" Your name is: " & Convert.ToString(objDataReader(0)))
Loop
Else
Console.WriteLine("No rows returned.")
End If

objDataReader.Close()
objCommand.Dispose()
0
Dim rdrDataReader As SqlClient.SqlDataReader
Dim cmdCommand As SqlClient.SqlCommand
Dim dtsData As New DataSet
Dim dtbTable As New DataTable
Dim i As Integer
Dim SQLStatement as String

msqlConnection.Open()

cmdCommand = New SqlClient.SqlCommand(SQLStatement, msqlConnection)

rdrDataReader = cmdCommand.ExecuteReader()

For i = 0 To (rdrDataReader.FieldCount - 1)
    dtbTable.Columns.Add(rdrDataReader.GetName(i), rdrDataReader.GetFieldType(i))
Next
dtbTable.BeginLoadData()

Dim values(rdrDataReader.FieldCount - 1) As Object

While rdrDataReader.Read
    rdrDataReader.GetValues(values)
    dtbTable.LoadDataRow(values, True)
End While
dtbTable.EndLoadData()

dtsData.Tables.Add(dtbTable)

msqlConnection.Close()

Return dtsData
1
  • 1
    There is no need to create/load a dataset for this. Commented Jan 28, 2010 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.