如何从连接字符串中获取dbname,servername,user,password .... [英] how to get dbname, servername, user, password, .... from connection string
问题描述
如何获得:
- 数据库
- 服务器
- 用户
- 密码
- 是集成安全
来自连接字符串
Database = DbName; Server = SomeServer; Integrated Security = SSPI;
或
Database = DbName; Server = SomeServer; User Id = myUsername; Password = myPassword ;
Hi,
How to get :
- database
- server
- user
- password
- is integrated security
from connection string
Database=DbName;Server=SomeServer;Integrated Security=SSPI;
or
Database=DbName;Server=SomeServer;User Id=myUsername;Password=myPassword;
推荐答案
您必须解析字符串。有几种方法可以做到这一点,你做哪种方式完全基于你自己的需要。
0)你可以使用string.Split方法,拆分'';''字符,然后解析生成的子字符串
1)你可以使用string.IndexOf()方法来查看字符串对于特定项目。
You have to parse the string. There are a couple of ways to do it, and which way you do it is completely based on your own needs.
0) You can use the string.Split method, splitting on the '';'' character, and then parsing the resulting substrings
1) You can use the string.IndexOf() method that walks the string looking for specific items.
您是否有机会查看:
SqlConnection..:::ConnectionString Property [ ^ ]
一旦你分配了连接字符串到SqlConnection
对象,您可以单独访问它们。
例如:
Did you got a chance to look at:
SqlConnection..::.ConnectionString Property[^]
Once you assign the connection string toSqlConnection
object, you can access them separately.
For ex:
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connectionString;
// Like this:
// conn.DataSource
// conn.Database
//
// some work
}
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = ConfigurationManager.ConnectionStrings [inventoryDBConnection]。ConnectionString; ;
string server = builder [Data Source] as string;
string database = builder [Initial Catalog] as string ;
string UserID = builder [User ID] as string;
string password = builder [Password] as string;
System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = ConfigurationManager.ConnectionStrings["inventoryDBConnection"].ConnectionString; ;
string server = builder["Data Source"] as string;
string database = builder["Initial Catalog"] as string;
string UserID = builder["User ID"] as string;
string password = builder["Password"] as string;
通过这种方式,您可以获得所有房产。
您可以从此MSDN链接中找到更多帮助:
msdn'的链接 < br $> b $ b
希望这会对你有所帮助。
谢谢。
This way you can get all the properties.
You may find more help from this MSDN''s link:
msdn''s link
Hope this will help you.
Thanks.
这篇关于如何从连接字符串中获取dbname,servername,user,password ....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!