Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Determining a datatable's primary key from C#
Message
De
28/03/2006 13:37:05
 
 
À
27/03/2006 14:09:49
Keith Payne
Technical Marketing Solutions
Floride, États-Unis
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01108081
Message ID:
01108473
Vues:
10
>>I know how to set a primary key when creating a table in SQL or by using the graphic interface of Enterprise Manager, but once a table has been created and I know its name, is there a way to determine if any of its columns are primary keys? I don't understand why ADO.NET, when you do some kind of SQL pass-through along the lines of
>>
>>SELECT * FROM TableName
>>
>>never captures the primary key information. Selecting all columns obviously includes any primary keys that are set.
>>
>>Is it just that it is possible to do, but Microsoft is leaving it up to developers because some may not need primary keys in their result sets and not want to incur the additional overhead of indexing the tables in their result sets?
>>
>>Even the results of a query that joins multiple tables could set primary keys, when applicable, if my sense of set theory is correct. If table1 has pk1 and table2 has pk2, a join of the two tables should have a pk3 that is defined by a unique pairing of pk1 + pk2. I am assuming that the uniqueness of primary keys is transitive, to use a mathematical term.
>>
>>I suppose that it is possible to write stored procedures that break primary key uniqueness and it would be a lot of overhead, if possible at all, for ADO.NET to evaluate a stored procedure or SQL query and decide if someone had broken PK uniqueness.
>>
>>For the database design that I am working with, we always have single column primary keys on every table, don't rename columns with AS, and all tables in my result sets that I get have primary keys that I can determine.
>>
>>Can someone tell me the SQL code to ask if a given table has primary key or identifier columns set?
>
>David,
>
>If you need the key and relationship data to populate automatically, you will have to use a DataSet and DataAdapter instead of ExecuteReader or ExecuteNonQuery.
>
>If you want to retrieve the information manually, you can get it by using SqlDataReader.GetSchemaTable in .NET or directly from SQL Server with SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS.

Thanks, Keith. I knew that it should be accessible somehow. I only know enough about SQL to query, filter, and write the occasional stored procedure, but I don't know much about the metastructures and admin stuff.
David S. Alexander
Kettley Publishing
20271 SW Birch Street, 2nd Floor
Newport Beach, CA 92660-1752
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform