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

时间:2022-04-08 11:14:48

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?

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

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从连接字符串获取数据库名称。(数据源是服务器名吗?)

7 个解决方案

#1


43  

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...

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

#2


104  

You can use the provider-specific ConnectionStringBuilder class (within the appropriate namespace), or System.Data.Common.DbConnectionStringBuilder to 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;

#3


23  

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.

同样,您也可以从connection对象获得服务器名。

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

#4


8  

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

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);

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

#5


5  

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.

#6


4  

Database name is a value of SqlConnectionStringBuilder.InitialCatalog property.

数据库名称是SqlConnectionStringBuilder的值。InitialCatalog财产。

#7


3  

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

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

#1


43  

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...

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

#2


104  

You can use the provider-specific ConnectionStringBuilder class (within the appropriate namespace), or System.Data.Common.DbConnectionStringBuilder to 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;

#3


23  

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.

同样,您也可以从connection对象获得服务器名。

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

#4


8  

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

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString);

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

#5


5  

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.

#6


4  

Database name is a value of SqlConnectionStringBuilder.InitialCatalog property.

数据库名称是SqlConnectionStringBuilder的值。InitialCatalog财产。

#7


3  

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

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