views:

585

answers:

4

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.

A: 
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
smoore
There is no need to create/load a dataset for this.
Mitchel Sellers
A: 

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()
Mitchel Sellers
A: 

I would recommend against this method unless you have some very specific need to do this in a code behind of a page. I suggest looking into the SqlDataSource object for this kind of thing as it can be auto-bound to your fields without the need to maintain spaghetti on the back end.

Joel Etherton
+1  A: 

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
Chris Haas