Back to Devexpress

MongoDBDataSourceBase.ConnectionName Property

corelibraries-devexpress-dot-dataaccess-dot-mongodb-dot-mongodbdatasourcebase.md

latest4.5 KB
Original Source

MongoDBDataSourceBase.ConnectionName Property

Stores the name of a MongoDB connection string specified in a project’s configuration file.

Namespace : DevExpress.DataAccess.MongoDB

Assembly : DevExpress.DataAccess.v25.2.dll

NuGet Package : DevExpress.DataAccess

Declaration

csharp
[LocalizableCategory(DataAccessStringId.PropertyGridConnectionCategoryName)]
public string ConnectionName { get; set; }
vb
<LocalizableCategory(DataAccessStringId.PropertyGridConnectionCategoryName)>
Public Property ConnectionName As String

Property Value

TypeDescription
String

The name of a MongoDB connection string specified in a project’s configuration file.

|

Remarks

A MongoDB data source requires a configured connection to a MongoDB instance. To use a connection string from a project’s configuration file to connect to the MongoDB instance, set the ConnectionName property to the name of this connection string. You can also implement a custom connection service and load a connection string by its name from a different file. Refer to the LoadConnection(String) method description for more details.

Example

The example below demonstrates how to use the MongoDBDataSource class to bind an application/component to a MongoDB instance. The example assigns the name of a connection string from a project’s configuration file to the class ConnectionName property to use this string to connect to a MongoDB instance and uses the MongoDBQuery class to specify data query to the Products collection of the Northwind database.

csharp
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
// ...
// Specify the name of a connection string from
// a project's configuration file.
var connectionName = "MongoDBConnection";

// Specify queries to database collections.
var queryProducts = new MongoDBQuery() {
    DatabaseName = "Northwind",
    CollectionName = "Products",
};

// Create a MongoDBDataSource object. Assign the name of the
// connection string to the object's ConnectionName property.
// Add the queries to the object's Queries collection.
var mongoDBDataSource = new MongoDBDataSource() {
    ConnectionName = connectionName,
    Queries = { queryProducts }
};

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

// Use the created object as a data source in your application or component.
//...
vb
Imports DevExpress.DataAccess.ConnectionParameters
Imports DevExpress.DataAccess.MongoDB
' ...
' Specify the name of a connection string from
' a project's configuration file.
Dim connectionName = "MongoDBConnection"

' Specify queries to database collections.
Dim queryProducts = New MongoDBQuery() With {
    .DatabaseName = "Northwind",
    .CollectionName = "Products"
}

' Create a MongoDBDataSource object. Assign the name of the
' connection string to the object's ConnectionName property.
' Add the queries to the object's Queries collection.
Dim mongoDBDataSource = New MongoDBDataSource() With {
    .ConnectionName = connectionName
}

mongoDBDataSource.Queries.Add(queryProducts)

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

' Use the created object as a data source in your application or component.
'...

See Also

ConnectionParameters

MongoDBDataSourceBase Class

MongoDBDataSourceBase Members

DevExpress.DataAccess.MongoDB Namespace