How to read from DataReader and Update using DataAdapter

G

Guest

I am looking for example of reading one table using datareader and updating
another with the values using a dataadapter.
 
J

Jeppe Dige Jespersen

I am looking for example of reading one table using datareader and
updating
another with the values using a dataadapter.

'set up the dataset and adapter
dim con as new sqlconnection(<connectionstring and all that....>)
con.open
dim myAdapter as new sqlDataAdapter("Select * from TableA", con)
dim myDataset as new dataset
myAdapter.fill(myDataset, "TableA")

dim con as new sqlconnection(<connectionstring and all that....>)
dim cmd as new sqlCommand("Select * from TableB", con)
dim myReader as sqlDatareader = cmd.executeReader

while myReader.Read
dim newRow as datarow = myDataset.tables("TableA").Newrow
newRow.beginEdit
newRow("firstname") = myReader("firstname")
newRow("lastname") = myReader("lastname")
newRow.endEdit
myDataset.tables("TableA").Rows.add(newRow)
end while

myAdapter.Update(myDataset, "TableA")
con.close

'remember to cleanup after yourself....

.....just off the top of my head... :)
Jeppe Jespersen
 
G

Guest

Thanks, will this allow me to loop through the datarows and only update
values matching those from the datareader? If the data does not match, then
I need to do an insert.

Thanks
 

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