Home All Groups Group Topic Archive Search About

Help. Determine PrimaryKey Column from sqlDataReader

Author
7 Feb 2006 11:00 AM
SMS_Dev_Dept
Hi,
I have an open and active sqlDataReader object and would like to determine
which of the fields is the PrimaryKey (if present).
I intend to use this to create generic DataTable objects in a library by
passing in a sqlDataReader object, and the library returns a DataTable object
from the reader. This all works fine so far, but I cannot find a way to
determine which field in the sqlDataReader is the PrimaryKey so as to set the
corresponding Column in the DataTable as the PrimaryKey.
Any help appreciated.

Author
7 Feb 2006 3:09 PM
John Kortis
use the dataaapter instead of the reader.  Readers are "forward only
cursors" so to speak and used
to quickly read data.  The DataAdapter will FILL your data set and your
column will already be there!

snipet
System.Data.SqlClient.SqlConnection con;

System.Data.DataSet ds=null;

System.Data.SqlClient.SqlDataAdapter adap=new
System.Data.SqlClient.SqlDataAdapter("select * from table",con);

ds=new DataSet();

adap.Fill(ds);


ds.Tables[0].PrimaryKey[0];



the PrimaryKey is an array of dataColumns!

to cut down on size, use TOP or select less columns in the query or check
the FILL method to limit the records collected.


Show quote
"SMS_Dev_Dept" <SMSDevD***@discussions.microsoft.com> wrote in message
news:AB2316EE-8524-4DAA-B996-6AEF488C5C5E@microsoft.com...
> Hi,
> I have an open and active sqlDataReader object and would like to determine
> which of the fields is the PrimaryKey (if present).
> I intend to use this to create generic DataTable objects in a library by
> passing in a sqlDataReader object, and the library returns a DataTable
object
> from the reader. This all works fine so far, but I cannot find a way to
> determine which field in the sqlDataReader is the PrimaryKey so as to set
the
> corresponding Column in the DataTable as the PrimaryKey.
> Any help appreciated.

AddThis Social Bookmark Button