C# 数据库连接字符串信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/706566/
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-04 14:10:40  来源:igfitidea点击:

Database Connection String Info

c#

提问by Ted Smith

In .Net is there a class in .Net where you can get the DB name, and all the connection string info without acutally doing a substring on the connection string?

在 .Net 中,.Net 中是否有一个类,您可以在其中获取数据库名称和所有连接字符串信息,而无需在连接字符串上执行子字符串?

EDIT:

编辑:

I am not creating a connection I am attempting to get info out of the connection string. So I am basicly looking for something that takes a connection string arg and has accessors to dbName, connection type, etc....

我不是在创建连接,而是试图从连接字符串中获取信息。所以我基本上是在寻找带有连接字符串 arg 并具有访问 dbName、连接类型等的东西......

采纳答案by Adam Robinson

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 示例,您可以执行以下两种操作之一:

Given

给定的

string connectionString = "Data Source = .\SQLEXPRESS;Database=Northwind;Integrated Security=True;";

You could do...

你可以做...

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

builder.ConnectionString = connectionString;

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

Or

或者

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

builder.ConnectionString = connectionString;

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

回答by Nathan Koop

Yep ConnectionInfo

是的连接信息

http://msdn.microsoft.com/en-us/library/ms226340(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ms226340(VS.80).aspx

EDIT: I, along with Chris, realized that this is only works if you have the Crystal Reports namespaces imported. Otherwise I'm not sure

编辑:我和 Chris 一起意识到这只有在您导入了 Crystal Reports 命名空间时才有效。否则我不确定

回答by J.W.

After you initialize the connection with the connection string, you can get those information from properties of the initialized connection object.

使用连接字符串初始化连接后,您可以从初始化的连接对象的属性中获取这些信息。

Check System.Data.Common.DbConnection.

检查 System.Data.Common.DbConnection。

回答by Chris Doggett

ConnectionInfo connectionInfo = new ConnectionInfo ();
connectionInfo = logOnInfo.ConnectionInfo;

connectionInfo.DatabaseName = database;
connectionInfo.ServerName = server;
connectionInfo.Password = password;
connectionInfo.UserID = user;

EDIT: Looks like Nathan beat me to it, as mine is from the same page.

编辑:看起来内森击败了我,因为我的来自同一页面。

EDIT AGAIN: I should note that ConnectionInfo is in the CrystalDecisions.Shared namespace. As for how to get it from a generic DB connection, I'm not sure.

再次编辑:我应该注意到 ConnectionInfo 在 CrystalDecisions.Shared 命名空间中。至于如何从通用数据库连接中获取它,我不确定。

回答by CodeMonkey1313

SqlConnection sq = new SqlConnection(ConnectionString);

Reference

参考

Done with "using" statement (from MSDN)

使用“使用”语句完成(来自 MSDN)

using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        // Do work here; connection closed on following line.
    }

回答by Mutant

if you build your connection string using the Connection string builder (e.g. OracleConnectionStringBuilde, and it will be different for different database), in that case easily retrieve the information out of it.

如果您使用连接字符串构建器(例如 OracleConnectionStringBuilde,它会因不同的数据库而不同)构建您的连接字符串,在这种情况下可以轻松地从中检索信息。

here it explained:

在这里它解释说:

http://msdn.microsoft.com/en-us/library/ms254947.aspx

http://msdn.microsoft.com/en-us/library/ms254947.aspx