none
PrimaryKey does not work for Oracle? RRS feed

  • Question

  • Hello,

    I am trying to get some basic information about a table in Oracle (10.x) from my C# code, say, its primary key columns. 

    I am following this example: http://msdn.microsoft.com/en-us/library/system.data.datatable.primarykey(VS.71).aspx
    But I found it can not get the primary keys of my table (it returns nothing).

    Any idea of why?

    Thanks a lot.

    My code is as simple as:

    using System.Data.OracleClient;

                    string connectionString = xxxxx;
                    OracleConnection connection = new OracleConnection(connectionString);
                    connection.Open();
                    OracleCommand command = connection.CreateCommand();
                    string queryString = "select * from ITEM";
                    command.CommandText = queryString;
                    OracleDataReader reader = command.ExecuteReader();
                    DataTable schema = reader.GetSchemaTable();
                    DataColumn[] colArr = schema.PrimaryKey;
                    checkedListBoxTables.Items.Add("PK Column Count: " + colArr.Length);


    Monday, March 23, 2009 9:48 PM

Answers

  • Hi,

    Please use CommandBehavior.KeyInfo when you are calling ExecuteReader to retrive primary key information.


    This posting is provided "AS IS" with no warranties, and confers no rights.  You assume all risk for your use.
    • Marked as answer by Kevin_2009C Tuesday, March 24, 2009 10:30 PM
    Tuesday, March 24, 2009 5:32 PM

All replies

  • Hi,

    Please use CommandBehavior.KeyInfo when you are calling ExecuteReader to retrive primary key information.


    This posting is provided "AS IS" with no warranties, and confers no rights.  You assume all risk for your use.
    • Marked as answer by Kevin_2009C Tuesday, March 24, 2009 10:30 PM
    Tuesday, March 24, 2009 5:32 PM
  • Thanks!

    Just in case someone else is looking, here is more info:
    http://msdn.microsoft.com/en-us/library/system.data.commandbehavior.aspx

    Tuesday, March 24, 2009 10:30 PM
  • Hey, it is hard to believe, but today when I re-run the program, it no longer works!
    What could be wrong there? I didn't change anything as I remember. :-(

    Thursday, April 16, 2009 7:22 PM
  • Well, I found I can still check whether a column is pk or not by checking the isKey, but not by schema.PrimaryKey.

    Thursday, April 16, 2009 9:28 PM