Solved

Adding a Primary Key column after a datatable has been created

Posted on 2013-05-13
3
461 Views
Last Modified: 2013-05-14
I'm importing values from a database into a local DataTable.

myDataTable = MyBLL.GetCustomComponents()

After the data has been imported into the datatable I want to add a primary key column called "ID" and then just have it auto-increment from 1 to the number of rows of data found in the datatable  

How can I do this?
0
Comment
Question by:cdemott33
3 Comments
 
LVL 1

Expert Comment

by:muh_abbas
ID: 39163810
Hello  cdemott33,

This article will help you.

http://www.java2s.com/Code/SQLServer/Constraints/CreatingaPrimaryKeyonanExistingTable.htm



Regards,
Abbas
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39164241
One option is to add column to the existing table but do not make it auto increment. Loop through the rows and populate manually.

Other option is demonstrated here

http://stackoverflow.com/questions/4310791/best-way-add-a-new-column-with-sequential-numbering-in-an-existing-data-table
0
 

Author Closing Comment

by:cdemott33
ID: 39164515
Thanks for you help.  I created a function that added the column than looped through the existing rows to added the primary key.

    Private Shared Function CreatePrimaryKeyColumn(ByVal dt As DataTable) As DataTable

        Dim dc As New DataColumn("PRIMARY_KEY")
        dc.DataType = GetType(Int32)

        dt.Columns.Add(dc)

        For i As Integer = 1 To dt.Rows.Count - 1
            dt.Rows(i)("PRIMARY_KEY") = i
        Next

        Return dt
    End Function
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

13 Experts available now in Live!

Get 1:1 Help Now