[英]Check if a table exists in an oracle sql database with c#
This code: 这段代码:
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='xxx'
The above code throws an exception: 上面的代码引发异常:
ORA-00900: invalid SQL statement
ORA-00900:无效的SQL语句
What did I do wrong? 我做错了什么? The above code worked long ago I could swear.
上面的代码很早以前就可以起效了。
Oracle不支持INFORMATION_SCHEMA,您需要使用ALL_TABLES,请参见此处
Have you migrated from another dbms? 您是否已从另一个dbms迁移?
AFAIK, Oracle does not support INFORMATION_SCHEMA (not even a subset of it) but you can retrieve a lot of metadata by querying the data dictionary . AFAIK,Oracle不支持INFORMATION_SCHEMA(甚至不支持它的一个子集),但是您可以通过查询数据字典来检索很多元数据 。
Assuming you want to check the schema you are currently connected to I would use user_tables
: 假设您要检查当前连接的架构,我将使用
user_tables
:
SELECT table_name
FROM USER_TABLES
WHERE table_name='xxx'
if you want to check the table is in in a different schema use all_tables
don't forget to add the owner predicate as the table may exist is several schemas : 如果要检查表是否在其他模式中,请使用
all_tables
,不要忘记添加所有者谓词,因为表可能存在几种模式:
SELECT table_name
FROM ALL_TABLES
WHERE table_name='xxx' AND owner='yourschemahere'
SELECT
TABLE_NAME
FROM
ALL_TABLES
WHERE
TABLE_NAME = 'YourTableName'
http://en.wikipedia.org/wiki/Oracle_metadata#Example_1:_finding_tables http://en.wikipedia.org/wiki/Oracle_metadata#Example_1:_finding_tables
On my most recent project requirements was to check if certain tables existed in an Oracle DB using C#. 在我最近的项目中,要求使用C#检查Oracle DB中是否存在某些表。
Prerequisites: 先决条件:
Oracle .Net Assembly Oracle .Net程序集
App.Config file with connection string 具有连接字符串的App.Config文件
I came up with this to meet this requirement. 我想出了这个来满足这个要求。
private static void CheckIfOracleTableExists()
{
try
{
string connectionString = ConfigurationManager.ConnectionStrings["dbConnectString"].ConnectionString;
if (connectionString == null) throw new Exception();
using (OracleConnection orclConn = new OracleConnection(connectionString))
using (OracleCommand orclCmd = new OracleCommand())
{
orclConn.Open();
orclCmd.Connection = orclConn;
string commandText = String.Format("SELECT COUNT(*) FROM " + DbTestName);
orclCmd.CommandText = commandText;
orclCmd.CommandType = CommandType.Text;
orclCmd.CommandTimeout = Convert.ToInt32(DbTimeout);
try
{
orclCmd.ExecuteScalar();
{
if (orclCmd.RowSize == 0) return;
TableExists = true;
orclConn.Close();
orclConn.Dispose();
orclCmd.Dispose();
}
}
catch (OracleException oex)
{
if (oex.ToString().Contains("table or view does not exist"))
{
Console.WriteLine("\r\n\tTable not found.");
}
TableExists = false;
}
catch (OracleException oex)
{
Console.WriteLine("{0}", oex);
TableExists = false;
}
catch (Exception ex)
{
if (ex.ToString().Contains("Object reference not set to an instance of an object"))
Console.WriteLine("\r\n\t Invalid Connection String.");
}
TableExists = false;
}
}
}//// / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
Actual code version of Kevin Burton's answer, with both the versions with and without schema: 凯文·伯顿(Kevin Burton)的答案的实际代码版本,带有和不带有模式的版本:
public Boolean TableExists(OracleConnection connection, String tableName)
{
return TableExists(connection, tableName, null)
}
public Boolean TableExists(OracleConnection connection, String tableName, String schema)
{
String sql;
if (schema == null)
sql = "SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME=:table";
else
sql = "SELECT TABLE_NAME FROM ALL_TABLES WHERE TABLE_NAME=:table AND OWNER=:schema";
OracleCommand command = new OracleCommand(sql, connection)
command.Parameters.AddWithValue("table", tableName);
if (schema != null)
command.Parameters.AddWithValue("schema", schema);
using (DbDataReader reader = command.ExecuteReader())
return reader.HasRows;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.