OdbcConnectionStringBuilder 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为创建和管理由 OdbcConnection 类使用的连接字符串的内容提供了一种简单方法。
public ref class OdbcConnectionStringBuilder sealed : System::Data::Common::DbConnectionStringBuilderpublic sealed class OdbcConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder[System.ComponentModel.TypeConverter(typeof(System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter))]
public sealed class OdbcConnectionStringBuilder : System.Data.Common.DbConnectionStringBuildertype OdbcConnectionStringBuilder = class
    inherit DbConnectionStringBuilder[<System.ComponentModel.TypeConverter(typeof(System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter))>]
type OdbcConnectionStringBuilder = class
    inherit DbConnectionStringBuilderPublic NotInheritable Class OdbcConnectionStringBuilder
Inherits DbConnectionStringBuilder- 继承
- 属性
示例
以下控制台应用程序为多个 ODBC 数据库生成连接字符串。 首先,该示例为 Microsoft Access 数据库创建一个连接字符串。 然后,它为 IBM DB2 数据库创建一个连接字符串。 该示例还分析现有连接字符串,并演示了操作连接字符串内容的各种方法。
注意
该示例包括一个密码以演示 OdbcConnectionStringBuilder 如何使用连接字符串。 在您的应用程序中,建议使用 Windows 身份验证。 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。
using System.Data.Odbc;
class Program
{
    static void Main()
    {
        OdbcConnectionStringBuilder builder =
            new OdbcConnectionStringBuilder();
        builder.Driver = "Microsoft Access Driver (*.mdb)";
        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Add("Dbq", "C:\\info.mdb");
        builder.Add("Uid", "Admin");
        builder.Add("Pwd", "pass!word1");
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();
        // Clear current values and reset known keys to their
        // default values.
        builder.Clear();
        // Pass the OdbcConnectionStringBuilder an existing
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString =
            "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" +
            "hostname=SampleServerName;port=SamplePortNum;" +
            "protocol=TCPIP;uid=Admin;pwd=pass!word1";
        Console.WriteLine("protocol = "
            + builder["protocol"].ToString());
        Console.WriteLine();
        // Modify existing items.
        builder["uid"] = "NewUser";
        builder["pwd"] = "Pass@word2";
        // Call the Remove method to remove items from
        // the collection of key/value pairs.
        builder.Remove("port");
        // Note that calling Remove on a nonexistent item does not
        // throw an exception.
        builder.Remove("BadItem");
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();
        // Setting the indexer adds the associated value, if
        // necessary.
        builder["NewKey"] = "newValue";
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}
Imports System.Data.Odbc    
Module Module1
  Sub Main()
    Dim builder As New OdbcConnectionStringBuilder()
    builder.Driver = "Microsoft Access Driver (*.mdb)"
    ' Call the Add method to explicitly add key/value
    ' pairs to the internal collection.
    builder.Add("Dbq", "C:\info.mdb")
    builder.Add("Uid", "Admin")
    builder.Add("Pwd", "pass!word1")
    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()
    ' Clear current values and reset known keys to their
    ' default values.
    builder.Clear()
    ' Pass the OdbcConnectionStringBuilder an existing 
    ' connection string, and you can retrieve and
    ' modify any of the elements.
    builder.ConnectionString = _
        "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" & _
        "hostname=SampleServerName;port=SamplePortNum;" & _
        "protocol=TCPIP;uid=Admin;pwd=pass!word1"
    Console.WriteLine("protocol = " & builder("protocol").ToString())
    Console.WriteLine()
    ' Modify existing items:
    builder("uid") = "NewUser"
    builder("pwd") = "Pass@word2"
    ' Call the Remove method to remove items from 
    ' the collection of key/value pairs.
    builder.Remove("port")
    ' Note that calling Remove on a nonexistent item does not
    ' throw an exception.
    builder.Remove("BadItem")
    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()
    ' The Item property is the default for the class, 
    ' and setting the Item property adds the value, if 
    ' necessary.
    builder("NewKey") = "newValue"
    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine("Press Enter to finish.")
    Console.ReadLine()
  End Sub
End Module
注解
连接字符串生成器允许开发人员使用 类的属性和方法以编程方式创建语法正确的连接字符串,并分析和重新生成现有连接字符串。 连接字符串生成器提供与 ODBC 连接允许的已知键/值对对应的强类型属性,开发人员可以为任何其他连接字符串值添加任意键/值对。
需要在应用程序中创建连接字符串的开发人员可以使用 OdbcConnectionStringBuilder 类生成和修改连接字符串。 使用该类还可以轻松管理存储在应用程序配置文件中的连接字符串。 OdbcConnectionStringBuilder 仅对已知键/值对的有限集执行检查。 因此,此类可用于创建无效的连接字符串。 下表列出了特定的已知键及其类中的 OdbcConnectionStringBuilder 相应属性及其默认值。 除了这些特定值之外,开发人员还可以将任何键/值对添加到实例中包含的集合。OdbcConnectionStringBuilder
| 键 | properties | 评论 | 默认值 | 
|---|---|---|---|
| 驱动程序 | Driver | 开发人员在设置 Driver 属性时,不应包括驱动程序名称周围的大括号。 实例 OdbcConnectionStringBuilder 根据需要添加大括号。 | 空字符串 | 
| DSN | Dsn | 空字符串 | 
如果除连接字符串中) 的值以外的Driver任何值 (包含分号 (;) ,则 OdbcConnectionStringBuilder 值在连接字符串中用引号括起来。 为避免经常包含分号的值出现此问题 Driver , OdbcConnectionStringBuilder 类始终用大括号将此值括起来。 ODBC 规范指示包含分号的驱动程序值必须用大括号括起来,此类会为你处理此操作。
属性 Item[] 处理插入恶意代码的尝试。 例如,以下代码使用索引器 (默认 Item[] 属性,在 C#) 中正确转义嵌套键/值对。
Dim builder As _
 New System.Data.Odbc.OdbcConnectionStringBuilder
' Take advantage of the Driver property.
builder.Driver = "SQL Server"
builder("Server") = "MyServer;NewValue=Bad"
Console.WriteLine(builder.ConnectionString)
System.Data.Odbc.OdbcConnectionStringBuilder builder =
  new System.Data.Odbc.OdbcConnectionStringBuilder();
// Take advantage of the Driver property.
builder.Driver = "SQL Server";
builder["Server"] = "MyServer;NewValue=Bad";
Console.WriteLine(builder.ConnectionString);
结果为以下连接字符串,该字符串以安全方式处理无效值:
Driver={SQL Server};Server="MyServer;NewValue=Bad"
构造函数
| OdbcConnectionStringBuilder() | 初始化 OdbcConnectionStringBuilder 类的新实例。 | 
| OdbcConnectionStringBuilder(String) | 初始化 OdbcConnectionStringBuilder 类的新实例。 所提供的连接字符串为实例的内部连接信息提供数据。 | 
属性
| BrowsableConnectionString | 获取或设置一个值,该值指示 ConnectionString 属性是否在 Visual Studio 设计器中可见。(继承自 DbConnectionStringBuilder) | 
| ConnectionString | 获取或设置与 DbConnectionStringBuilder 相关联的连接字符串。(继承自 DbConnectionStringBuilder) | 
| Count | 获取属性 ConnectionString 中包含的键的当前数目。(继承自 DbConnectionStringBuilder) | 
| Driver | 获取或设置与连接关联的 ODBC 驱动程序的名称。 | 
| Dsn | 获取或设置与连接关联的数据源名称 (DSN) 的名称。 | 
| IsFixedSize | 获取一个值,该值指示 DbConnectionStringBuilder 是否具有固定大小。(继承自 DbConnectionStringBuilder) | 
| IsReadOnly | 获取一个值,该值指示 DbConnectionStringBuilder 是否为只读。(继承自 DbConnectionStringBuilder) | 
| Item[String] | 获取或设置与指定的键关联的值。 在 C# 中,此属性为索引器。 | 
| Keys | 获取包含 ICollection 中的键的 OdbcConnectionStringBuilder。 | 
| Values | 获取一个包含 ICollection 中的值的 DbConnectionStringBuilder。(继承自 DbConnectionStringBuilder) | 
方法
显式接口实现
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |