JaedenRuiner
Well-known member
- Joined
- Aug 13, 2007
- Messages
- 340
- Programming Experience
- 10+
I'm working on shifting some of my SQL command operations into the SqlBulkCopy class, as I can gain real time processing updates along the way, and yet i'm discovering a minor annoyance with the Exception not being all too clear about what is going wrong.
The below code is part of a SqlWrapper class of mine, that operates as a glorified parsing and execution engine, allowing me to format and execute specific sql statements via the SqlCommand() and other classes.
the _bulk member is simply a reference to a setting that tells the engine to either SqlCommand.ExecuteNonQuery() on the whole thing, or to attempt using the SqlBulkCopy()
Note:
Basically, I grab my source and format it as a SELECT STATEMENT (even though this object could have initially been parsed from an INSERT INTO or a SELECT INTO statement) and then set my SqlBulkCopy Destination table and try to execute the WriteToServer()
(Note: I am listening to the SqlRowsCopied() event)
However, in all of this I am getting an Exception the moment I try to run this command, and the goofy thing is the Exception is:
The odd parts, as you can probably guess, is that I haven't opened another DataReader for this Command. Count() property referenced at the beginning is a SqlCommand.ExecuteScalar() on the source table, but then I close out that command (=Nothing). There shouldn't be any "Reader" associated with anything at this juncture of my application.
There is a point to note, however:
The Command I'm parsing in this situation is a "SELECT INTO" which means the DB destination table has not yet been created. That is the only thing i could think of that might cause a problem and I am wondering if this Exception from the SqlBulkCopy() is simply a confused reaction to the missing Destination Table in the database.
Thanks
The below code is part of a SqlWrapper class of mine, that operates as a glorified parsing and execution engine, allowing me to format and execute specific sql statements via the SqlCommand() and other classes.
the _bulk member is simply a reference to a setting that tells the engine to either SqlCommand.ExecuteNonQuery() on the whole thing, or to attempt using the SqlBulkCopy()
Note:
- the _ is a prefix for all class member fields
- MakeSelect() is a Shared function that interprets the information and generates a formatted SQL SELECT statement.
VB.NET:
If _bulk AndAlso (Count > 0) Then
Dim sql As String = ""
If MakeSelect(sql, _source, _columns, _condition) = SqlParseResult.NoError Then
Dim rdr As SqlClient.SqlDataReader
_cmd = New SqlCommand(sql, _db.Connection)
_bcopy = New SqlBulkCopy(_db.Connection)
Maxitems = _count
rdr = _cmd.ExecuteReader()
_bcopy.NotifyAfter = ((_count \ 1000) + 1) * 50
_bcopy.DestinationTableName = _dest
Try
_bcopy.WriteToServer(rdr)
Catch ex As Exception
_error = SqlParseResult.SQLExecException
_exmsg = ex.Message
Me.Exception = ex
End Try
End If
Else
Return (ExecuteSQL() = SqlParseResult.NoError)
End If
Basically, I grab my source and format it as a SELECT STATEMENT (even though this object could have initially been parsed from an INSERT INTO or a SELECT INTO statement) and then set my SqlBulkCopy Destination table and try to execute the WriteToServer()
(Note: I am listening to the SqlRowsCopied() event)
However, in all of this I am getting an Exception the moment I try to run this command, and the goofy thing is the Exception is:
Ex Message said:There is already an open DataReader associated with this Command which must be closed first.
The odd parts, as you can probably guess, is that I haven't opened another DataReader for this Command. Count() property referenced at the beginning is a SqlCommand.ExecuteScalar() on the source table, but then I close out that command (=Nothing). There shouldn't be any "Reader" associated with anything at this juncture of my application.
There is a point to note, however:
The Command I'm parsing in this situation is a "SELECT INTO" which means the DB destination table has not yet been created. That is the only thing i could think of that might cause a problem and I am wondering if this Exception from the SqlBulkCopy() is simply a confused reaction to the missing Destination Table in the database.
Thanks