OdbcConnectionStringBuilder 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供一種簡單的方式來建立和管理類別所使用的 OdbcConnection 連接字串內容。
public ref class OdbcConnectionStringBuilder sealed : System::Data::Common::DbConnectionStringBuilder
public sealed class OdbcConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
[System.ComponentModel.TypeConverter(typeof(System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter))]
public sealed class OdbcConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
type OdbcConnectionStringBuilder = class
inherit DbConnectionStringBuilder
[<System.ComponentModel.TypeConverter(typeof(System.Data.Odbc.OdbcConnectionStringBuilder+OdbcConnectionStringBuilderConverter))>]
type OdbcConnectionStringBuilder = class
inherit DbConnectionStringBuilder
Public NotInheritable Class OdbcConnectionStringBuilder
Inherits DbConnectionStringBuilder
- 繼承
- 屬性
範例
以下主控台應用程式為多個 ODBC 資料庫建立連接字串。 首先,範例為 Microsoft Access 資料庫建立一個連接字串。 接著它為 IBM DB2 資料庫建立一個連接字串。 本範例同時解析已存在的連接字串,並展示操作連接字串內容的各種方式。
using System.Data.Odbc;
class Program
{
static void Main()
{
OdbcConnectionStringBuilder builder = new()
{
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");
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";
Console.WriteLine($"protocol = {builder["protocol"].ToString()}");
Console.WriteLine();
// 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);
}
}
Imports System.Data.Odbc
Module Module1
Sub Main()
Dim builder As New OdbcConnectionStringBuilder With {
.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")
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"
Console.WriteLine("protocol = " & builder("protocol").ToString())
Console.WriteLine()
' 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 集合。
| 鑰匙 | 房產 | 註解 | 預設值 |
|---|---|---|---|
| Driver | 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"
建構函式
| 名稱 | Description |
|---|---|
| OdbcConnectionStringBuilder() |
初始化 OdbcConnectionStringBuilder 類別的新執行個體。 |
| OdbcConnectionStringBuilder(String) |
初始化 OdbcConnectionStringBuilder 類別的新執行個體。 所提供的連接字串提供實例內部連線資訊的資料。 |
屬性
| 名稱 | Description |
|---|---|
| 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) |
方法
明確介面實作
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |