Sideway
output.to from Sideway
Draft for Information Only

Content

System.Data.OleDB
OleDbConnectionStringBuilder Class
 Definition
  In this article
 Examples
 Remarks
 Constructors
 Properties
 Methods
 Explicit Interface Implementations
 Extension Methods
 Applies to
   .NET Core
   .NET Framework
   .NET Platform Extensions
   Xamarin.Mac
 See also
 Examples
 Source/Reference

System.Data.OleDB

The System.Data.OleDb namespace is the.NET Framework Data Provider for OLE DB.

OleDbConnectionStringBuilder Class

Definition

Namespace:
System.Data.OleDb
Assemblies:
System.Data.dll, System.Data.OleDb.dll

Provides a simple way to create and manage the contents of connection strings used by the OleDbConnection class.

In this article

  1. Definition
  2. Examples
  3. Remarks
  4. Constructors
  5. Properties
  6. Methods
  7. Explicit Interface Implementations
  8. Extension Methods
  9. Applies to
  10. See also
C#
[System.ComponentModel.TypeConverter(typeof(System.Data.OleDb.OleDbConnectionStringBuilder/OleDbConnectionStringBuilderConverter))]
public sealed class OleDbConnectionStringBuilder : System.Data.Common.DbConnectionStringBuilder
Inheritance
Object DbConnectionStringBuilder OleDbConnectionStringBuilder
Attributes
TypeConverterAttribute

Examples

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.

C#
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();
    }
}

Remarks

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:

Key Property Default value
File Name FileName ""
Provider Provider ""
Data Source DataSource ""
Persist Security Info PersistSecurityInfo False
OLE DB Services OleDbServices -13

The Item[String] property handles attempts to insert malicious entries. For example, the following code, using the default Item[String] property (the indexer, in C#) correctly escapes the nested key/value pair:

C#
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

OleDbConnectionStringBuilder()

Initializes a new instance of the OleDbConnectionStringBuilder class.

OleDbConnectionStringBuilder(String)

Initializes a new instance of the OleDbConnectionStringBuilder class. The provided connection string provides the data for the instance's internal connection information.

Properties

BrowsableConnectionString

Gets or sets a value that indicates whether the ConnectionString property is visible in Visual Studio designers.

(Inherited from DbConnectionStringBuilder)
ConnectionString

Gets or sets the connection string associated with the DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
Count

Gets the current number of keys that are contained within the ConnectionString property.

(Inherited from DbConnectionStringBuilder)
DataSource

Gets or sets the name of the data source to connect to.

FileName

Gets or sets the name of the Universal Data Link (UDL) file for connecting to the data source.

ICollection.IsSynchronized

Gets a value indicating whether access to the ICollection is synchronized (thread safe).

(Inherited from DbConnectionStringBuilder)
ICollection.SyncRoot

Gets an object that can be used to synchronize access to the ICollection.

(Inherited from DbConnectionStringBuilder)
IDictionary.Item[Object]

Gets or sets the element with the specified key.

(Inherited from DbConnectionStringBuilder)
IsFixedSize

Gets a value that indicates whether the DbConnectionStringBuilder has a fixed size.

(Inherited from DbConnectionStringBuilder)
IsReadOnly

Gets a value that indicates whether the DbConnectionStringBuilder is read-only.

(Inherited from DbConnectionStringBuilder)
Item[String]

Gets or sets the value associated with the specified key. In C#, this property is the indexer.

Keys

Gets an ICollection that contains the keys in the OleDbConnectionStringBuilder.

OleDbServices

Gets or sets the value to be passed for the OLE DB Services key within the connection string.

PersistSecurityInfo

Gets or sets a Boolean value that indicates whether security-sensitive information, such as the password, is returned as part of the connection if the connection is open or has ever been in an open state.

Provider

Gets or sets a string that contains the name of the data provider associated with the internal connection string.

Values

Gets an ICollection that contains the values in the DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)

Methods

Add(String, Object)

Adds an entry with the specified key and value into the DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
Clear()

Clears the contents of the OleDbConnectionStringBuilder instance.

ClearPropertyDescriptors()

Clears the collection of PropertyDescriptor objects on the associated DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
ContainsKey(String)

