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 12:28:57
 
 
À
27/03/2006 13:55:12
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01108081
Message ID:
01108423
Vues:
12
Expanding on Keith's answer, if you set the MissingSchemaAction property of the DataAdapter, it will set the PrimaryKey property of your table corresponding to the PrimaryKey in SQL Server. Something like this:
SqlCommand sc = new SqlCommand("select * from foo", new SqlConnection(this.TestConnection));

SqlDataAdapter da = new SqlDataAdapter(sc);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataSet ds = new DataSet();
da.Fill(ds, "MyTable");
~~Bonnie




>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?
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform