PC Review Forums Newsgroups Microsoft DotNet Microsoft ADO .NET Help. Determine PrimaryKey Column from sqlDataReader

Reply

Help. Determine PrimaryKey Column from sqlDataReader

 
Thread Tools Rate Thread
Old 07-02-2006, 11:00 AM   #1
=?Utf-8?B?U01TX0Rldl9EZXB0?=
Guest
 
Posts: n/a
Default Help. Determine PrimaryKey Column from sqlDataReader


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.
  Reply With Quote
Old 07-02-2006, 03:09 PM   #2
John Kortis
Guest
 
Posts: n/a
Default Re: Help. Determine PrimaryKey Column from sqlDataReader

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.


"SMS_Dev_Dept" <SMSDevDept@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.



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off