corelibraries-devexpress-dot-dataaccess-dot-mongodb-dot-imongodbconnectionproviderservice-dot-loadconnection-x28-system-dot-string-x29.md
Creates an object that manages a connection to a MongoDB instance.
Namespace : DevExpress.DataAccess.MongoDB
Assembly : DevExpress.DataAccess.v25.2.dll
NuGet Package : DevExpress.DataAccess
MongoDBDataConnection LoadConnection(
string name
)
Function LoadConnection(
name As String
) As MongoDBDataConnection
| Name | Type | Description |
|---|---|---|
| name | String |
The name of a MongoDB connection string.
|
| Type | Description |
|---|---|
| MongoDBDataConnection |
An object that manages a connection to a MongoDB instance.
|
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.
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.
// ...
}
Imports DevExpress.DataAccess.MongoDB
Imports DevExpress.XtraPrinting.Native
' ...
' Create a new class and implement the IMongoDBConnectionProviderService interface.
Public Class CustomMongoDBConnectionProviderService
Implements IMongoDBConnectionProviderService
' Implement the LoadConnection method.
Public Function LoadConnection(name As String) As MongoDBDataConnection Implements IMongoDBConnectionProviderService.LoadConnection
' 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.
' ...
Dim connectionString = "..."
' Create and return a MongoDBDataConnection object.
Return New MongoDBDataConnection(connectionString)
End Function
End Class
' ...
Private Sub 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.
Dim mongoDBDataSource = New MongoDBDataSource() With {.ConnectionName = "..."}
' Add queries to the data source's Queries collection.
' ...
' Register the created custom connection service for the MongoDBDataSource object.
mongoDBDataSource.AddService(Of 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.
' ...
End Sub
See Also
IMongoDBConnectionProviderService Interface