Skip to main content
.NET Framework 4.5.2+

How to: Connect to a SQL Server

  • 2 minutes to read

To make XPO use a SQL Server, you should do the following:

  • Create a SQL Server connection string.
  • Connect XPO to a database.

After the XpoDefault.ConnectionString property has been set, the default session and sessions that are manually created with default parameters will use the specified connection string. The following examples demonstrate two different ways to specify a connection string.

Supplying a SQL Server connection string

You can manually specify a SQL Server connection string. The current version of XPO determines the connection provider type from the given connection string: if it contains the “Initial Catalog” string, it is recognized as a SQL Server connection.

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

static void Main() {
    XpoDefault.Session.ConnectionString =
         @"data source=localhost\SQLEXPRESS;integrated security=true;initial catalog=Customers;";
    Application.Run(new MainForm());
}

Using the GetConnectionString Function

Additionally, the GetConnectionString overloaded function of the MSSqlConnectionProvider class can be used to create the default Data Access Layer (DAL).

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

static void Main() {
  string sqlConn =
     MSSqlConnectionProvider.GetConnectionString(@"localhost\SQLEXPRESS", "Customers");
  XpoDefault.DataLayer = XpoDefault.GetDataLayer(sqlConn, AutoCreateOption.DatabaseAndSchema); 
  Application.Run(new MainForm());
}

Note

Using the MSSqlConnectionProvider, you can also connect to SQL Azure databases. To learn more, refer to How to: Connect to SQL Azure.

See Also