Microsoft 企业库连接字符串

发布于 2024-10-06 03:57:02 字数 197 浏览 0 评论 0原文

大家好,我已成功从代码中显示的数据库实例中获取数据。但是如何从数据库实例中获取数据库名称。我找不到任何与此相关的属性。请帮忙。

private Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("ConnString");

Hi all I've successfully got data out of the database instance shown in the code. But how do I get the database name from the database instance. I can't find any properties regarding this. Please help.

private Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("ConnString");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2024-10-13 03:57:02

对此没有明确的属性,因为数据库是一个独立于数据库技术的类,并且“数据库名称”的概念是特定于数据库的。哎呀,像 Sqlite 或 SqlCE 这样的东西甚至没有“数据库名称”,只有文件名。

如果您知道数据库的类型,您可以使用“_db.ConnectionString”获取连接字符串,然后对其进行解析。每个 ADO.NET 提供程序都包含一个连接字符串生成器类来为您进行解析。

例如,如果您有 MS Sql 连接字符串,则可以通过以下方式获取数据库名称:

var connectionStringBuilder = new SqlConnectionStringBuilder(_db.ConnectionString);
string databaseName = connectionStringBuilder.InitialCatalog;

不同的数据库提供商当然会使用不同的属性和术语来为您提供此信息。

There is no explicit property for this, because Database is a database technology independent class, and the concept of "database name" is db specific. Heck, things like Sqlite or SqlCE don't even have "database names", just filenames.

You can use "_db.ConnectionString" to get the connection string back out and then parse through that if you know the type of database. Each ADO.NET provider includes a connection string builder class to do that parsing for you.

For example, if you have a MS Sql connection string, you can get the database name this way:

var connectionStringBuilder = new SqlConnectionStringBuilder(_db.ConnectionString);
string databaseName = connectionStringBuilder.InitialCatalog;

Different database providers will of course use different properties and terminology to give you this information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文