DataTable.PrimaryKey columns in different order from Database's primary key

327 views Asked by At

I'm using SQL 2014 database. (Also tested in SQL 2012 with same behavior.) I have a table with a primary key with columns 'A,B'. The columns in the create table statement are in a different order 'B,A'.

When I'm running DataAdapter.FillSchema(), DataTable.PrimaryKey gets set to columns in order 'B,A'. My expectation would be to get the columns in order 'A,B' - the way the primary key is ordered in the database.

When the table is created as follows, DataTable.PrimaryKey is in order 'A,B':

drop table alex_test
CREATE TABLE alex_test( 
    [a] [varchar](128) NOT NULL,
    [b] [varchar](128) NOT NULL,
    [c] [varchar](128) NOT NULL
 CONSTRAINT [pk_alex_test] PRIMARY KEY CLUSTERED 
(
    a ASC,
    b ASC
)
)
insert into alex_test (a, b, c) Values('a', 'b', 'c')

However when the table is created as follows, the order of DataTable.PrimaryKey is 'B,A':

drop table alex_test
CREATE TABLE alex_test( 
    [b] [varchar](128) NOT NULL,
    [a] [varchar](128) NOT NULL,
    [c] [varchar](128) NOT NULL
 CONSTRAINT [pk_alex_test] PRIMARY KEY CLUSTERED 
(
    a ASC,
    b ASC
)
)
insert into alex_test (a, b, c) Values('a', 'b', 'c')
0

There are 0 answers