2

I would like to inform my visitors of which database they are currently interacting with. My application only has a single Entity Framework connection string in it's web.config. The database name (initial catalog) is part of my connection string stored in my web.config.

I can expose the ENTIRE connection string in the shared layout by including the following html.

You are connecting to <span class="databasename">@System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString)</span>!

But I don't need the entire connection string only the database name. Is there a way to do this without involving a controller or instantiating a new SqlConnection()? Any suggestions?

tereško
  • 58,060
  • 25
  • 98
  • 150
Troy Witthoeft
  • 2,498
  • 2
  • 28
  • 37
  • Why do you want to dynamically generate this information if you only allow one conection? – Stuart Apr 02 '13 at 16:48
  • Because I maintain three version of the application (dev,test,production) where the the only core difference is the connection string. Having the db name in the page header is convenient way to identify which database I am interacting with. – Troy Witthoeft Apr 02 '13 at 19:41

2 Answers2

2

You could try something like this:

@{
    var sqlDB = new System.Data.SqlClient.SqlConnectionStringBuilder(
        System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString)
    );
    var dbName = sqlDB.InitialCatalog;
}

You are connecting to <span class="databasename">@dbName</span>!

That will parse the connection string and return the database. Or if you want a different property from the connection string, just use a different property off of the SqlConnectionStringBuilder object.

Eilon
  • 25,582
  • 3
  • 84
  • 102
  • Headed out for lunch, but this looks like it could work. I will let you know in a bit. Thanks! – Troy Witthoeft Apr 02 '13 at 17:33
  • I tried your solution and received a run-time error that `keyword not supported 'metadata'` This [thread](http://stackoverflow.com/questions/1404268/keyword-not-supported-data-source) helped shed some light on the fact that I should be using EntityConnectionStringBuilder instead of SqlConnectionStringBuilder to tease out the dbname. – Troy Witthoeft Apr 02 '13 at 19:32
  • My final solution is a bit cludgy, but works.... `@{ var entityConnectionString = new System.Data.EntityClient.EntityConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionStringName"].ConnectionString); var ProviderConnectionString = entityConnectionString.ProviderConnectionString; var sqlDB = new System.Data.SqlClient.SqlConnectionStringBuilder(ProviderConnectionString); var dbName = sqlDB.InitialCatalog; }` Thanks for leading me in the right direction Eilon! – Troy Witthoeft Apr 02 '13 at 19:33
1

I use

@using Firm.Applic.Common
@Html.RenderDataSource() 

in the markup and this HtmlHelper in a class

using System.Web;
using System.Web.Mvc;
using System.Configuration;
using System.Data.Common;

namespace Firm.Applic.Common
{
    public static class HtmlHelpers
    {
        public static IHtmlString RenderDataSource(this HtmlHelper htmlHelper)
        {
            var cnstr = ConfigurationManager.ConnectionStrings["ORA"].ConnectionString;
            var builder = new DbConnectionStringBuilder();
            builder.ConnectionString = cnstr;
            return new MvcHtmlString(builder["Data Source"].ToString());
        }
    }   
}
user3780177
  • 41
  • 1
  • 2