New Posts New Posts RSS Feed: How to get Database Name?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How to get Database Name?

 Post Reply Post Reply
Author
JPeacock View Drop Down
Newbie
Newbie


Joined: 15-Jun-2007
Location: Canada
Posts: 2
Post Options Post Options   Quote JPeacock Quote  Post ReplyReply Direct Link To This Post Topic: How to get Database Name?
    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
Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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.



Edited by davidklitzke - 03-Dec-2007 at 9:39pm
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down