tags:

views:

58

answers:

3

Hi,

How can I programmatically get 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.

+2  A: 

Hi Grzenio,

you could query v$database:

SQL> SELECT NAME FROM v$database;

NAME
---------
PROD
Vincent Malgrat
And supposing he can't connect to the database?! This problem is solvable without a network connection, no?
Stefan Kendall
+4  A: 

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.

APC
A: 

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

Black Frog