C
Cip
Hi,
I have created a small app used to transfer data from MSDE to my own
DB.
I am having some serious problems with SQLDataReader. I use it to
loop through records in order to build my own "INSERT INTO"
statements.
After a random number of records, I get the exception: "Invalid
attempt to read data when reader is closed."
I dont close the reader anywhere!!! It seems like it is closing
itself whenever it doesnt feel like doing anymore work. sometimes
after 40,000 rows, sometimes after 140,000 rows, sometimes even after
10,000 rows.
I wish *I* could close down whenever i didnt feel like working...
any ideas? if the code were to run properly, around 175,000 rows would
be inserted in my destination DB.
Is this a bug?
Here is the code:
Dim srcConn As New SqlClient.SqlConnection
Dim srcCommand As New SqlClient.SqlCommand
Dim srcAdapter As New SqlClient.SqlDataAdapter
Dim srcReader As SqlClient.SqlDataReader
Dim srcStrSql As String
srcConn.ConnectionString = LOCALDB_CONNECTIONSTRING
srcCommand.Connection = srcConn
srcStrSql = "Select ID, col1, col2, col3 from Table where source=0"
srcCommand.CommandText = srcStrSql
srcConn.Open()
srcReader = srcCommand.ExecuteReader
Dim rowCount As Integer = 0
While srcReader.Read()
rowCount = rowCount + 1
strSQL = "INSERT INTO NewTable values(" +
srcReader.getValue(0).ToString 'This is the line that always throws
the exception!! when the exception is thrown, rowCount is always a
different number, anywhere from 10,000 to 140,000
For i As Integer = 1 To srcReader.FieldCount - 1
strSQL += "," + srcReader.GetValue(i).ToString
Next i
strSQL += strSQL + ")"
command.CommandText = strSQL
command.ExecuteNonQuery()
End While
I have created a small app used to transfer data from MSDE to my own
DB.
I am having some serious problems with SQLDataReader. I use it to
loop through records in order to build my own "INSERT INTO"
statements.
After a random number of records, I get the exception: "Invalid
attempt to read data when reader is closed."
I dont close the reader anywhere!!! It seems like it is closing
itself whenever it doesnt feel like doing anymore work. sometimes
after 40,000 rows, sometimes after 140,000 rows, sometimes even after
10,000 rows.
I wish *I* could close down whenever i didnt feel like working...
any ideas? if the code were to run properly, around 175,000 rows would
be inserted in my destination DB.
Is this a bug?
Here is the code:
Dim srcConn As New SqlClient.SqlConnection
Dim srcCommand As New SqlClient.SqlCommand
Dim srcAdapter As New SqlClient.SqlDataAdapter
Dim srcReader As SqlClient.SqlDataReader
Dim srcStrSql As String
srcConn.ConnectionString = LOCALDB_CONNECTIONSTRING
srcCommand.Connection = srcConn
srcStrSql = "Select ID, col1, col2, col3 from Table where source=0"
srcCommand.CommandText = srcStrSql
srcConn.Open()
srcReader = srcCommand.ExecuteReader
Dim rowCount As Integer = 0
While srcReader.Read()
rowCount = rowCount + 1
strSQL = "INSERT INTO NewTable values(" +
srcReader.getValue(0).ToString 'This is the line that always throws
the exception!! when the exception is thrown, rowCount is always a
different number, anywhere from 10,000 to 140,000
For i As Integer = 1 To srcReader.FieldCount - 1
strSQL += "," + srcReader.GetValue(i).ToString
Next i
strSQL += strSQL + ")"
command.CommandText = strSQL
command.ExecuteNonQuery()
End While