使用SQLDataReader而不是记录集 [英] Using SQLDataReader instead of recordset

查看:88
本文介绍了使用SQLDataReader而不是记录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此并不陌生,并且有这个问题。我可以使用SQLDataReader代替Recordset吗?我想在SQLDataReader中实现以下结果。

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

我可以用SQLDataReader替换记录集吗,如果可以,请告诉我代码更改?

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

推荐答案

强烈建议您使用 using 模式:

    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

这篇关于使用SQLDataReader而不是记录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