2013-10-10 67 views
0

我該如何解決這個問題?我不想使用兩個連接。 ExecuteNonQuery僅適用於閱讀器關閉的情況。ExecuteNonQuery裏面的閱讀器

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

謝謝

回答

2

使用列表來存儲您的ID在閱讀DataReader,則對於列表中的每個元素,你應該能夠與當時相同的連接執行查詢:

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

PS我不明白For i = 1 To NumExecutions for循環,但我添加它,因爲你寫它