Skip to main content
A newer version of this page is available. .

Session.ConnectionString Property

Gets or sets the string which is used to open a database.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v18.2.dll

Declaration

public string ConnectionString { get; set; }

Property Value

Type Description
String

A String value which specifies the parameters needed to establish the initial connection.

Remarks

A session can establish a connection only if a string assigned to the ConnectionString property contains the XpoProvider parameter, specifying the provider type used for the connection. If this parameter is not specified and XPO cannot determine the provider type using other parameters, a data access layer cannot be created, and as a result, the CannotFindAppropriateConnectionProviderException is thrown. To get a fully qualified connection string for the ConnectionString property, use the GetConnectionString method of a corresponding connection provider. For the InMemoryDataStore, use the InMemoryDataStore.GetConnectionString method instead.

When a session is connected to a data store, the ConnectionString property returns only the connection string that is required to create the underlying IDbConnection, without the XpoProvider parameter entry. So, to create a session based on connection settings of another session, you cannot simply copy its ConnectionString property value. Instead, create the session’s connection as shown below.


using DevExpress.Xpo;
using DevExpress.Xpo.DB;

public static Session CloneSession(Session source) {
    Session result = new Session();
    result.AutoCreateOption = AutoCreateOption.DatabaseAndSchema;
    result.Connection = 
        (IDbConnection)Activator.CreateInstance(source.DataLayer.Connection.GetType());
    result.Connection.ConnectionString = source.ConnectionString;
    return result;
}

If a session was instantiated by a data access layer specified either via the corresponding Session constructor or the XpoDefault.DataLayer property, the ConnectionString property returns an empty string.

Note

The ConnectionString property can be modified only when the connection is closed. Otherwise, the CannotChangePropertyWhenSessionIsConnectedException is thrown.

The following code snippets (auto-collected from DevExpress Examples) contain references to the ConnectionString property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also