C# & SQL Server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sumit

    #1

    C# & SQL Server

    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...unfortun ately i have one
    :-(

    Kindly Help
    Warm Regards
    Sumit

  • Nick Malik [Microsoft]

    #2
    Re: C# & SQL Server

    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.
    --
    "Sumit" <vohrasumit@gma il.com> wrote in message
    news:1117460906 .432564.320970@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > 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...unfortun ately i have one
    > :-(
    >
    > Kindly Help
    > Warm Regards
    > Sumit
    >[/color]


    Comment

    Working...