xpo-devexpress-dot-xpo-dot-session.md
Do not build or assign a connection string from untrusted input (such as user-entered text). Always validate connection parameters to mitigate CWE-15-related security risks.
Gets or sets a connection string.
Namespace : DevExpress.Xpo
Assembly : DevExpress.Xpo.v25.2.dll
NuGet Package : DevExpress.Xpo
public string ConnectionString { get; set; }
Public Property ConnectionString As String
| Type | Description |
|---|---|
| String |
Data store connection parameters.
|
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 Core.
|
To connect a .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;
}
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
Public Shared Function CloneSession(ByVal source As Session) As Session
Dim result As New Session()
result.AutoCreateOption = AutoCreateOption.DatabaseAndSchema
result.Connection = _
CType(Activator.CreateInstance(source.DataLayer.Connection.GetType()), IDbConnection)
result.Connection.ConnectionString = source.ConnectionString
Return result
End Function
Note
To avoid a CannotChangePropertyWhenSessionIsConnectedException, do not change the ConnectionString property value when the IsConnected property returns true.
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 Microsoft 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 Microsoft 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();
Imports DevExpress.Xpo
Imports DevExpress.Xpo.DB
' If the default session is connected, disconnect it.
If Session.DefaultSession.IsConnected Then
Session.DefaultSession.Disconnect()
End If
' Retrieve the Microsoft 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.
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.
winforms-scheduler-bind-to-xpo/CS/XPO_XtraScheduler_Simple_Example/Form1.cs#L11
session1.ConnectionString = "XpoProvider=InMemoryDataStore";
winforms-scheduler-bind-xpo-multi-resource-appointments/CS/XPO_MultiResource_Example/Form1.cs#L13
session1.ConnectionString = "XpoProvider=InMemoryDataStore";
winforms-scheduler-bind-to-xpo/VB/XPO_XtraScheduler_Simple_Example/Form1.vb#L11
session1.ConnectionString = "XpoProvider=InMemoryDataStore"
winforms-scheduler-bind-xpo-multi-resource-appointments/VB/XPO_MultiResource_Example/Form1.vb#L15
session1.ConnectionString = "XpoProvider=InMemoryDataStore"
See Also
AccessConnectionProvider.GetConnectionString
AccessConnectionProviderMultiUserThreadSafe.GetConnectionString
InMemoryDataStore.GetConnectionString