Given a database, how can I retrieve a list of Tables along with the primary key of each table?
Thanks.
Edit: This is for a code generation tool I’m making, I need to somehow run the SQL script using C# only.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This uses no T-SQL by itself. It generates it on its own but will not be as efficient as just using the exact, short T-SQL that will get the same information.
Each of the Objects in the collection “vTables” will be a definition for a Table in that database called vDatabaseName.
That table will have a collection of columns, and the primary keys can be found be looping through the “Columns” property of each table. Any columns in the primary key will have its property “InPrimaryKey” marked true.
Also you must get all information out of the various objects BEFORE the end of the using block. I went about making mini classes that had just the information I needed once the whole process was done, but you can probably just spit out the code from the code generation instead.