数据库连接字符串信息 [英] Database Connection String Info

查看:103
本文介绍了数据库连接字符串信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.NET是有在.net中的一类,你可以得到数据库的名称,完全不需要做实际上可以在连接字符串的子字符串连接字符串信息?

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?

编辑:

我没有创造我试图获取信息出来的连接字符串的连接。所以我basicly寻找的东西需要一个连接字符串Arg和具有访问到DBNAME,连接类型,等等...

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

推荐答案

您可以使用特定的提供者ConnectionStringBuilder类(适当的命名空间内),或 System.Data.Common.DbConnectionStringBuilder 来抽象,如果你需要连接字符串对象。你需要知道用来指定你正在寻找的信息的提供者特有的关键字,但对于一个SQL Server例如,你可以做以下任一两件事情:

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:

由于

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

您可以做...

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;

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

builder.ConnectionString = connectionString;

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

这篇关于数据库连接字符串信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