我使用的是Oracle防爆preSS版,我想表信息(列名称,类型,大小等)编程,里面我的程序(.NET C#,微软甲骨文连接器)。
在sqlplus,当我给命令DESC<表名>中它完美的作品和打印有关表格的所有信息。
现在我想以编程方式做到这一点。我试着给查询DESC<表名>中但是失败了。然后,我读的地方,说明是一个存储过程,我们必须把它作为一个存储过程,那么我想下面的code,它的失败,错误,
code 1
私人OracleDataReader OracleDescribe(字符串表名)
{
的OracleCommand CMD =新的OracleCommand(形容+表名,OracleConn);
返回cmd.ExecuteReader();
}
错误1
无效的SQL语句
code 2
私人OracleDataReader OracleDescribe(字符串表名)
{
的OracleCommand CMD =新的OracleCommand(形容,OracleConn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(表,OracleType.VarChar)。价值=表名;
返回cmd.ExecuteReader();
}
错误2
出现符号>中当在需要下列之一:(
解决方案
使用的查询对系统的元数据视图/表。例如:
SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE,DATA_LENGTH,
DATA_ preCISION,DATA_SCALE,空,DATA_DEFAULT,CHAR_LENGTH
从ALL_TAB_COLUMNS
WHERE OWNER ='SCOTT'和TABLE_NAME IN('EMP','DEPT')
ORDER BY TABLE_NAME ASC,COLUMN_ID ASC
您也可以考虑上的DbConnection GetSchema方法,(如果你使用.NET 2.0或更高版本,推荐),并检查MSDN中定义的具体意见。
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: (
解决方案
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.