The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

How to: Connect to a Data Store

  • 7 min to read

To make XPO use your database server, you should add assemblies which correspond both to the database system, and an XPO data store adapter to your project's References (see Database Systems Supported by XPO for details), and then connect to a database (data store).

In terms of XPO, a connection to a data store is an attribute of a data access layer (DAL for short). Each Session object is associated with a DAL which is used to access data in a data store.

A connection to a data store is established:

 

Before establishing a connection, relevant connection settings should be specified using one of the following options.

The connection is alive while a session is active, i.e. until the Session.Disconnect method is explicitly called. This method is implicitly called when a Session object is disposed of.

The sections below provide additional information on these options.

Using Data Access Layer Objects

Like session objects, the DAL objects can be created using either a connection string or an existing connection object (see XpoDefault.GetDataLayer for more information). After the default DAL has been set using the XpoDefault.DataLayer property, the default session object and all the newly created session objects will use the specified DAL to access the data store.

The following sample demonstrates how to create the default DAL using the connection string, and save persistent objects using the Session.DefaultSession which uses the created default DAL.


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

// Represents the Person class which contains personal information.
public class Person: XPObject {
    // ...
}

static void Main() {
    // Retrieve the MS Access database-specific connection string.
    string connectionString =
     AccessConnectionProvider.GetConnectionString("sample.mdb", "user", "pwd");

    // Create the default DAL which is used by the default Session and
    // newly created objects 
    // to access a data store.
    // If the database does not exist, XPO creates it.
    XpoDefault.DataLayer = XpoDefault.GetDataLayer(connectionString,
        AutoCreateOption.DatabaseAndSchema);

    // An instance of the Person object is created and saved using the default session 
    // which uses the connected default DAL.
    Person person = new Person();
    person.Save();

    // Create a new Session object which uses the default DAL
    // (as specified by the XpoDefault).
    Session session = new Session();
    Application.Run(new MainForm());
}

Using the XpoDefault.ConnectionString Properties

After the XpoDefault.ConnectionString property has been set, the default sessions and newly created sessions will use the specified connection string when they are connected to a data store, as shown in the following code example.


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

// Retrieve the MS Access database-specific connection string.
XpoDefault.ConnectionString =
    AccessConnectionProvider.GetConnectionString("sample.mdb", "user", "pwd");
// If the default session is connected, disconnect it.
// XpoDefault.Session and Session.DefaultSession can be used interchangeably.
if(XpoDefault.Session.IsConnected) {
    XpoDefault.Session.Disconnect();
}

// The default data access layer is created and connected to the database
// using the connection string.
XpoDefault.Session.Connect();

Using the ConnectionString and Connection Properties

Connection strings provide essential data store-specific information to obtain access to a data store via the session objects. You can specify the ConnectionString property for the Session.DefaultSession, or create your own Session object (see Creating a Session to obtain general tips on doing this). 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();

Alternatively, you can specify the existing OleDbConnection or the SqlConnection objects as the connection objects to a data store used by the Session or Session.DefaultSession.

Using the Default Settings for Sessions

When connection settings are not specified for a custom session, the session uses the default settings to connect to a data store. By default, XPO uses the MS Access OLEDB provider, assumes that the database name is the same as the application name, and it resides in the application folder. If the data store does not exist, XPO creates it. You can customize the default settings, such as a connection string, via the XpoDefault object.

In the code snippet below the Person object is saved using a custom session which uses the pre-initialized default settings to connect to a data store (since neither XpoDefault.ConnectionString nor XpoDefault.DataLayer is not specified).


// Represents the Person class which contains personal information.
public class Person: XPObject {
    // A session-specific constructor is required when using custom sessions.
    public Person(Session session): base(session) { }
    // ...
}

static void Main() {
    // A new session is created with the pre-initialized default settings.
    Session session = new Session();
    // An instance of the Person object is created and saved into a data store
    // using the created session.
    Person person = new Person(session);
    person.Save();

    Application.Run(new MainForm());
}
See Also