none
GetSchema and PrimaryKeys - getting primary key column name RRS feed

  • Question

  • Hi

    I looked at documentation for DbConnection.GetSchema method of ODP.NET library.

    When I call (to get primary keys metadata)

    Code Snippet

    DataTable pkeys = DbConnection.GetSchema("PrimaryKeys"...
    foreach (DataRow dr in pkeys.Rows)
    {
    string pkTable = dr["TABLE_NAME"].ToString();
    // COLUMN_NAME does not exist
    string columnName = dr["COLUMN_NAME"].ToString();
    }



    I do not see a primary key column name in result.
    In core GetOleDbSchema("PrimaryKeys"... method there was a column name called COLUMN_NAME.

    How can I get primary key column names in this GetSchema method?


    Regards,
    Sebastijan
    Wednesday, March 5, 2008 4:14 PM

All replies

  • Hi

    try this

     

    Code Snippet

    string[] restrictions = new string[] { sDataBase, null, sTableName, "PK_" + sTableName };
    dataTable = con.GetSchema("IndexColumns", restrictions);

    foreach (DataRow row in dataTable.Rows)

    Console.writeln(row["column_name"]);

     

     

     

    Wednesday, April 2, 2008 12:34 PM
  • How about this?
    DataTable primaryKeys = conn.GetSchema("IndexColumns", new string[] { database, null, table, null });

    Thursday, September 1, 2011 5:53 PM
  • The following uses GetSchemaTable where the column name for a key column is IsKey.

    MSDN http://support.microsoft.com/kb/310107

    private void SchemaDemo(string ConnectionString)
    {
    	using (OleDbConnection cn = new OleDbConnection(ConnectionString))
    	{
    		using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM YourTableName", cn))
    		{
    			cn.Open() ;
    			OleDbDataReader Reader = cmd.ExecuteReader(CommandBehavior.KeyInfo) ;
    			var schemaTable = Reader.GetSchemaTable() ;
    			var KeyQuery = (
    					from T in schemaTable.AsEnumerable
    					where T.Field<bool>("IsKey")
    					select T.Field<string>("ColumnName")).ToArray() ;
    
    			if (KeyQuery.Count > 0)
    			{
    				Console.WriteLine("{0}", string.Join(",", KeyQuery)) ;
    			}
    		}
    	}
    }
    

     


    KSG
    Sunday, September 4, 2011 4:14 AM