日期:2014-05-20  浏览次数:21066 次

急,已知一个连接字符串s="packet size=4096;user id=sa;....", 求连接串中的,用户名...和密码? 试问一定要分解字符串么,详细如下:

  public   static   string   sqlConnectionString   =   "packet   size=4096;user   id=sa;data   source= "   +   ". "   +   ";persist   security   info=True;initial   catalog=plan;password=aaa ";

现在俺想求得   sqlConnectionString   中的   用户名,和密码   ,赋给变量   username   ,pwd

怎么实现?

------解决方案--------------------
正则表达式..
------解决方案--------------------
具体如何替换见你的另外一贴..
------解决方案--------------------
public static string sqlConnectionString = "packet size=4096;user id=sa;data source= " + ". " + ";persist security info=True;initial catalog=plan;password=aaa ";

方案1。
string[] str = sqlConnectionString.Split( new char[]{ '= ', ': '});
for(int i= 0;i <str.Lenght;i++
{
switch(str[i])
{
case "user id ":
username=str[i+1];i++;break;
case "password ":
pwd=str[i+1];i++;break;
default:
break;
}
}

方案2。
username = "user id= ";
pwd= "password= ";
int pos1 = sqlCOnnectionString.Find(username);
int pos2 = sqlCOnnectionString.Find( "; ",pos1);
username = sqlCOnnectionString.SubString(pos1+username.Length,pos2);
pwd ....
方案3。
正则//MSDN不在,写不出来了

------解决方案--------------------
DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
builder.ConnectionString = sqlConnectionString ;
string username = builder[ "User ID "];