Creating a Session
- 3 minutes to read
In simple applications, manually creating session objects is not required because most data store-specific actions can be performed by XPO using defaults (see XpoDefault.Session or Session.DefaultSession). If you are planning to use multiple data stores simultaneously or share the same Data Access Layer between multiple sessions, then sessions are the cornerstone of your application development.
Session allows you to either take control of data store specifics, such as the database name, user name and password, specified in the Session.ConnectionString property to connect to a data store or to use one of the existing connection objects to access a data source (see How to: Connect to a Data Store for details).
All the default settings for the default sessions being created can be customized using the XpoDefault object.
Your options for creating a session are:
- Create a session programmatically.
- Create a standalone session at design time. This option creates a session object on your form or a component whose properties you configure manually. This strategy is useful if you intend to set session properties at runtime or if you simply prefer setting the properties in the Properties window.
The sections below provide additional information on both these options.
Creating a Session at Runtime
The following code sample demonstrates how to create a new session with default settings.
Creating a Session at Design Time
To create the Session object at design time, follow the steps below.
In the Solution Explorer window, click View Designer.
Click View on the toolbar, and then click Toolbox.
From the Developer Express tab of the Toolbox, drag a Session object onto your form or component.
If you want to rename the session object, set its Name property.
If you want to change the visibility level of the session object, set its Modifiers property.
You can specify session-specific options, such as the Session.AutoCreateOption and the Session.LockingOption, at design time. Set the corresponding properties in the Properties window and change the AutoCreateOption.DatabaseAndSchema and the LockingOption.Optimistic property values which are set by default.
You can also designate methods to handle session-specific events on the Events tab in a way you typically do for the other objects.
Customizing a Session
Creating a session doesn't automatically connect it to a data store. To associate a session with a particular data store and override the defaults, you need to specify the connection specifics via the Session.ConnectionString or Session.Connection properties (see Connecting to a Data Store for more information).
For the created standalone session, you can select the session in the designer and then use the Properties window to set its Session.Connection. You can select the OleDbConnection or the SqlConnection objects defined in your form or component. Use OleDbConnection to access an MS Access database and SqlConnection to access an MS SQL Server database.
When a connection is not specified, the session uses its default settings to connect to a data store (see Connecting to a Data Store for details).