在考虑转义字符时,用分号拆分字符串

Jus*_*tin 3 c# string connection-string

真的很简单的问题:

我想将连接字符串拆分为其关键字/值对,例如以下连接字符串:

Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=vm-jp-dev2;Data Source=scsql\sql2005;Auto Translate=False
Run Code Online (Sandbox Code Playgroud)

会成为:

Provider=SQLOLEDB.1
Integrated Security=SSPI
Persist Security Info=False
Initial Catalog=vm-jp-dev2
Data Source=scsql\sql2005
Auto Translate=False
Run Code Online (Sandbox Code Playgroud)

问题是MSDN文档声明如果值包含在单引号或双引号字符中,则允许连接字符串值包含分号(因此,如果我理解,则以下内容有效):

Provider="Some;Provider";Initial Catalog='Some;Catalog';...
Run Code Online (Sandbox Code Playgroud)

什么是拆分此字符串的最佳方式(在C#中)?

Rob*_*Day 9

有一个DBConnectionStringBuilder类可以做你想要的...

        System.Data.Common.DbConnectionStringBuilder builder = new System.Data.Common.DbConnectionStringBuilder();

        builder.ConnectionString = "Provider=\"Some;Provider\";Initial Catalog='Some;Catalog';";

        foreach (string key in builder.Keys)
        {
            Response.Write(String.Format("{0}: {1}<br>", key , builder[key]));
        }
Run Code Online (Sandbox Code Playgroud)