ExecuteNonQuery inside of Reader

旧城冷巷雨未停 提交于 2019-12-13 21:08:24

问题


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

Thank you


回答1:


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. I don't understand the For i = 1 To NumExecutions for loop, but I added it as you wrote it



来源:https://stackoverflow.com/questions/19303970/executenonquery-inside-of-reader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!