J
jlapenta
I am trying to get the Primary Keys for a table using GetSchemaTable as
follows:
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =
d:\\access\\northwind.mdb";
string strSQL = "select * from PRODUCTS";
string strMessage;
OleDbConnection cn = new OleDbConnection(strConnection);
cn.Open();
OleDbCommand cmd = new OleDbCommand(strSQL , cn);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly |
CommandBehavior.KeyInfo);
DataTable tbl = rdr.GetSchemaTable();
if (tbl.PrimaryKey.Length > 0)
{
strMessage = "";
foreach (DataColumn column in tbl.PrimaryKey)
{
strMessage += "Column :" + column.ColumnName + " ";
}
MessageBox.Show("Primary KEY fields: " + strMessage);
}
else
{
MessageBox.Show("NO PRIMARY KEY");
}
I have tried using CommandBehavior.KeyInfo and
CommandBehvavior.SchemaOnly as the arguments as well, with the same
results.
Anyone know what i am doing wrong?
follows:
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =
d:\\access\\northwind.mdb";
string strSQL = "select * from PRODUCTS";
string strMessage;
OleDbConnection cn = new OleDbConnection(strConnection);
cn.Open();
OleDbCommand cmd = new OleDbCommand(strSQL , cn);
OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.SchemaOnly |
CommandBehavior.KeyInfo);
DataTable tbl = rdr.GetSchemaTable();
if (tbl.PrimaryKey.Length > 0)
{
strMessage = "";
foreach (DataColumn column in tbl.PrimaryKey)
{
strMessage += "Column :" + column.ColumnName + " ";
}
MessageBox.Show("Primary KEY fields: " + strMessage);
}
else
{
MessageBox.Show("NO PRIMARY KEY");
}
I have tried using CommandBehavior.KeyInfo and
CommandBehvavior.SchemaOnly as the arguments as well, with the same
results.
Anyone know what i am doing wrong?