Skip to main content
All docs
V24.1

IMongoDBConnectionProviderService.LoadConnection(String) Method

Creates an object that manages a connection to a MongoDB instance.

Namespace: DevExpress.DataAccess.MongoDB

Assembly: DevExpress.DataAccess.v24.1.dll

NuGet Packages: DevExpress.DataAccess, DevExpress.Win.PivotGrid, DevExpress.Win.TreeMap

Declaration

MongoDBDataConnection LoadConnection(
    string name
)

Parameters

Name Type Description
name String

The name of a MongoDB connection string.

Returns

Type Description
MongoDBDataConnection

An object that manages a connection to a MongoDB instance.

Remarks

A MongoDB data source can load a connection string from a project’s configuration file and use this string to connect to a MongoDB instance. Use the data source’s ConnectionName property to specify the connection string name. You can also implement a custom connection service for the MongoDB data source and load a connection string by its name from a different file. Refer to the code example below for more details.

Example

The example below demonstrates how to implement a custom connection service for the MongoDBDataSource class.

using DevExpress.DataAccess.MongoDB;
using DevExpress.XtraPrinting.Native;
// ...
// Create a new class and implement the IMongoDBConnectionProviderService interface.
public class CustomMongoDBConnectionProviderService : IMongoDBConnectionProviderService {
    // Implement the LoadConnection method. 
    public MongoDBDataConnection LoadConnection(string name) {
        // The first argument of the LoadConnection method stores a string
        // assigned to the ConnectionName property of a MongoDBDataSource object.
        // Use this name to load a connection string from a file.
        // ...

        var connectionString = "...";

        // Create and return a MongoDBDataConnection object.
        return new MongoDBDataConnection(connectionString);
    }
}
// ...
void MongoDBCustomConnectionProvideServiceExample() {
    // Create a MongoDBDataSource object and specify its ConnectionName property.
    // The specified name is passed as an argument to the LoadConnection method of
    // the custom connection service.
    var mongoDBDataSource = new MongoDBDataSource() {
        ConnectionName = "...",
        Queries = { /* ... */ }
    };

    // Register the created custom connection service for the MongoDBDataSource object.
    mongoDBDataSource.AddService<IMongoDBConnectionProviderService>(
        new CustomMongoDBConnectionProviderService()
    );

    // Call the Fill method of the MongoDBDataSource object to execute queries
    // and load data from a MongoDB instance.
    mongoDBDataSource.Fill();

    // Use the created object as a data source in your application or component.
    // ...
}
See Also