How can I programmatically g开发者_运维问答et the name of the Oracle database I am connecting to? I tried:
using (OracleConnection connection = new OracleConnection(oraConnectStr))
{
connection.Open();
return connection.Database;
}
but it returns empty string. I can't use the whole connection string because it may contain username/password.
Not every user has access to the V$ views. But everybody can run this:
SQL> select * from global_name
2 /
GLOBAL_NAME
---------------------------------------------------
ORCL
SQL>
edit
If you want to solve this from inside c#, this article shows how to solve it through the ConnectionString.
you could query v$database
:
SQL> SELECT NAME FROM v$database;
NAME
---------
PROD
You can use OracleConnectionStringBuilder object.
OracleConnectionStringBuilder connStrBuilder = new OracleConnectionStringBuilder(oraConnectStr);
connStrBuilder.DataSource will contain your information.
Check out: http://download.oracle.com/docs/html/E10927_01/OracleConnectionStringBuilderClass.htm#CHDHBCDA
精彩评论