Solved

Copy datatable into new datatable and rename columns

Posted on 2011-09-21
3
439 Views
Last Modified: 2012-05-12
I have a datatable with the following columns

[start Date]
[Pmt Amount]
[Compounding]

How do I copy 2 of those columns into a new datatable to pass to SQL 2008 as a structured data table

the new datatable would be
startDate
PaymentAmt
0
Comment
Question by:lrbrister
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
x77 earned 500 total points
ID: 36575647
I think you don' t need create other table.
You can Map columns from your table to Parameters on Command that update Sql 2008.

Note that it is very easy copy to other table, but I need know:

   Copy only current values ?
   Copy Deleted Rows ?

Note that if you use DataAdapter Update you need copy rowstate and also original an current values.

To do this

   dim ccOr = new dataColumn(){ OrignalTabl.Column("start Date"), OrignalTabl.Column("Pmt Amount"),
   Dim ccDes = new dataColumn(){ OrignalTabl.Column("startDate"), OrignalTabl.Column("PaymentAmt "),

   For each Row as Datarow in OriginaTable
      dirm rDes = DestTable.newRow
      for n=0 to ccor.length -1
         rdest(ccdes(n)) = row(ccor, DataRowVersion.Original)
      next
      rdes.rows.add(rdest)
   next

   Tdest.acceptchanges

Now, write code to delete deleted rows and Update modified rows

   for i as integer = 0 to OriginaTable.rows.count -1
       select case OriginaTable.rows(i).rowstate
            Case DataRowState.Modified
                   dim row=  OriginaTable.rows(i), rdest=DestTable.rows(i)
                   for n=0 to ccor.length -1
                        rdest(ccdes(n)) = row(ccor)
                   next
            Case DataRowState.Deleted
                destTable.rows(N).delete
       end select
   next
0
 
LVL 15

Expert Comment

by:x77
ID: 36575663
Note. I write code directly, I don´t verify.

by sample :  where says         row(ccor)    ->    row(ccor(n))

0
 

Author Closing Comment

by:lrbrister
ID: 36576045
You put me on the right track...thanks

Dim dt As New DataTable
dt = ConvertTo(l_lead.Payments)
dt.Columns(0).ColumnName = "LeadType"
blah...blah...blah..

dt.Columns.Remove("LeadType")
dt.Columns.Remove("PaymentStreamID")
blah...blah...blah..

dt.Columns("EventType").SetOrdinal(0)
dt.Columns("StartDate").SetOrdinal(1)
dt.Columns("PmtAmount").SetOrdinal(2)
blah...blah...blah.."
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question