2

I searched the web for multiple OleDbDataReader on one OleDbConnection, but all the answers were negative, however, this code works without error. Why is that?

Dim DATA_BASE_Name As String = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\TABLES.XLS"
Dim ConnectionStringBuilder As New System.Data.OleDb.OleDbConnectionStringBuilder
ConnectionStringBuilder("Provider") = "Microsoft.Jet.OLEDB.4.0"
ConnectionStringBuilder("Data Source") = DATA_BASE_Name
ConnectionStringBuilder("Mode") = "ReadWrite"
ConnectionStringBuilder("Extended Properties") = "Excel 8.0;HDR=No"
Dim XLS_DATA_BASE As OleDbConnection = New OleDbConnection(ConnectionStringBuilder.ConnectionString)
XLS_DATA_BASE.Open()
Dim Command As OleDbCommand = New OleDbCommand
Dim Command2 As OleDbCommand = New OleDbCommand
Command.Connection = XLS_DATA_BASE
Command2.Connection = XLS_DATA_BASE
Command.CommandText = "SELECT * FROM [Sheet1$A1:A100]"
Command2.CommandText = "SELECT * FROM [Sheet1$B2:E10]"
Dim dbReader1 As OleDbDataReader = Command.ExecuteReader()
Dim dbReader2 As OleDbDataReader = Command2.ExecuteReader()
For row As Short = 1 To 2
   dbReader1.Read()
   Dim a1 As String = dbReader1.GetValue(0).ToString
   dbReader2.Read()
   Dim a2 As String = dbReader2.GetValue(0).ToString
Next
dbReader1.Close()
dbReader2.Close()
vitcaro
  • 21
  • 2
  • *Don't wake up a sleeping dog* @vitcaro. :D You got two separate `command`s and two separate `OldeDbDataReader`s. I don't know what's the issue here. You can `open` the connection, execute as many commands as you wish and then `close` the connection. – Mehrad Dec 16 '14 at 06:57

0 Answers0