DbConnectionStringBuilder DbConnectionStringBuilder DbConnectionStringBuilder DbConnectionStringBuilder Class

定義

提供強類型連接字串產生器的基底類別。Provides a base class for strongly typed connection string builders.

public ref class DbConnectionStringBuilder : System::Collections::IDictionary, System::ComponentModel::ICustomTypeDescriptor
public class DbConnectionStringBuilder : System.Collections.IDictionary, System.ComponentModel.ICustomTypeDescriptor
type DbConnectionStringBuilder = class
    interface IDictionary
    interface ICustomTypeDescriptor
    interface ICollection
    interface IEnumerable
Public Class DbConnectionStringBuilder
Implements ICustomTypeDescriptor, IDictionary
繼承
DbConnectionStringBuilderDbConnectionStringBuilderDbConnectionStringBuilderDbConnectionStringBuilder
衍生
實作

範例

下列主控台應用程式會建立兩個連接字串,一個用於 Microsoft Jet 資料庫,一個用於 SQL Server 資料庫。The following console application builds two connection strings, one for a Microsoft Jet database, and one for a SQL Server database. 在每個案例中,程式碼會使用泛型DbConnectionStringBuilder類別來建立連接字串,並傳遞ConnectionString屬性DbConnectionStringBuilder強類型連接類別的建構函式的執行個體。In each case, the code uses a generic DbConnectionStringBuilder class to create the connection string, and then passes the ConnectionString property of the DbConnectionStringBuilder instance to the constructor of the strongly type connection class. 這並非必要;建立的程式碼可能也有個別的強型別的連接字串產生器執行個體。This is not required; the code could also have created individual strongly typed connection string builder instances. 此範例也會剖析現有的連接字串,並示範操作連接字串內容的各種方式。The example also parses an existing connection string, and demonstrates various ways of manipulating the connection string's contents.

static void Main()
{
    DbConnectionStringBuilder builder =
        new DbConnectionStringBuilder();
    builder.ConnectionString = @"Data Source=c:\MyData\MyDb.mdb";
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
    builder.Add("Jet OLEDB:Database Password", "*******");
    builder.Add("Jet OLEDB:System Database",
        @"c:\MyData\Workgroup.mdb");
    // Set up row-level locking.
    builder.Add("Jet OLEDB:Database Locking Mode", 1);

    // The DbConnectionStringBuilder class 
    // is database agnostic, so it's possible to 
    // build any type of connection string using 
    // this class.

    // The ConnectionString property may have been 
    // formatted by the DbConnectionStringBuilder class.
    OleDbConnection oledbConnect = new
        OleDbConnection(builder.ConnectionString);
    Console.WriteLine(oledbConnect.ConnectionString);

    // Use the same DbConnectionStringBuilder to create 
    // a SqlConnection object.
    builder.Clear();
    builder.Add("integrated security", true);
    builder.Add("Initial Catalog", "AdventureWorks");
    builder.Add("Data Source", "(local)");

    SqlConnection sqlConnect = new
        SqlConnection(builder.ConnectionString);
    Console.WriteLine(sqlConnect.ConnectionString);

    // Pass the DbConnectionStringBuilder an existing 
    // connection string, and you can retrieve and
    // modify any of the elements.
    builder.ConnectionString = "server=(local);user id=*******;" +
        "password=*******;initial catalog=AdventureWorks";
    builder["Server"] = ".";
    builder.Remove("User ID");

    // Note that calling Remove on a nonexistent item doesn't
    // throw an exception.
    builder.Remove("BadItem");

    // Setting the indexer adds the value if 
    // necessary.
    builder["Integrated Security"] = true;
    builder.Remove("password");
    builder["User ID"] = "Hello";
    Console.WriteLine(builder.ConnectionString);

    Console.WriteLine("Press Enter to finish.");
    Console.ReadLine();
}
Sub Main()
    Dim builder As New DbConnectionStringBuilder()
    builder.ConnectionString = "Data Source=c:\MyData\MyDb.mdb"
    builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
    builder.Add("Jet OLEDB:Database Password", "*******")
    builder.Add("Jet OLEDB:System Database", _
        "c:\MyData\Workgroup.mdb")

    ' Set up row-level locking.
    builder.Add("Jet OLEDB:Database Locking Mode", 1)

    ' Note that the DbConnectionStringBuilder class 
    ' is database agnostic, and it's possible to 
    ' build any type of connection string using 
    ' this class.
    ' Notice that the ConnectionString property may have been 
    ' formatted by the DbConnectionStringBuilder class.

    Dim oledbConnect As New _
        OleDbConnection(builder.ConnectionString)
    Console.WriteLine(oledbConnect.ConnectionString)

    ' Use the same DbConnectionStringBuilder to create 
    ' a SqlConnection object.
    builder.Clear()
    builder.Add("integrated security", True)
    builder.Add("Initial Catalog", "AdventureWorks")
    builder.Add("Data Source", "(local)")

    Dim sqlConnect As New SqlConnection(builder.ConnectionString)
    Console.WriteLine(sqlConnect.ConnectionString)

    ' Pass the DbConnectionStringBuilder an existing 
    ' connection string, and you can retrieve and
    ' modify any of the elements.
    builder.ConnectionString = _
        "server=(local);user id=*******;" & _
        "password=*******;initial catalog=AdventureWorks"
    builder.Item("Server") = "."
    builder.Remove("User ID")

    ' Note that calling Remove on a nonexistent item doesn't
    ' throw an exception.
    builder.Remove("BadItem")

    ' The Item property is the default for the class, 
    ' and setting the Item property adds the value if 
    ' necessary.
    builder("Integrated Security") = True
    builder.Remove("password")
    builder.Item("User ID") = "Hello"
    Console.WriteLine(builder.ConnectionString)

    Console.WriteLine("Press Enter to finish.")
    Console.ReadLine()
