Solved

Adding a Primary Key column after a datatable has been created

Posted on 2013-05-13
3
464 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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