正确的方式来获得连接字符串的用户名和密码? [英] Right way to get username and password from connection string?
本文介绍了正确的方式来获得连接字符串的用户名和密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个连接字符串是这样的:
SERVER =本地主机; DATABASE =树; UID =根;密码=分支;闵池大小= 0;最大池大小= 200
我如何获得各种数据库参数出来的吗?我可以得到数据库名和服务器这样的:
服务器名称= conObject.DataSource;
DBNAME = conObject.Database;
我所需要的用户名和密码,以及与此类似。无属性被设置MySqlConnection对象上
目前我做的是这样的:
公共静态无效GetDatabaseParameters(字符串的connectionString,出字符串服务器名,出字符串DBNAME,出用户名字符串,字符串出密码)
{
匹配M = Regex.Match(的connectionString,SERVER =(*?);(。*?)DATABASE =; UID =(。*?); PASSWORD =(。*?); *);
//服务器= m.Groups [1] .value的;
// DBNAME = m.Groups [2] .value的; $ B $ 2.用户名= m.Groups [3] .value的;
密码= m.Groups [4] .value的;
}
是否有一个接受的做法在这里?
< DIV CLASS =h2_lin>解决方案
您可以使用的 SqlConnectionStringBuilder 类
字符串conString =SERVER =本地主机; DATABASE =树; UID =根;密码=分支;闵池大小= 0;最大池大小= 200;
SqlConnectionStringBuilder建设者=新SqlConnectionStringBuilder(conString);
字符串用户= builder.UserID;
字符串传递= builder.Password;
I have a connection string like this:
"SERVER=localhost;DATABASE=tree;UID=root;PASSWORD=branch;Min Pool Size = 0;Max Pool Size=200"
How do I get the various database parameters out of it? I can get database name and server like this:
serverName = conObject.DataSource;
dbName = conObject.Database;
I need the username and password as well similarly. No property is set on the MySqlConnection object.
At present I do it like this:
public static void GetDatabaseParameters(string connectionString, out string serverName, out string dbName, out string userName, out string password)
{
Match m = Regex.Match(connectionString, "SERVER=(.*?);DATABASE=(.*?);UID=(.*?);PASSWORD=(.*?);.*");
//serverName = m.Groups[1].Value;
//dbName = m.Groups[2].Value;
userName = m.Groups[3].Value;
password = m.Groups[4].Value;
}
Is there an accepted practice here?
解决方案
You could use the SqlConnectionStringBuilder Class
string conString = "SERVER=localhost;DATABASE=tree;UID=root;PASSWORD=branch;Min Pool Size = 0;Max Pool Size=200";
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(conString);
string user = builder.UserID;
string pass = builder.Password;
这篇关于正确的方式来获得连接字符串的用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文