End Sub

備註

DbConnectionStringBuilder類別提供的強型別的連接字串產生器的基底類別 (SqlConnectionStringBuilderOleDbConnectionStringBuilder,依此類推) 衍生。The DbConnectionStringBuilder class provides the base class from which the strongly typed connection string builders (SqlConnectionStringBuilder, OleDbConnectionStringBuilder, and so on) derive. 連接字串產生器可讓開發人員以程式設計方式建立語法正確的連接字串,以及剖析和重建現有的連接字串。The connection string builders let developers programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings.

DbConnectionStringBuilder已定義資料庫無關的方式。The DbConnectionStringBuilder has been defined in a database-agnostic manner. 因為加入System.Data.Common命名空間中,開發人員需要針對它們進行程式設計才能建立可以處理的任意資料庫的連接字串的基底類別。Because of the addition of the System.Data.Common namespace, developers require a base class against which they can program in order to build connection strings that can work against an arbitrary database. 因此,DbConnectionStringBuilder類別可讓使用者在指派任意索引鍵/值組,並將產生的連接字串傳遞給強型別提供者。Therefore, the DbConnectionStringBuilder class lets users assign arbitrary key/value pairs and pass the resulting connection string to a strongly typed provider. .NET Framework 的一部分的所有資料提供者都提供強型別的的類別繼承自DbConnectionStringBuilder: SqlConnectionStringBuilderOracleConnectionStringBuilderOdbcConnectionStringBuilder,和OleDbConnectionStringBuilderAll the data providers that are included as part of the .NET Framework provide a strongly typed class that inherits from DbConnectionStringBuilder: SqlConnectionStringBuilder, OracleConnectionStringBuilder, OdbcConnectionStringBuilder, and OleDbConnectionStringBuilder.

開發人員可以建置、 指派及編輯任何任意的提供者的連接字串。The developer can build, assign, and edit connection strings for any arbitrary provider. 對於支援特定的索引鍵/值組的提供者,連接字串產生器提供強型別的屬性對應到已知的配對。For providers that support specific key/value pairs, the connection string builder provides strongly typed properties corresponding to the known pairs. 為了支援需要能夠支援未知的值提供者,開發人員也可以提供任意索引鍵/值組。In order to support providers that require the ability to support unknown values, developers can also supply arbitrary key/value pairs.

DbConnectionStringBuilder 類別會實作 ICustomTypeDescriptor 介面。The DbConnectionStringBuilder class implements the ICustomTypeDescriptor interface. 這表示,此類別適用於 Visual Studio 設計工具在設計階段。This means that the class works with Visual Studio designers at design time. 當開發人員會使用設計工具,建置強型別資料集和 Visual Studio 內的強型別的連接時,強型別的連接字串產生器類別會顯示它的型別相關聯的屬性,並也會有可以的轉換器對應至已知索引鍵的通用值。When developers use the designer to build strongly typed DataSets and strongly typed connections within Visual Studio, the strongly typed connection string builder class will display the properties associated with its type and will also have converters that can map common values for known keys.

