Get Primary Key from Table.

M

_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;
}
 
M

_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;
}

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/


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.

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;
}
 
M

_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]);

}
 

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