使用 SQLDataReader 代替记录集

发布于 2024-08-19 16:18:38 字数 515 浏览 3 评论 0原文

我对此很陌生并且有这个问题。我可以使用 SQLDataReader 代替 Recordset 吗?我想在 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 替换记录集吗?如果可以,请告诉我代码中的更改吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫思念不要吵 2024-08-26 16:18:38

强烈建议您使用 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

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
海风掠过北极光 2024-08-26 16:18:38

您将必须更换一些东西,类似于以下内容。

这是一个示例,您需要修改它才能满足您的目标,但这显示了差异。

我还建议使用“Using”语句来管理连接/读取器。另外,还有参数化查询。

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()

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()
明月夜 2024-08-26 16:18:38
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文