Home All Groups Group Topic Archive Search About

Get Primary Key from Table.

Author
14 Feb 2007 8:05 AM
_MC_
Hi,

I want to request if an Colum is Primary Key or Not via Ole (C# Code).
Following Part of Code descripes my ideas. However the function
returns always false, no matter what Database System (e. g. MS SQL,
Paradox, ...). This also appears if i request the Attribute
'AllowDBNull'.  Now - how can I fix this?

Thanks for your help.

       Stefan

Code:

public Boolean isPrimary (int checkPos)
{
            OleDbDataAdapter myAdapter = new
OleDbDataAdapter(commandText, conn);
            DataSet mySet = new DataSet();

            myAdapter.Fill(mySet);

            return mySet.Tables[0].Columns[checkPos].Unique;
}

Author
14 Feb 2007 10:42 AM
Miha Markic [MVP C#]
Hi,

I would rather use GetOleDbSchemaTable method.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Show quote
"_MC_" <m*@andariel.informatik.uni-erlangen.de> wrote in message
news:1171440313.891401.166990@m58g2000cwm.googlegroups.com...
> Hi,
>
> I want to request if an Colum is Primary Key or Not via Ole (C# Code).
> Following Part of Code descripes my ideas. However the function
> returns always false, no matter what Database System (e. g. MS SQL,
> Paradox, ...). This also appears if i request the Attribute
> 'AllowDBNull'.  Now - how can I fix this?
>
> Thanks for your help.
>
>       Stefan
>
> Code:
>
> public Boolean isPrimary (int checkPos)
> {
>            OleDbDataAdapter myAdapter = new
> OleDbDataAdapter(commandText, conn);
>            DataSet mySet = new DataSet();
>
>            myAdapter.Fill(mySet);
>
>            return mySet.Tables[0].Columns[checkPos].Unique;
> }
>
Author
14 Feb 2007 12:29 PM
_MC_
Hi,

tried it now. The same Result. As I see it Unique != Primary Key.
Unique allows Null Values, Primary Not. Nevertheless I found no
function to extract the neccessary information.

Thanks,

      Stefan

     public Boolean isPrimary(int checkPos)
      {

          DataTable mySchema =
getConnectionPtr().GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new
Object[] { null, null, null, "Table" });
            return mySchema.Columns[checkPos].Unique;
      }

Show quote
On 14 Feb., 11:42, "Miha Markic [MVP C#]" <miha at rthand com> wrote:
> Hi,
>
> I would rather use GetOleDbSchemaTable method.
>
> --
> Miha Markic [MVP C#, INETA Country Leader for Slovenia]
> RightHand .NET consulting & developmentwww.rthand.com
> Blog:http://cs.rthand.com/blogs/blog_with_righthand/
>
> "_MC_" <m***@andariel.informatik.uni-erlangen.de> wrote in message
>
> news:1171440313.891401.166990@m58g2000cwm.googlegroups.com...
>
> > Hi,
>
> > I want to request if an Colum is Primary Key or Not via Ole (C# Code).
> > Following Part of Code descripes my ideas. However the function
> > returns always false, no matter what Database System (e. g. MS SQL,
> > Paradox, ...). This also appears if i request the Attribute
> > 'AllowDBNull'.  Now - how can I fix this?
>
> > Thanks for your help.
>
> >       Stefan
>
> > Code:
>
> > public Boolean isPrimary (int checkPos)
> > {
> >            OleDbDataAdapter myAdapter = new
> > OleDbDataAdapter(commandText, conn);
> >            DataSet mySet = new DataSet();
>
> >            myAdapter.Fill(mySet);
>
> >            return mySet.Tables[0].Columns[checkPos].Unique;
> > }
Author
14 Feb 2007 1:00 PM
_MC_
Hi, finally it works (not nice, but it works). Thanks a lot. (Tested
with MS SQl and Paradox Tables)

   Stefan

public Boolean isPrimary(int checkPos, String tableName)
{
   DataTable mySchema =
getConnectionPtr().GetOleDbSchemaTable(OleDbSchemaGuid.Primary_Keys,
new    Object[] {null, null, tableName});

                  return (mySchema.Rows[0].ItemArray[3].ToString() ==
(String) getColumNames()[checkPos]);

}

AddThis Social Bookmark Button