Print Page | Close Window

How to get Database Name?

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=576
Printed Date: 11-Jun-2026 at 3:05pm


Topic: How to get Database Name?
Posted By: JPeacock
Subject: How to get Database Name?
Date Posted: 03-Dec-2007 at 1:01pm
Does anyone know how to get the name of the database currently connected to?
 
The client can choose Database A or Database B to connect to.
 
I'm trying to write some error logging code and I would like to have in the error log information which database the client logged into and if possible some information from the extended properties of the database.
 
Does anyone know if this is possible?
If it is where I can find some documentation on how to do it.
If there is no documentation provide an example?
 
Thanks



Replies:
Posted By: davidklitzke
Date Posted: 03-Dec-2007 at 9:19pm
The secret to getting the database name is to get the rdbKey.  Here is some code I have often shown to others to get the DatabaseTableName:

 

    /// <summary>Return specified Rdb entity type's DATABASE table name

    /// via a given PersistenceManager.</summary>

    /// <param name="pRdbEntityType">RdbEntityType whose name may be different

    /// than the corresponding SQL table name.</param>

    /// <param name="pManager">The PersistenceManager which is our gateway to

    /// the concrete database datasource.</param>

    /// <remarks>

    /// Includes table owner name, formatted per the applicable SQL provider, e.g.

    /// orderInfo.GetSqlTableName returns "dbo"."MachineSummary".

    /// </remarks>

    public static string GetSqlTableName(Type pRdbEntityType, PersistenceManager pManager) {

      RdbKey key = (RdbKey) pManager.DataSourceResolver.GetDataSourceKey(pRdbEntityType);

      RdbQuerySqlFormatter formatter = new RdbQuerySqlFormatter(key.AdoHelper);

      return formatter.QualifyTableName(pRdbEntityType);

    }

 

In a smilar manner, to get the database name, get an RdbKey from the PersistenceManager.  From the RdbKey, you can get the connection string.  From the connectionstring,  you can get the Database (or Host) name.




Print Page | Close Window