Help. Determine PrimaryKey Column from sqlDataReader

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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.
 
Back
Top