開發人員若要建立的連接字串,隨應用程式可以使用DbConnectionStringBuilder類別或其中一個來建立和修改連接字串,其強型別衍生項目。Developers needing to create connection strings as part of applications can use the DbConnectionStringBuilder class or one of its strongly typed derivatives to build and modify connection strings. DbConnectionStringBuilder類別也可以輕鬆管理儲存在應用程式組態檔中的連接字串。The DbConnectionStringBuilder class also makes it easy to manage connection strings stored in an application configuration file.

開發人員可以建立連接字串使用強型別的連接字串產生器類別,或者也可以使用DbConnectionStringBuilder類別。Developers can create connection strings using either a strongly typed connection string builder class, or they can use the DbConnectionStringBuilder class. DbConnectionStringBuilder不會執行檢查有效的索引鍵/值組。The DbConnectionStringBuilder performs no checks for valid key/value pairs. 因此,可能可以使用這個類別建立無效的連接字串。Therefore, it is possible using this class to create invalid connection strings. SqlConnectionStringBuilder支援僅索引鍵/值組所支援的 SQL Server,則嘗試加入無效組會擲回例外狀況。The SqlConnectionStringBuilder supports only key/value pairs that are supported by SQL Server; trying to add invalid pairs will throw an exception.

這兩個Add方法和Item[String]屬性控制代碼嘗試插入惡意的項目。Both the Add method and Item[String] property handle tries to insert malicious entries. 例如,下列程式碼正確地逸出巢狀索引鍵/值組:For example, the following code correctly escapes the nested key/value pair:

Dim builder As New System.Data.Common.DbConnectionStringBuilder  
builder("Data Source") = "(local)"  
builder("integrated sSecurity") = True  
builder("Initial Catalog") = "AdventureWorks;NewValue=Bad"  
System.Data.Common.DbConnectionStringBuilder builder =   
    new System.Data.Common.DbConnectionStringBuilder();  
builder["Data Source"] = "(local)";  
builder["integrated Security"] = true;  
builder["Initial Catalog"] = "AdventureWorks;NewValue=Bad";  

結果是以安全方式處理無效值的下列連接字串:The result is the following connection string that handles the invalid value in a safe manner:

data source=(local);integrated security=True;  
initial catalog="AdventureWorks;NewValue=Bad"  

建構函式

DbConnectionStringBuilder() DbConnectionStringBuilder() DbConnectionStringBuilder() DbConnectionStringBuilder()

初始化 DbConnectionStringBuilder 類別的新執行個體。Initializes a new instance of the DbConnectionStringBuilder class.

DbConnectionStringBuilder(Boolean) DbConnectionStringBuilder(Boolean) DbConnectionStringBuilder(Boolean) DbConnectionStringBuilder(Boolean)

選擇性地使用 ODBC 規則將值以引號括住,進而初始化 DbConnectionStringBuilder 類別的新執行個體。Initializes a new instance of the DbConnectionStringBuilder class, optionally using ODBC rules for quoting values.

屬性

BrowsableConnectionString BrowsableConnectionString BrowsableConnectionString BrowsableConnectionString

取得或設定值,指出 ConnectionString 屬性是否會顯示在 Visual Studio 設計工具中。Gets or sets a value that indicates whether the ConnectionString property is visible in Visual Studio designers.

ConnectionString ConnectionString ConnectionString ConnectionString

取得或設定與 DbConnectionStringBuilder 相關聯的連接字串。Gets or sets the connection string associated with the DbConnectionStringBuilder.

Count Count Count Count

取得 ConnectionString 屬性內所包含的索引鍵目前數目。Gets the current number of keys that are contained within the ConnectionString property.

IsFixedSize IsFixedSize IsFixedSize IsFixedSize

取得值,指出 DbConnectionStringBuilder 是否有固定的大小。Gets a value that indicates whether the DbConnectionStringBuilder has a fixed size.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

取得值,這個值表示 DbConnectionStringBuilder 是否為唯讀。Gets a value that indicates whether the DbConnectionStringBuilder is read-only.

Item[String] Item[String] Item[String] Item[String]

