Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling DataTable from DataAdapter - the wrong column is selected as primary key

I am filling a datatable object through a dataadapter pulling data from a MS-SQL database. For some reason the wrong column is set as the primary key on the datatable. I have tried FillSchema and MissingSchemaAction.AddWithKey to no avail.

The correct primary key is defined properly in the SQL server DB schema & is backed by a non clustered unique index PK_x.

The column selected as the primary key by the dataadapter is a unique column backed by a clustered unique index.

These are the only two columns in the table.

How does DataAdapter retrieve the table schema from SQL? I have interrogated the sys tables (sys.tables, sys.index_columns, sys.indexes) and they indicate the proper primary key schema. What gives?

like image 927
Drew R Avatar asked Nov 12 '22 22:11

Drew R


1 Answers

"If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. For more information about specifying query hints, see Query Hint (Transact-SQL)."

http://msdn.microsoft.com/EN-US/library/152bda9x(v=VS.110,d=hv.2).aspx

like image 166
IT63 Avatar answered Nov 15 '22 12:11

IT63