ExecuteNonQuery within the loop of a datareader

G

Guest

Hello,

How can I use an ExecuteNonQuery within the loop of a datareader?
My code is as follows:

Dim cmdToProcess As New SqlClient.SqlCommand
Dim rdToProcess As SqlClient.SqlDataReader

With cmdToProcess
.CommandText = " SELECT * FROM tblDetails "
.Connection = cn
End With

rdToProcess = cmdToProcess .ExecuteReader

Select Case rdToProcess
Case True
Do
'here I want to add or update data
Dim cmdToChange As New SqlClient.SqlCommand

With cmdToChange
.CommandText = "INSERT INTO tblNew(AD_Action) VALUES (@Action)"
.Parameters.AddWithValue("@Action", rdToProcess("DE_Number"))
.Connection = cn
End With

cmdToChange.ExecuteNonQuery
Loop until not rdProcess.Read
End Select

At "cmdNieuweCafetariaActiedetail.ExecuteNonQuery", I get the message that I
have to close the first datareader in order to

execute, but then my code cannot proceed because rdToProcess("DE_Number")
can no longer be read.

How can I solve this?

Many thanks and greetings for your help,

Michel
 
G

Guest

Michel,

Try running the ExecuteNonQuery on a connection different than the one being
used by the data reader.

Kerry Moorman
 
W

William Vaughn

Wait a sec. While it's possible to use MARS and such, it's silly to pull the
rows down from the system to build an INSERT statement. Even JET can execute
a INSERT based on the values supplied with a SELECT or an expression or
both. Do all of this work on the server with the database engine. That's
what its for.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant, Dad, Grandpa
Microsoft MVP
INETA Speaker
www.betav.com
www.betav.com/blog/billva
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
B

Brandon Gano

I agree. Michel, google "select into" to learn how to insert data directly
from one table to another.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top