Determines whether the OleDbConnectionStringBuilder contains a specific key.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
EquivalentTo(DbConnectionStringBuilder)

Compares the connection information in this DbConnectionStringBuilder object with the connection information in the supplied object.

(Inherited from DbConnectionStringBuilder)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetProperties(Hashtable)

Fills a supplied Hashtable with information about all the properties of this DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Remove(String)

Removes the entry with the specified key from the OleDbConnectionStringBuilder instance.

ShouldSerialize(String)

Indicates whether the specified key exists in this DbConnectionStringBuilder instance.

(Inherited from DbConnectionStringBuilder)
ToString()

Returns the connection string associated with this DbConnectionStringBuilder.

(Inherited from DbConnectionStringBuilder)
TryGetValue(String, Object)

Retrieves a value corresponding to the supplied key from the OleDbConnectionStringBuilder instance.

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32)

Copies the elements of the ICollection to an Array, starting at a particular Array index.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetAttributes()

Returns a collection of custom attributes for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetClassName()

Returns the class name of this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetComponentName()

Returns the name of this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetConverter()

Returns a type converter for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetDefaultEvent()

Returns the default event for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetDefaultProperty()

Returns the default property for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEditor(Type)

Returns an editor of the specified type for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEvents()

Returns the events for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetEvents(Attribute[])

Returns the events for this instance of a component using the specified attribute array as a filter.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetProperties()

Returns the properties for this instance of a component.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetProperties(Attribute[])

Returns the properties for this instance of a component using the attribute array as a filter.

(Inherited from DbConnectionStringBuilder)
ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor)

Returns an object that contains the property described by the specified property descriptor.

(Inherited from DbConnectionStringBuilder)
IDictionary.Add(Object, Object)

Adds an element with the provided key and value to the IDictionary object.

(Inherited from DbConnectionStringBuilder)
IDictionary.Contains(Object)

Determines whether the IDictionary object contains an element with the specified key.

(Inherited from DbConnectionStringBuilder)
IDictionary.GetEnumerator()

Returns an IDictionaryEnumerator object for the IDictionary object.

(Inherited from DbConnectionStringBuilder)
IDictionary.Remove(Object)

Removes the element with the specified key from the IDictionary object.

(Inherited from DbConnectionStringBuilder)
IEnumerable.GetEnumerator()

Returns an enumerator that iterates through a collection.

(Inherited from DbConnectionStringBuilder)

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

.NET Core

3.0 Preview 8

.NET Framework

4.8 4.7.2 4.7.1 4.7 4.6.2 4.6.1 4.6 4.5.2 4.5.1 4.5 4.0 3.5 3.0 2.0

.NET Platform Extensions

3.0 Preview 8

Xamarin.Mac

3.0

See also

 

Examples

Examples of OleDbConnectionStringBuilder Class
ASP.NET Code Input:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
       <title>Sample Page</title>
       <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
       <script runat="server" >
           Sub Page_Load()
               Dim xstr As String
               Dim xconn As System.Data.OleDb.OleDbConnection = new System.Data.OleDb.OleDbConnection()
               Dim xconnstrb As System.Data.OleDb.OleDbConnectionStringBuilder = new System.Data.OleDb.OleDbConnectionStringBuilder()
               xstr = xstr + "Declare OleDbConnectionStringBuilder: xconnstrb<br />"
               xconnstrb.ConnectionString = "Data Source=T:\test.mdb"
               xstr = xstr + "->Assign xconnstrb.connectionstring: " + xconnstrb.ConnectionString + "<br />"
               xconnstrb.Provider = "Microsoft.ACE.OLEDB.12.0"
               xstr = xstr + "->Assign xconnstrb.provider: " + xconnstrb.ConnectionString + "<br />"
               xconnstrb.Add("User Id", "admin")
               xstr = xstr + "->Add xconnstrb.Add(user id): " + xconnstrb.ConnectionString + "<br />"
               xconnstrb.Add("Password", "admin")
               xstr = xstr + "->Add xconnstrb.Add(password): " + xconnstrb.ConnectionString + "<br />"
               xconnstrb.Remove("Password")
               xstr = xstr + "->xconnstrb.Remove Password: " + xconnstrb.ConnectionString + "<br />"
               xconnstrb.Item("Password")=""
               xstr = xstr + "->xconnstrb.item(Password): " + xconnstrb.ConnectionString + "<br />"
               
               xconn.ConnectionString = xconnstrb.ConnectionString
               xstr = xstr + "Assign xconnstrb.ConnectionString to xconn.ConnectionString  <br />"
               xconnstrb.clear()
               xstr = xstr + "xconnstrb is cleared<br />"
               xstr = xstr + "OleDbConnectionStringBuilder: " + xconnstrb.ConnectionString + "<br />"
               xconn.Open()
               xstr = xstr + "Database test.mdb connection is opened successfully.<br />"
               xconn.Close()
               xstr = xstr + "Database test.mdb connection is closed successfully.<br />"
               xconn.Dispose()
               xstr = xstr + "Database test.mdb connection is disposed successfully."
               lbl01.Text = xstr
           End Sub
       </script>
    </head>
    <body>
