如何在MVC4/Razor中向访问者显示数据库名称?

时间:2022-11-29 22:53:21

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.

我想通知我的访客他们目前正在与哪个数据库交互。我的应用程序在它的web.config中只有一个实体框架连接字符串。数据库名称(初始目录)是存储在web.config中的连接字符串的一部分。

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

通过包含以下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?

但是我不需要整个连接字符串,只需要数据库名。是否有一种方法可以在不涉及控制器或实例化新的SqlConnection()的情况下做到这一点?有什么建议吗?

2 个解决方案

#1


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.

这将解析连接字符串并返回数据库。或者,如果您希望使用与连接字符串不同的属性,只需使用SqlConnectionStringBuilder对象之外的另一个属性。

#2


0  

I use

我使用

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

in the markup and this HtmlHelper in a class

在标记和这个HtmlHelper类中

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());
        }
    }   
}

#1


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.

这将解析连接字符串并返回数据库。或者,如果您希望使用与连接字符串不同的属性,只需使用SqlConnectionStringBuilder对象之外的另一个属性。

#2


0  

I use

我使用

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

in the markup and this HtmlHelper in a class

在标记和这个HtmlHelper类中

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());
        }
    }   
}