C# & SQL Server

  • Thread starter Thread starter Sumit
  • Start date Start date
S

Sumit

Hi all,

I have a table having 20 fields & 10 rows with one field as primary key
say rollNo.
I want to copy a row of the table to the table itself with a new
rollNo.

Is there a simple way out or i have to take the row in a dataset & then
modify the rollno column of the dataset with the new value & after that
write a insert query like:
insert into mytable values(dataset.Tables[0].Rows[0]["RollNo"],
dataset.Tables[0].Rows[0]["RollNo"],.....& so on).
The approach that i have written is a very cumbersome one..
also think of a table having 80 - 100 rows...unfortunately i have one
:-(

Kindly Help
Warm Regards
Sumit
 
easiest way is to create a data adapter pointed at your table. Fill a
dataset using the data adapter, filtered as best you can to get as few rows
as possible.

Find the row you want to duplicate.

Then, using the rows and columns collections of the dataset, copy the value
of the old row to the new one. Catch and change the primary key column.

Then use the data adapter to write the updated dataset back, which will do
the complex insert statement for you.

The amount of code should be less than about 50 lines for a generic solution
you can reuse.


--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

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

Back
Top