Solved

Copy datatable into new datatable and rename columns

Posted on 2011-09-21
3
428 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
Comment Utility
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
Comment Utility
Note. I write code directly, I don´t verify.

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

0
 

Author Closing Comment

by:lrbrister
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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 discusses moving either the default database or any database to a new volume.

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now