Help. Determine PrimaryKey Column from sqlDataReader

G

Guest

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.
 
J

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top