OleDbConnectionStringBuilder 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为创建和管理由 OleDbConnection 类使用的连接字符串的内容提供了一种简单方法。
public ref class OleDbConnectionStringBuilder sealed : System::Data::Common::DbConnectionStringBuilder[System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder+OleDbConnectionStringBuilderConverter))]
public sealed class OleDbConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder[<System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder+OleDbConnectionStringBuilderConverter))>]
type OleDbConnectionStringBuilder = class
    inherit DbConnectionStringBuilderPublic NotInheritable Class OleDbConnectionStringBuilder
Inherits DbConnectionStringBuilder- 继承
- 属性
示例
以下控制台应用程序为多个 OLE DB 数据库生成连接字符串。 首先,该示例为 Microsoft Access 数据库创建连接字符串,然后为 IBM DB2 数据库创建连接字符串。 该示例还分析现有连接字符串,并演示了操作连接字符串内容的各种方法。
注意
该示例包括一个密码以演示 OleDbConnectionStringBuilder 如何使用连接字符串。 在您的应用程序中,建议使用 Windows 身份验证。 如果必须使用密码,请不要在你的应用程序中包括硬编码的密码。
using System.Data.OleDb;
class Program
{
    static void Main(string[] args)
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.ConnectionString = @"Data Source=C:\Sample.mdb";
        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
        builder.Add("Jet OLEDB:Database Password", "MyPassword!");
        builder.Add("Jet OLEDB:System Database", @"C:\Workgroup.mdb");
        // Set up row-level locking.
        builder.Add("Jet OLEDB:Database Locking Mode", 1);
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();
        // Clear current values and reset known keys to their
        // default values.
        builder.Clear();
        // Pass the OleDbConnectionStringBuilder an existing
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString =
            "Provider=DB2OLEDB;Network Transport Library=TCPIP;" +
            "Network Address=192.168.0.12;Initial Catalog=DbAdventures;" +
            "Package Collection=SamplePackage;Default Schema=SampleSchema;";
        Console.WriteLine("Network Address = " + builder["Network Address"].ToString());
        Console.WriteLine();
        // Modify existing items.
        builder["Package Collection"] = "NewPackage";
        builder["Default Schema"] = "NewSchema";
        // Call the Remove method to remove items from
        // the collection of key/value pairs.
        builder.Remove("User ID");
        // 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 value, if
        // necessary.
        builder["User ID"] = "SampleUser";
        builder["Password"] = "SamplePassword";
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}
Imports System.Data.OleDb
Imports System.Collections
Module Module1
  Sub Main()
    Dim builder As New OleDbConnectionStringBuilder()
    builder.ConnectionString = "Data Source=C:\Sample.mdb"
    ' Call the Add method to explicitly add key/value
    ' pairs to the internal collection.
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
    builder.Add("Jet OLEDB:Database Password", "MyPassword!")
    builder.Add("Jet OLEDB:System Database", "C:\Workgroup.mdb")
    ' Set up row-level locking.
    builder.Add("Jet OLEDB:Database Locking Mode", 1)
    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine()
    ' Clear current values and reset known keys to their
    ' default values.
    builder.Clear()
    ' Pass the OleDbConnectionStringBuilder an existing
    ' connection string, and you can retrieve and
    ' modify any of the elements.
    builder.ConnectionString = "..."
    Console.WriteLine("Network Address = " & builder("Network Address").ToString())
    Console.WriteLine()
    ' Modify existing items.
    builder("Package Collection") = "NewPackage"
    builder("Default Schema") = "NewSchema"
    ' Call the Remove method to remove items from
    ' the collection of key/value pairs.
    builder.Remove("User ID")
    ' 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("User ID") = "SampleUser"
    builder("Password") = "SamplePassword"
    Console.WriteLine(builder.ConnectionString)
    Console.WriteLine("Press Enter to finish.")
    Console.ReadLine()
  End Sub