取得或設定與指定之索引鍵相關聯的值。Gets or sets the value associated with the specified key.

Keys Keys Keys Keys

取得 ICollection 中包含索引鍵的 DbConnectionStringBuilderGets an ICollection that contains the keys in the DbConnectionStringBuilder.

Values Values Values Values

取得包含 ICollection 中之值的 DbConnectionStringBuilderGets an ICollection that contains the values in the DbConnectionStringBuilder.

方法

Add(String, Object) Add(String, Object) Add(String, Object) Add(String, Object)

將有指定索引鍵和數值項目加入 DbConnectionStringBuilder 中。Adds an entry with the specified key and value into the DbConnectionStringBuilder.

AppendKeyValuePair(StringBuilder, String, String) AppendKeyValuePair(StringBuilder, String, String) AppendKeyValuePair(StringBuilder, String, String) AppendKeyValuePair(StringBuilder, String, String)

提供有效且安全的方法,將索引鍵和值附加至現有的 StringBuilder 物件。Provides an efficient and safe way to append a key and value to an existing StringBuilder object.

AppendKeyValuePair(StringBuilder, String, String, Boolean) AppendKeyValuePair(StringBuilder, String, String, Boolean) AppendKeyValuePair(StringBuilder, String, String, Boolean) AppendKeyValuePair(StringBuilder, String, String, Boolean)

提供有效且安全的方法,將索引鍵和值附加至現有的 StringBuilder 物件。Provides an efficient and safe way to append a key and value to an existing StringBuilder object.

Clear() Clear() Clear() Clear()

清除 DbConnectionStringBuilder 執行個體的內容。Clears the contents of the DbConnectionStringBuilder instance.

ClearPropertyDescriptors() ClearPropertyDescriptors() ClearPropertyDescriptors() ClearPropertyDescriptors()

清除在相關聯 DbConnectionStringBuilder 上的 PropertyDescriptor 物件集合。Clears the collection of PropertyDescriptor objects on the associated DbConnectionStringBuilder.

ContainsKey(String) ContainsKey(String) ContainsKey(String) ContainsKey(String)

判斷 DbConnectionStringBuilder 是否包含特定索引鍵。Determines whether the DbConnectionStringBuilder contains a specific key.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
EquivalentTo(DbConnectionStringBuilder) EquivalentTo(DbConnectionStringBuilder) EquivalentTo(DbConnectionStringBuilder) EquivalentTo(DbConnectionStringBuilder)

比較在這個 DbConnectionStringBuilder 物件中的連接資訊和提供之物件中的連接資訊。Compares the connection information in this DbConnectionStringBuilder object with the connection information in the supplied object.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetProperties(Hashtable) GetProperties(Hashtable) GetProperties(Hashtable) GetProperties(Hashtable)

以這個 DbConnectionStringBuilder 之所有屬性的相關資訊填滿提供的 HashtableFills a supplied Hashtable with information about all the properties of this DbConnectionStringBuilder.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(String) Remove(String) Remove(String) Remove(String)

DbConnectionStringBuilder 執行個體移除具有指定之索引鍵的項目。Removes the entry with the specified key from the DbConnectionStringBuilder instance.

ShouldSerialize(String) ShouldSerialize(String) ShouldSerialize(String) ShouldSerialize(String)

指出這個 DbConnectionStringBuilder 執行個體中,是否存在指定的索引鍵。Indicates whether the specified key exists in this DbConnectionStringBuilder instance.

ToString() ToString() ToString() ToString()

傳回與這個 DbConnectionStringBuilder 相關聯的連接字串。Returns the connection string associated with this DbConnectionStringBuilder.

TryGetValue(String, Object) TryGetValue(String, Object) TryGetValue(String, Object) TryGetValue(String, Object)

從這個 DbConnectionStringBuilder 擷取對應到所提供之索引鍵的值。Retrieves a value corresponding to the supplied key from this DbConnectionStringBuilder.

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

從特定的 ICollection 索引開始,將 Array 的項目複製到 ArrayCopies the elements of the ICollection to an Array, starting at a particular Array index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

取得值,這個值表示對 ICollection 的存取是否同步 (安全執行緒)。Gets a value indicating whether access to the ICollection is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

取得可用以同步存取 ICollection 的物件。Gets an object that can be used to synchronize access to the ICollection.

