Provides a base class for strongly typed connection string builders.
public ref class DbConnectionStringBuilder : System::Collections::IDictionary
public ref class DbConnectionStringBuilder : System::Collections::IDictionary, System::ComponentModel::ICustomTypeDescriptor
public class DbConnectionStringBuilder : System.Collections.IDictionary
public class DbConnectionStringBuilder : System.Collections.IDictionary, System.ComponentModel.ICustomTypeDescriptor
type DbConnectionStringBuilder = class
interface ICollection
interface IEnumerable
interface IDictionary
type DbConnectionStringBuilder = class
interface ICollection
interface IEnumerable
interface IDictionary
interface ICustomTypeDescriptor
type DbConnectionStringBuilder = class
interface IDictionary
interface ICollection
interface IEnumerable
interface ICustomTypeDescriptor
Public Class DbConnectionStringBuilder
Implements IDictionary
Public Class DbConnectionStringBuilder
Implements ICustomTypeDescriptor, IDictionary
The following console application builds two connection strings, one for a Microsoft Jet database, and one for a SQL Server database. 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 typed 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.
DbConnectionStringBuilder builder =
new DbConnectionStringBuilder();
builder.ConnectionString = @"Data Source=c:\MyData\MyDb.mdb";
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
// 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 might 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);initial catalog=AdventureWorks";
builder["Server"] = ".";
// Setting the indexer adds the value, if necessary.
builder["Integrated Security"] = true;
Console.WriteLine(builder.ConnectionString);
Sub Main()
Dim builder As New DbConnectionStringBuilder()
builder.ConnectionString = "Data Source=c:\MyData\MyDb.mdb"
builder.Add("Provider", "Microsoft.Jet.Oledb.4.0")
' 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);initial catalog=AdventureWorks"
builder.Item("Server") = "."
' The Item property is the default for the class,
' and setting the Item property adds the value if
' necessary.
builder("Integrated Security") = True
Console.WriteLine(builder.ConnectionString)
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
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.
The DbConnectionStringBuilder has been defined in a database-agnostic manner. 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. Therefore, the DbConnectionStringBuilder class lets users assign arbitrary key/value pairs and pass the resulting connection string to a strongly typed provider. All the data providers that are included as part of .NET offer a strongly typed class that inherits from DbConnectionStringBuilder: SqlConnectionStringBuilder, OracleConnectionStringBuilder, OdbcConnectionStringBuilder, and OleDbConnectionStringBuilder.
You 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. For providers that require the ability to support unknown values, you can also supply arbitrary key/value pairs.
The DbConnectionStringBuilder class implements the ICustomTypeDescriptor interface. This means that the class works with Visual Studio designers at design time. When developers use the designer to build strongly typed DataSets and strongly typed connections within Visual Studio, the strongly typed connection string builder class displays the properties associated with its type and also has converters that can map common values for known keys.
If you need to create connection strings as part of applications, use the DbConnectionStringBuilder class or one of its strongly typed derivatives to build and modify connection strings. The DbConnectionStringBuilder class also makes it easy to manage connection strings stored in an application configuration file.
You can create connection strings using either a strongly typed connection string builder class or the DbConnectionStringBuilder class. The DbConnectionStringBuilder performs no checks for valid key/value pairs. Therefore, it's possible to create invalid connection strings when using this class. The SqlConnectionStringBuilder supports only key/value pairs that are supported by SQL Server; trying to add invalid pairs will throw an exception.
Both the Add method and Item[] property handle cases where a bad actor 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"
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