Create and set up a MongoDBDataSource object to bind a report to a MongoDB instance in code.
- Specify connection parameters to connect to a MongoDB instance. You can create a MongoDBConnectionParameters
class instance and specify connection parameters individually, such as a hostname and port. If you want to use a connection string, you can create a MongoDBCustomConnectionParameters object
and assign this string to the object's ConnectionString property.
Assign the created connection parameters to the MongoDB data source's ConnectionParameter property. - Create queries to retrieve data from database collections of a MongoDB instance. An object of a MongoDBQuery class corresponds to one query. Set
the query's DatabaseName and CollectionName
properties to the name of a database and collection from which you want to load data. You can also filter the collection's items. For this, assign a filter condition to the query's FilterString property.
Add the created queries to the MongoDB data source's Queries collection.
[!NOTE]
This example uses the following connection string to connect to a MongoDB instance:mongodb://localhost:27017
. The example also specifies queries to load data from the collections of the Northwind database. Before you run this example, specify a connection string and data queries to a configured MongoDB instance.
Files to Review
Documentation
Example Code
C##region usings
using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.MongoDB;
using DevExpress.XtraReports.UI;
#endregion
using System;
using System.Windows.Forms;
namespace Bind_a_Report_to_a_MongoDB_Instance {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
#region bindReportToMongoDBInstanceExample
// Create a MongoDBCustomConnectionParameters object and assign
// a connection string to a MongoDB instance to the object's
// ConnectionString property.
var connectionString = new MongoDBCustomConnectionParameters() {
ConnectionString = "mongodb://localhost:27017"
};
// Specify data queries to the MongoDB instance.
var queryCategories = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Categories",
};
var queryProducts = new MongoDBQuery() {
DatabaseName = "Northwind",
CollectionName = "Products",
};
// Create a MongoDBDataSource object. Assign the created connection
// string to the object's ConnectionParameters property. Add the
// queries to the object's Queries collection.
var mongoDBDataSource = new MongoDBDataSource() {
ConnectionParameters = connectionString,
Queries = { queryCategories, queryProducts }
};
// Create a report. Set the report's DataSource property
// to the created mongoDBDataSource object.
var report = new XtraReport() {
DataSource = mongoDBDataSource,
DataMember = "Categories"
};
// Configure the report layout.
// ...
#endregion
//mongoDBDataSource.RebuildResultSchema();
report.ShowDesigner();
}
}
}