Provides a simple way to create and manage the contents of connection strings used by the OracleConnection class.
Assembly: System.Data.OracleClient (in System.Data.OracleClient.dll)
The connection string builders allow developers to programmatically create syntactically correct connection strings, and to 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 Oracle. The 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 class to build and modify connection strings. The class also makes it easy to manage connection strings stored in an application configuration file.
The performs checks for valid key/value pairs. Therefore, this class cannot be used to create invalid connection strings. Trying to add invalid pairs will throw an exception. The class maintains a fixed collection of synonyms, and when required, can perform the required translation to convert from a synonym to the corresponding well-known key name. For example, when you use the Item property to retrieve a value, you can specify a string that contains any synonym for the key you need. See the Item property for a full list of acceptable synonyms.
The 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. _ OracleClient.OracleConnectionStringBuilder builder("Data Source") = "OracleDemo" builder("Integrated Security") = True builder("User ID") = "Mary;NewValue=Bad" System.Diagnostics.Debug.WriteLine(builder.ConnectionString)
System.Data.OracleClient.OracleConnectionStringBuilder builder = new System.Data.OracleClient.OracleConnectionStringBuilder(); builder["Data Source"] = "OracleDemo"; builder["integrated Security"] = true; builder["User ID"] = "Mary;NewValue=Bad"; System.Diagnostics.Debug.WriteLine(builder.ConnectionString);
The result is the following connection string that handles the invalid value in a safe manner by enclosing the User ID value in quotes:
Data Source=OracleDemo;Integrated Security=True;User ID="Mary;NewValue=Bad"
The following console application builds connection strings for an Oracle database. The code uses an class to create the connection string, and then passes the ConnectionString property of the instance to the constructor of the connection class. The example also parses an existing connection string, and demonstrates various ways of manipulating the connection string's contents.
This example includes a password to demonstrate how 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.
' You may need to set a reference to the System.Data.OracleClient ' assembly before running this example. Imports System.Data.OracleClient Module Module1 Sub Main() ' Create a new OracleConnectionStringBuilder and ' initialize it with a few name/value pairs. Dim builder As New OracleConnectionStringBuilder(GetConnectionString()) ' Note that the input connection string used the ' Server key, but the new connection string uses ' the well-known Data Source key instead. Console.WriteLine(builder.ConnectionString) ' Pass the OracleConnectionStringBuilder an existing ' connection string, and you can retrieve and ' modify any of the elements. builder.ConnectionString = _ "server=OracleDemo;user id=Mary;" & _ "password=*****" ' Now that the connection string has been parsed, ' you can work with individual items. Console.WriteLine(builder.Password) builder.Password = "newPassword" builder.PersistSecurityInfo = True ' You can refer to connection keys using strings, ' as well. When you use this technique (the default ' Item property in Visual Basic, or the indexer in C#), ' you can specify any synonym for the connection string key ' name. builder("Server") = "NewDemo" builder("Load Balance Timeout") = 1000 ' The Item property is the default for the class, ' and setting the Item property adds the value to the ' dictionary, if necessary. builder.Item("Integrated Security") = True Console.WriteLine(builder.ConnectionString) Console.WriteLine("Press Enter to finish.") Console.ReadLine() End Sub Private Function GetConnectionString() As String ' To avoid storing the connection string in your code, ' you can retrieve it from a configuration file. Return "Server=OracleDemo;Integrated Security=True;" & _ "Unicode=True" End Function End Module
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.