解析连接字符串

发布于 2024-12-21 10:49:29 字数 569 浏览 2 评论 0原文

是否有标准库或代码片段可以使用这样的连接字符串获取值?

string connstr = "DataServiceUrl=http://localhost/foo;" + 
        "RemoteServerConnection={server=http://localhost/foo2;interface=0.0.0.0;};" + 
        "publisherport=1234;StatisticsURL=http://localhost/foo3";

整个内部连接属性有点将其置于循环中。我想根据键获取特定值。

这是我使用的约翰发布的答案:

System.Data.Odbc.OdbcConnectionStringBuilder builder = new System.Data.Odbc.OdbcConnectionStringBuilder(); 
builder.ConnectionString = this.ConnectionString;
MessageBox.Show(builder["RemoteServerConnection"]);

Is there a standard library or code snippet to get a value with a connection string like this?

string connstr = "DataServiceUrl=http://localhost/foo;" + 
        "RemoteServerConnection={server=http://localhost/foo2;interface=0.0.0.0;};" + 
        "publisherport=1234;StatisticsURL=http://localhost/foo3";

The whole inner connection property is kind of throwing this in a loop. I'd like to get specific values based on a key.

Here was the answer posted by John I used:

System.Data.Odbc.OdbcConnectionStringBuilder builder = new System.Data.Odbc.OdbcConnectionStringBuilder(); 
builder.ConnectionString = this.ConnectionString;
MessageBox.Show(builder["RemoteServerConnection"]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2024-12-28 10:49:29

将“{”和“}”替换为()即可解决问题:

string conn = "DataServiceUrl=http://localhost/foo;" +
    "RemoteServerConnection={server=http://localhost/foo2;interface=0.0.0.0;};" +
    "publisherport=1234;StatisticsURL=http://localhost/foo3";

var builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = conn.Replace("{", "\"").Replace("}", "\"");
var keys = builder.Keys;
var values = builder.Values;
string remoteServerConnection = (string)builder["RemoteServerConnection"];

Replacing "{" and "}" by (") does the trick:

string conn = "DataServiceUrl=http://localhost/foo;" +
    "RemoteServerConnection={server=http://localhost/foo2;interface=0.0.0.0;};" +
    "publisherport=1234;StatisticsURL=http://localhost/foo3";

var builder = new System.Data.Common.DbConnectionStringBuilder();
builder.ConnectionString = conn.Replace("{", "\"").Replace("}", "\"");
var keys = builder.Keys;
var values = builder.Values;
string remoteServerConnection = (string)builder["RemoteServerConnection"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文