System.Data.Odbc.OdbcConnection.ConnectionString Property

Gets or sets the string used to open a data source.

Syntax

[System.ComponentModel.RecommendedAsConfigurable(true)]
[System.ComponentModel.Editor("Microsoft.VSDesigner.Data.Odbc.Design.OdbcConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All)]
[System.ComponentModel.DefaultValue("")]
public override string ConnectionString { set; get; }

Value

Documentation for this section has not yet been entered.

Remarks

The OdbcConnection.ConnectionString property is designed to match ODBC connection string format as closely as possible. The OdbcConnection.ConnectionString can be set only when the connection is closed, and as soon as it is set it is passed, unchanged, to the Driver Manager and the underlying driver. Therefore, the syntax for the OdbcConnection.ConnectionString must exactly match what the Driver Manager and underlying driver support.

You can use the OdbcConnection.ConnectionString property to connect to a variety of data sources. This includes an ODBC data source name (DSN). The following example illustrates several possible connection strings.

Example

"Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"

"Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes"

"Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"

"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"

"Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"

"DSN=dsnname"
Note:

The .NET Framework Data Provider for ODBC does not support the Persist Security Info keyword that is supported by other .NET Framework data providers. However, the OdbcConnection.ConnectionString property behaves as if Persist Security Info were set to false. This means that you cannot retrieve the password from the OdbcConnection.ConnectionString property if the connection has been opened. When the OdbcConnection.ConnectionString property is read from an System.Data.Odbc.OdbcConnection object that has been opened, the connection string is returned minus the password. You cannot change this behavior; therefore, if the application requires the password, store it separately before calling OdbcConnection.Open.

Many of the settings specified in the string have corresponding read-only properties (for example, Server=(local), which corresponds to the OdbcConnection.DataSource property). These properties are updated after the connection is opened, except when an error is detected. In this case, none of the properties are updated. System.Data.Odbc.OdbcConnection properties (such as OdbcConnection.Database) return only default settings or those settings specified in the OdbcConnection.ConnectionString.

In the Microsoft .NET Framework version 1.0, validation of the connection string does not occur until an application calls the OdbcConnection.Open method. It is the responsibility of the underlying ODBC driver to validate the connection string. If the connection string contains invalid or unsupported properties, the driver may raise an System.Data.Odbc.OdbcException at run time.

However, in the .NET Framework version 1.1 and later versions, some basic validation of the connection string occurs as soon as you set the OdbcConnection.ConnectionString property. At that time, the data provider verifies that the connection string meets the "keyword=value;..." format, but it does not verify whether keywords or values are valid. The remaining verification is performed by the underlying ODBC driver when the application calls the OdbcConnection.Open method.

An ODBC connection string has the following syntax:

Example

connection-string ::= empty-string[;] | attribute[;] | attribute; connection-string
empty-string ::=
attribute ::= attribute-keyword=attribute-value | DRIVER=[{]attribute-value[}]
attribute-keyword ::= DSN | UID | PWD
 | driver-defined-attribute-keyword
attribute-value ::= character-string
driver-defined-attribute-keyword ::= identifier

where character-string has zero or more characters; identifier has one or more characters; attribute-keyword is not case sensitive; attribute-value can be case sensitive; and the value of the DSN keyword does not consist only of blanks.

Because of connection string and initialization file grammar, keywords and attribute values should be avoided that contain the characters []{}(),;?*=!@ not enclosed with braces. The value of the DSN keyword cannot consist only of blanks and should not contain leading blanks. Because of the grammar of the system information, keywords and data source names cannot contain the backslash (\) character.

Applications do not have to add braces around the attribute value after the Driver keyword unless the attribute contains a semicolon (;), in which case the braces are required. If the attribute value that the driver receives includes braces, the driver should not remove them but they should be part of the returned connection string.

A DSN or connection string value enclosed with braces ({}) that contains any of the characters []{}(),;?*=!@ is passed intact to the driver. However, when you use these characters in a keyword, the Driver Manager returns an error when you work with file DSNs, but passes the connection string to the driver for regular connection strings. Avoid using embedded braces in a keyword value.

The connection string may include any number of driver-defined keywords. Because the DRIVER keyword does not use information from the system, the driver must define enough keywords so that a driver can connect to a data source using only the information in the connection string. The driver defines which keywords are required to connect to the data source.

If any keywords are repeated in the connection string, there is no guarantee which value will be selected.

Note:

If an ODBC driver supports connection strings longer than 1024 characters, you can use a data source name (DSN) to exceed the maximum length limitation.

Requirements

Namespace: System.Data.Odbc
Assembly: System.Data (in System.Data.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0