c# - How to get columns Primary key constraints using SqlConnection.GetSchema() -


i have code of ado.net dynamically detect database schema, need how unique columns constraints , primary key constraints using getschema method on sqlconnection. code have:

conn.open();      sqlcommand msqlcommand = new sqlcommand("sp_pkeys", conn); msqlcommand.commandtype = commandtype.storedprocedure; msqlcommand.parameters.add(     "@table_name", sqldbtype.nvarchar).value = tablename; sqldatareader mreader = msqlcommand.executereader(     (commandbehavior.keyinfo | commandbehavior.schemaonly)); //executereader(); datatable schema = mreader.getschematable(); mreader.close(); conn.close(); 

there nothing in call getschematable on sqlconnection allow figure out.

it might seem can, using iskey column value, should return true contributes uniquely identifying record in table. however, documentation iskey column (emphasis mine):

true : column 1 of set of columns in rowset that, taken together, uniquely identify row. set of columns iskey set true must uniquely identify row in rowset. there no requirement set of columns minimal set of columns. set of columns may be generated base table primary key, unique constraint or unique index.

because of this, can't guarantee contributes primary key per-se.

now, if need uniquely identify row, iskey fine, primary key not way uniquely identify row (e.g. can have natural identifiers unique index). if have primary key , unique index other columns, values across columns in combination unique.

however, if need @ columns make primary key, getschematable not give information need. rather, can make call sp_pkeys system stored procedure find names of columns contribute making primary key.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -