C# 如何使用 SqlConnectionStringBuilder 从连接字符串中获取数据库名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10550541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:16:44  来源:igfitidea点击:

How to get Database Name from Connection String using SqlConnectionStringBuilder

c#asp.netsqlado.net

提问by Imran Rizvi

I never want to split connection string using string manipulation and get server,database,uid and password.

我从不想使用字符串操作拆分连接字符串并获取服务器、数据库、uid 和密码。

I read the following link and read the accepted answer , I found that is the best way to get userid and password out from connection string, but what about Database Name?

我阅读了以下链接并阅读了接受的答案,我发现这是从连接字符串中获取用户 ID 和密码的最佳方法,但是数据库名称呢?

Right way to get username and password from connection string?

从连接字符串中获取用户名和密码的正确方法?

How to get Database Name from Connection String using SqlConnectionStringBuilder. (does the DataSource is the Server name?)

如何使用 SqlConnectionStringBuilder 从连接字符串中获取数据库名称。(数据源是服务器名称吗?)

采纳答案by unarity

See MSDN documentation for InitialCatalog Property:

请参阅InitialCatalog 属性的 MSDN 文档

Gets or sets the name of the database associated with the connection...

This property corresponds to the "Initial Catalog" and "database" keys within the connection string...

获取或设置与连接关联的数据库的名称...

此属性对应于连接字符串中的“初始目录”和“数据库”键...

回答by Dennis

Database name is a value of SqlConnectionStringBuilder.InitialCatalogproperty.

数据库名称是SqlConnectionStringBuilder.InitialCatalog属性的值。

回答by Habib

You can use InitialCatalogProperty or builder["Database"]works as well. I tested it with different case and it still works.

您可以使用InitialCatalog属性或也可以使用builder["Database"]。我用不同的情况测试了它,它仍然有效。

回答by Kishore Kumar

string connectString = "Data Source=(local);" + "Integrated Security=true";

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);

Console.WriteLine("builder.InitialCatalog = " + builder.InitialCatalog);

回答by Romil Kumar Jain

You can use the provider-specific ConnectionStringBuilder class (within the appropriate namespace), or System.Data.Common.DbConnectionStringBuilderto abstract the connection string object if you need to. You'd need to know the provider-specific keywords used to designate the information you're looking for, but for a SQL Server example you could do either of these two things:

您可以使用特定于提供程序的 ConnectionStringBuilder 类(在适当的命名空间内),或者System.Data.Common.DbConnectionStringBuilder根据需要抽象连接字符串对象。您需要知道用于指定您要查找的信息的特定于提供程序的关键字,但对于 SQL Server 示例,您可以执行以下两种操作之一:

System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);

string server = builder.DataSource;
string database = builder.InitialCatalog;

or

或者

System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();

builder.ConnectionString = connectionString;

string server = builder["Data Source"] as string;
string database = builder["Initial Catalog"] as string;

回答by pvaju896

this gives you the Xact;

这为您提供了 Xact;

System.Data.SqlClient.SqlConnectionStringBuilder connBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();

connBuilder.ConnectionString = connectionString;

string server = connBuilder.DataSource;           //-> this gives you the Server name.
string database = connBuilder.InitialCatalog;     //-> this gives you the Db name.

回答by nawfal

A much simpler alternative is to get the information from the connection object itself. For example:

一个更简单的替代方法是从连接对象本身获取信息。例如:

IDbConnection connection = new SqlConnection(connectionString);
var dbName = connection.Database;

Similarly you can get the server name as well from the connection object.

同样,您也可以从连接对象中获取服务器名称。

DbConnection connection = new SqlConnection(connectionString);
var server = connection.DataSource;