<%Response.Write("<p>Results on "& Request.ServerVariables("SERVER_SOFTWARE") & " .net: " & System.Environment.Version.ToString & " " & ScriptEngine & " Version " & ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion & "</p>")%>
       <% Response.Write ("<h1>This is a Sample Page of OleDbConnectionStringBuilder Class</h1>") %>
       <p>
           <%-- Set on Page_Load --%>
           <asp:Label id="lbl01" runat="server" />
       </p>
    </body>
</html>
HTML Web Page Embedded Output:

Source/Reference


©sideway

ID: 201100004 Last Updated: 11/4/2020 Revision: 0 Ref:

close

References

  1. Active Server Pages,  , http://msdn.microsoft.com/en-us/library/aa286483.aspx
  2. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929%28v=vs.90%29.aspx
  3. ASP Best Practices,  , http://technet.microsoft.com/en-us/library/cc939157.aspx
  4. ASP Built-in Objects,  , http://msdn.microsoft.com/en-us/library/ie/ms524716(v=vs.90).aspx
  5. Response Object,  , http://msdn.microsoft.com/en-us/library/ms525405(v=vs.90).aspx
  6. Request Object,  , http://msdn.microsoft.com/en-us/library/ms524948(v=vs.90).aspx
  7. Server Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525541(v=vs.90).aspx
  8. Application Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525360(v=vs.90).aspx
  9. Session Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms524319(8v=vs.90).aspx
  10. ASPError Object,  , http://msdn.microsoft.com/en-us/library/ms524942(v=vs.90).aspx
  11. ObjectContext Object (IIS),  , http://msdn.microsoft.com/en-us/library/ms525667(v=vs.90).aspx
  12. Debugging Global.asa Files,  , http://msdn.microsoft.com/en-us/library/aa291249(v=vs.71).aspx
  13. How to: Debug Global.asa files,  , http://msdn.microsoft.com/en-us/library/ms241868(v=vs.80).aspx
  14. Calling COM Components from ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524620(v=VS.90).aspx
  15. IIS ASP Scripting Reference,  , http://msdn.microsoft.com/en-us/library/ms524664(v=vs.90).aspx
  16. ASP Keywords,  , http://msdn.microsoft.com/en-us/library/ms524672(v=vs.90).aspx
  17. Creating Simple ASP Pages,  , http://msdn.microsoft.com/en-us/library/ms524741(v=vs.90).aspx
  18. Including Files in ASP Applications,  , http://msdn.microsoft.com/en-us/library/ms524876(v=vs.90).aspx
  19. ASP Overview,  , http://msdn.microsoft.com/en-us/library/ms524929(v=vs.90).aspx
  20. FileSystemObject Object,  , http://msdn.microsoft.com/en-us/library/z9ty6h50(v=vs.84).aspx
  21. http://msdn.microsoft.com/en-us/library/windows/desktop/ms675944(v=vs.85).aspx,  , ADO Object Model
  22. ADO Fundamentals,  , http://msdn.microsoft.com/en-us/library/windows/desktop/ms680928(v=vs.85).aspx
close

Latest Updated LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019