End Module
注解
通过连接字符串生成器,开发人员可以使用该类的属性和方法,以编程方式创建语法正确的连接字符串以及分析和重新生成现有的连接字符串。 连接字符串生成器提供与 OLE DB 连接允许的已知键/值对对应的强类型属性,开发人员可以为任何其他连接字符串值添加任意键/值对。 OleDbConnectionStringBuilder 类实现 ICustomTypeDescriptor 接口。 这意味着 类在设计时与 Visual Studio .NET 设计器一起使用。 当开发人员使用设计器在 Visual Studio .NET 中生成强类型数据集和强类型连接时,强类型连接字符串生成器类将显示与其类型关联的属性,并且还将具有可映射已知键的通用值的转换器。
需要在应用程序中创建连接字符串的开发人员可以使用 OleDbConnectionStringBuilder 类生成和修改连接字符串。 使用该类还可以轻松管理存储在应用程序配置文件中的连接字符串。 仅 OleDbConnectionStringBuilder 检查已知键/值对的有限集。 因此,此类可用于创建无效的连接字符串。 下表列出了 类中的 OleDbConnectionStringBuilder 已知键及其相应属性及其默认值。 除了这些特定值之外,开发人员还可以将任何键/值对添加到 实例中包含的集合:OleDbConnectionStringBuilder
| 键 | properties | 默认值 | 
|---|---|---|
| 文件名 | FileName | "" | 
| 提供程序 | Provider | "" | 
| 数据源 | DataSource | "" | 
| 持久性安全信息 | PersistSecurityInfo | 错误 | 
| OLE DB 服务 | OleDbServices | -13 | 
属性 Item[] 处理插入恶意条目的尝试。 例如,以下代码在 C#) 中使用索引器 (默认 Item[] 属性正确转义嵌套键/值对:
Dim builder As _
    New System.Data.OleDb.OleDbConnectionStringBuilder
builder("Provider") = "Microsoft.Jet.OLEDB.4.0"
builder("Data Source") = "C:\Sample.mdb"
builder("User Id") = "Admin;NewValue=Bad"
System.Data.OleDb.OleDbConnectionStringBuilder builder =
    new System.Data.OleDb.OleDbConnectionStringBuilder();
builder["Provider"] = "Microsoft.Jet.OLEDB.4.0";
builder["Data Source"] = "C:\\Sample.mdb";
builder["User Id"] = "Admin;NewValue=Bad";
结果为以下连接字符串,该字符串以安全方式处理无效值:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Sample.mdb;User ID="Admin;NewValue=Bad"
构造函数
| OleDbConnectionStringBuilder() | 初始化 OleDbConnectionStringBuilder 类的新实例。 | 
| OleDbConnectionStringBuilder(String) | 初始化 OleDbConnectionStringBuilder 类的新实例。 所提供的连接字符串为实例的内部连接信息提供数据。 | 
属性
| BrowsableConnectionString | 获取或设置一个值,该值指示 ConnectionString 属性是否在 Visual Studio 设计器中可见。(继承自 DbConnectionStringBuilder) | 
| ConnectionString | 获取或设置与 DbConnectionStringBuilder 相关联的连接字符串。(继承自 DbConnectionStringBuilder) | 
| Count | 获取属性 ConnectionString 中包含的键的当前数目。(继承自 DbConnectionStringBuilder) | 
| DataSource | 获取或设置要连接到的数据源的名称。 | 
| FileName | 获取或设置用于连接到数据源的通用数据链接 (UDL) 文件的名称。 | 
| IsFixedSize | 获取一个值,该值指示 DbConnectionStringBuilder 是否具有固定大小。(继承自 DbConnectionStringBuilder) | 
| IsReadOnly | 获取一个值,该值指示 DbConnectionStringBuilder 是否为只读。(继承自 DbConnectionStringBuilder) | 
| Item[String] | 获取或设置与指定的键关联的值。 在 C# 中,此属性为索引器。 | 
| Keys | 获取包含 ICollection 中的键的 OleDbConnectionStringBuilder。 | 
| OleDbServices | 获取或设置要为连接字符串内的 OLE DB Services 键传递的值。 | 
| PersistSecurityInfo | 获取或设置一个布尔值,该值指示如果连接是打开的或者一直处于打开状态,那么安全敏感信息(如密码)是否作为连接的一部分返回。 | 
| Provider | 获取或设置一个字符串,该字符串包含与内部连接字符串关联的数据提供程序的名称。 | 
| Values | 获取一个包含 ICollection 中的值的 DbConnectionStringBuilder。(继承自 DbConnectionStringBuilder) | 
方法
显式接口实现
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |