Skip to main content
 首页 » 编程设计

c#之FCL中有SqlConnectionStringBuilder的解析部分吗

2024年02月24日20zhengyun_ustc

我正在使用 SqlConnectionStringBuilder解析连接字符串的实例,但不想检查其键名称的有效性。根据行为,如果在字符串中遇到不受支持的键,构建器将抛出异常。

例如,未知键“whatever”的异常是:

Keyword not supported: 'whatever'.

我想做的是无一异常(exception)地解析整个连接字符串,尽管关键字不受支持。

如果不编写代码来执行此技巧,是否仅对 .NET Framework 中的解析方面提供 native 支持,或者我能否以某种方式连接到连接构建器类中已经内化的内容?

请您参考如下方法:

您可以使用基础 DbConnectionStringBuilder不验证其 key 的类。

Developers can create connection strings using either a strongly typed connection string builder class, or they can use the DbConnectionStringBuilder class. The DbConnectionStringBuilder performs no checks for valid key/value pairs. Therefore, it is possible using this class to create invalid connection strings. The SqlConnectionStringBuilder supports only key/value pairs that are supported by SQL Server; trying to add invalid pairs will throw an exception.