读取器内部的ExecuteNonQuery [英] ExecuteNonQuery inside of Reader

查看:118
本文介绍了读取器内部的ExecuteNonQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何解决这个问题?我不想使用两个连接. ExecuteNonQuery仅在关闭阅读器后才有效.

How can I solve this problem? I do not want to use two connections. ExecuteNonQuery only works if reader is closed.

oleCnn = New System.Data.OleDb.OleDbConnection
oleCnn.ConnectionString = ConnectionString
oleCmm = New OleDb.OleDbCommand()
oleCnn.Open()
oleStr = "SELECT ID_Process FROM MyProcessTable"
Dim reader As System.Data.OleDb.OleDbDataReader = oleCmm.ExecuteReader()
        While reader.Read()
            For i = 1 To NumExecutions
                    oleStr = "INSERT INTO MyOtherProcessTable(reader.getInt32(0))"           
                    oleCmm.ExecuteNonQuery()
            Next
        End While
reader.Close()
oleCnn.Close()

谢谢

推荐答案

使用一个列表来存储ID,同时读取DataReader,那么对于列表中的每个元素,您都应该能够使用相同的连接来执行查询:

Use a list to store your IDs while reading the DataReader, then for each element of the list you should be able to execute your query with then same connection:

oleCnn = New System.Data.OleDb.OleDbConnection
oleCnn.ConnectionString = ConnectionString
oleCmm = New OleDb.OleDbCommand()
oleCnn.Open()
oleStr = "SELECT ID_Process FROM MyProcessTable"

Dim ids As New List(Of Integer)

Dim reader As System.Data.OleDb.OleDbDataReader = oleCmm.ExecuteReader()
    While reader.Read()
    ids.Add(reader.GetInt32(0))
    End While
reader.Close()

For Each curr_id As Integer In ids
    For i = 1 To NumExecutions
        oleStr = "INSERT INTO MyOtherProcessTable(" & curr_id.ToString & ")"
        oleCmm.ExecuteNonQuery()
    Next
Next
oleCnn.Close()

P.S.我不了解For i = 1 To NumExecutions for循环,但我在您编写时将其添加了

P.S. I don't understand the For i = 1 To NumExecutions for loop, but I added it as you wrote it

这篇关于读取器内部的ExecuteNonQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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