ICustomTypeDescriptor.GetAttributes() ICustomTypeDescriptor.GetAttributes() ICustomTypeDescriptor.GetAttributes() ICustomTypeDescriptor.GetAttributes()

傳回元件之這個執行個體的自訂屬性集合。Returns a collection of custom attributes for this instance of a component.

ICustomTypeDescriptor.GetClassName() ICustomTypeDescriptor.GetClassName() ICustomTypeDescriptor.GetClassName() ICustomTypeDescriptor.GetClassName()

傳回這個元件執行個體的類別名稱。Returns the class name of this instance of a component.

ICustomTypeDescriptor.GetComponentName() ICustomTypeDescriptor.GetComponentName() ICustomTypeDescriptor.GetComponentName() ICustomTypeDescriptor.GetComponentName()

傳回這個元件執行個體的名稱。Returns the name of this instance of a component.

ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor.GetConverter() ICustomTypeDescriptor.GetConverter()

傳回這個元件執行個體的類型轉換子。Returns a type converter for this instance of a component.

ICustomTypeDescriptor.GetDefaultEvent() ICustomTypeDescriptor.GetDefaultEvent() ICustomTypeDescriptor.GetDefaultEvent() ICustomTypeDescriptor.GetDefaultEvent()

傳回這個元件執行個體的預設事件。Returns the default event for this instance of a component.

ICustomTypeDescriptor.GetDefaultProperty() ICustomTypeDescriptor.GetDefaultProperty() ICustomTypeDescriptor.GetDefaultProperty() ICustomTypeDescriptor.GetDefaultProperty()

傳回這個元件執行個體的屬性。Returns the default property for this instance of a component.

ICustomTypeDescriptor.GetEditor(Type) ICustomTypeDescriptor.GetEditor(Type) ICustomTypeDescriptor.GetEditor(Type) ICustomTypeDescriptor.GetEditor(Type)

傳回元件的這個執行個體之指定類型編輯器。Returns an editor of the specified type for this instance of a component.

ICustomTypeDescriptor.GetEvents() ICustomTypeDescriptor.GetEvents() ICustomTypeDescriptor.GetEvents() ICustomTypeDescriptor.GetEvents()

傳回這個元件執行個體的事件。Returns the events for this instance of a component.

ICustomTypeDescriptor.GetEvents(Attribute[]) ICustomTypeDescriptor.GetEvents(Attribute[]) ICustomTypeDescriptor.GetEvents(Attribute[]) ICustomTypeDescriptor.GetEvents(Attribute[])

使用指定屬性陣列做為篩選條件傳回元件之執行個體的事件。Returns the events for this instance of a component using the specified attribute array as a filter.

ICustomTypeDescriptor.GetProperties() ICustomTypeDescriptor.GetProperties() ICustomTypeDescriptor.GetProperties() ICustomTypeDescriptor.GetProperties()

傳回元件之這個執行個體的屬性。Returns the properties for this instance of a component.

ICustomTypeDescriptor.GetProperties(Attribute[]) ICustomTypeDescriptor.GetProperties(Attribute[]) ICustomTypeDescriptor.GetProperties(Attribute[]) ICustomTypeDescriptor.GetProperties(Attribute[])

使用屬性陣列做為篩選條件,傳回這個元件執行個體的屬性。Returns the properties for this instance of a component using the attribute array as a filter.

ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor) ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor) ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor) ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)

傳回物件,包含指定的屬性描述項所描述的屬性。Returns an object that contains the property described by the specified property descriptor.

IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object)

將隨附有索引鍵和值的項目加入至 IDictionary 物件。Adds an element with the provided key and value to the IDictionary object.

IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object)

判斷 IDictionary 物件是否包含具有指定索引鍵的項目。Determines whether the IDictionary object contains an element with the specified key.

IDictionary.GetEnumerator() IDictionary.GetEnumerator() IDictionary.GetEnumerator() IDictionary.GetEnumerator()

傳回 IDictionary 物件的 IDictionaryEnumerator 物件。Returns an IDictionaryEnumerator object for the IDictionary object.

IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize
IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly
IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object]

取得或設定具有指定索引鍵的項目。Gets or sets the element with the specified key.

IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object)

IDictionary 物件中移除具有指定索引鍵的項目。Removes the element with the specified key from the IDictionary object.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

傳回逐一查看集合的列舉值。Returns an enumerator that iterates through a collection.

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

另請參閱