简体   繁体   English

如何替换部分连接字符串?

[英]How to replace part of Connection String?

I'm using c# and I need to replace a string with other data. 我正在使用c#,我需要用其他数据替换字符串。 This is the string, the servername, user1 and 380kj data and length can change. 这是字符串,servername,user1和380kj数据和长度可以更改。

"Data Source=servername;User ID=user1;Password=380kj"

I want this result: 我想要这个结果:

"Data Source=servername;User ID=XXXXX;Password=XXXXX"

I have done an IndexOf on User ID and Password but I don't know how to get the exact count to use the remove function so I can then insert the XXXXX into the string. 我已经在用户ID和密码上做了一个IndexOf但是我不知道如何获得使用remove函数的确切计数,所以我可以将XXXXX插入到字符串中。

   int index = SaleDatabase.ConnectionString.IndexOf("User ID=");
   int index2 = SaleDatabase.ConnectionString.IndexOf(";Password");

What can I do? 我能做什么?

thanks... 谢谢...

You can use the String.Replace method, but it seems that in your case you are editing a database connection string, in which case you should use the specific ConnectionStringBuilder for that connection string, such as: 您可以使用String.Replace方法,但在您的情况下,您似乎正在编辑数据库连接字符串,在这种情况下,您应该使用特定的ConnectionStringBuilder作为该连接字符串,例如:

string connStr = "Data Source=servername;User ID=user1;Password=380kj";
System.Data.SqlClient.SqlConnectionStringBuilder sb = new System.Data.SqlClient.SqlConnectionStringBuilder(connStr);
sb.UserID = "XXXXX";
sb.Password = "XXXXX";
connStr = sb.ToString();

Why not split the string by using Split Method by character (";"). 为什么不通过使用Split Method by character(“;”)拆分字符串。 Result will be array. 结果将是数组。 Then u can fetch value from result array using simple linq or foreach loop 然后你可以使用简单的linq或foreach循环从结果数组中获取值

You should definitely use the ConnectionStringBuilder in this specific case as @MA Hamm suggested. 在@MA Hamm建议的情况下,你肯定应该在这个特定的情况下使用ConnectionStringBuilder But for completeness, you can also do something like this with a simple Regex.Replace . 但为了完整Regex.Replace ,您还可以使用简单的Regex.Replace执行此类操作。 Something like: 就像是:

    Regex r = new Regex(@"User ID=[^;]*");
    String newString = r.Replace(s,"User ID=XXXX");

Will look for text starting with User ID= and capture all characters up to the next ; 将查找以User ID=开头的文本并捕获所有字符直到下一个; (note: if user id contains a ; this would break - but it probably would have broken anyway and would need to have been escaped). (注意:如果用户id包含一个;这会破坏 - 但它可能会破坏,并且需要被转义)。 Then we just replace the matched string with our new string. 然后我们用新字符串替换匹配的字符串。

Doing the same for Password= is left as an exercise for the reader. Password=做同样的Password=留给读者练习。

Just use string.Format and generate a new string , for sample: 只需使用string.Format并为示例生成一个新string

public static string GenerateConnectionString(string dataSource, string user, string password)
{
   return string.Format("Data Source={0};User ID={1};Password={2}", dataSource, user, password);
}

Some solutions can be very simple. 一些解决方案可以非常简单。 Read more about KISS 了解更多关于KISS的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM