Provides a simple way to create and manage the contents of connection strings used by the OleDbConnection class.
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 DbConnectionStringBuilder
Public NotInheritable Class OleDbConnectionStringBuilder
Inherits DbConnectionStringBuilder
The following console application builds connection strings for several OLE DB databases. First, the example creates a connection string for a Microsoft Access database, and then creates a connection string for an IBM DB2 database. The example also parses an existing connection string, and demonstrates various ways of manipulating the connection string's contents.
Note
This example includes a password to demonstrate how OleDbConnectionStringBuilder works with connection strings. In your applications, we recommend that you use Windows Authentication. If you must use a password, do not include a hard-coded password in your application.
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
The connection string builder lets developers programmatically create syntactically correct connection strings, and parse and rebuild existing connection strings, using properties and methods of the class. The connection string builder provides strongly typed properties corresponding to the known key/value pairs allowed by OLE DB connections, and developers can add arbitrary key/value pairs for any other connection string values. The OleDbConnectionStringBuilder class implements the ICustomTypeDescriptor interface. This means that the class works with Visual Studio .NET designers at design time. When developers use the designer to build strongly typed DataSets and strongly typed connections within Visual Studio .NET, 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.
Developers needing to create connection strings as part of applications can use the OleDbConnectionStringBuilder class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file. The OleDbConnectionStringBuilder performs checks only for the limited set of known key/value pairs. Therefore, this class can be used to create invalid connection strings. The following table lists the known keys and their corresponding properties within the OleDbConnectionStringBuilder class, and their default values. Besides these specific values, developers can add any key/value pairs to the collection that is contained within the OleDbConnectionStringBuilder instance:
The Item[] property handles attempts to insert malicious entries. For example, the following code, using the default Item[] property (the indexer, in C#) correctly escapes the nested key/value pair:
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";
The result is the following connection string that handles the invalid value in a safe manner:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Sample.mdb;User ID="Admin;NewValue=Bad"
Constructors Properties Methods Explicit Interface Implementations Extension Methods See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4