tags:

views:

237

answers:

1

I am using Oracle Express Edition and I want to get table information (column name,type,size,etc..) programmatically , inside my program (.Net C#, Microsoft oracle connector).

In SqlPlus, when i give command "desc <tablename>" it works perfectly and print all information about table.

Now i want to do it programmatically. I tried to give query "desc <tablename>" but it failed. Then i read somewhere that desc is a stored procedure and we have to call it as a stored procedure,then i tried following code, its failing with error,

Code 1

private OracleDataReader OracleDescribe(string tablename)
{
    OracleCommand cmd = new OracleCommand("describe " + tablename, OracleConn);
    return cmd.ExecuteReader();
}

Error 1

Invalid SQL Statement

Code 2

private OracleDataReader OracleDescribe(string tablename)
{
    OracleCommand cmd = new OracleCommand("describe", OracleConn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("table", OracleType.VarChar).Value = tablename;
    return cmd.ExecuteReader();
}

Error 2

Encountered the symbol ">" when expecting one of the following: (
+1  A: 

Use a query on the system's meta-data views/tables. E.g.:

SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, DATA_LENGTH, 
       DATA_PRECISION, DATA_SCALE, NULLABLE, DATA_DEFAULT, CHAR_LENGTH 
FROM ALL_TAB_COLUMNS 
WHERE OWNER='SCOTT' AND TABLE_NAME IN ('EMP', 'DEPT') 
ORDER BY TABLE_NAME ASC, COLUMN_ID ASC

You could also look into the GetSchema method on DbConnection, (if you're using .NET 2.0 or higher, recommended), and check the specific views defined in the MSDN.

Frans Bouma