[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

Copy datatable into new datatable and rename columns

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
lrbrister
Asked:
lrbrister
  • 2
1 Solution
 
x77Commented:
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
 
x77Commented:
Note. I write code directly, I donĀ“t verify.

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

0
 
lrbristerAuthor Commented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now