.NET Standard 2.0+
.NET Framework 4.5.2+
.NET Standard 2.0+

Session.ConnectionString Property

Gets or sets a connection string.

Namespace: DevExpress.Xpo

Assembly: DevExpress.Xpo.v20.2.dll

Declaration

public string ConnectionString { get; set; }
Public Property ConnectionString As String

Property Value

Type Description
String

Data store connection parameters.

Remarks

If a connection string is a reference to a Data Store Service (URL), Session connects to a Data Store Service client.

Session throws CannotFindAppropriateConnectionProviderException in the following situations:

A connection string does not contain the XpoProvider attribute and XPO cannot infer the provider name from other attributes.

To get a fully qualified connection string, use the GetConnectionString method of a corresponding connection provider.

A connection string references a Data Store Service and a target platform is .NET Standard or .NET Core.

To connect a .NET Standard/.NET Core project to a Data Store Service, create and configure DataStoreClientAsync or CachedDataStoreClient. For additional information, see WCF Client in .NET Core.

Do not assign the ConnectionString property value to another Session, because the property may return an empty string or the ConnectionString property value of the underlying IDbConnection object. The underlying connection string does not contain the XpoProvider attribute. To connect Session to the same database, create a new IDbConnection object (see the code example 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;
}
NOTE

To avoid a CannotChangePropertyWhenSessionIsConnectedException, do not change the ConnectionString property value when the IsConnected property returns true.

Example

Pass a connection string to the ConnectionString property for the Session.DefaultSession, or create your own Session object. To obtain a connection string which is relevant to a data store, use the GetConnectionString method of the corresponding XPO data store adapter (see Database Systems Supported by XPO for more information).

The following code demonstrates how to connect to an MS Access database using the default session.

using DevExpress.Xpo
using DevExpress.Xpo.DB;

// If the default session is connected, disconnect it.
if(Session.DefaultSession.IsConnected) {
    Session.DefaultSession.Disconnect();
}

// Retrieve the MS Access database-specific connection string.
Session.DefaultSession.ConnectionString =
AccessConnectionProvider.GetConnectionString("sample.mdb","user", "pwd");
// The default data access layer is created and connected to the database 
// using the retrieved connection string.
Session.DefaultSession.Connect();

You can also specify the existing OleDbConnection or the SqlConnection objects as the connection objects to a data store used by the Session or Session.DefaultSession.

See Also