When a user adds a new data source, the list of available connections in the Data Source Wizard is populated with connections defined in the application configuration file. This example demonstrates how to use a custom storage (a connectons.xml
) file to load and save Data Source Wizard connections. For this, implement the IConnectionStorageService and register it in the Report Designer component.
The CustomSqlDataConnection
class (the SqlDataConnection descendant) in this example holds the names displayed in the connection list.
Important: In this example, the connection string in the XML file is not encrypted and exposes sensitive information (a username and a password). You should protect the file storage to prevent unauthorized access in a real-world application.
Files to Review
- CustomConnectionStorageService.cs (VB: CustomConnectionStorageService.vb)
- CustomSqlDataConnection.cs (VB: CustomSqlDataConnection.vb)
- Form1.cs (VB: Form1.vb)
Documentation
- Customize Data Connections in the Data Source Wizard (WinForms)
- Data Sources in Web End-User Report Designer (ASP.NET Core)
Example Code
C#using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Wizard.Model;
using DevExpress.DataAccess.Wizard.Native;
using DevExpress.DataAccess.Wizard.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
class CustomConnectionStorageService : IConnectionStorageService
{
public bool CanSaveConnection => true;
bool IConnectionStorageService.Contains(string connectionName)
{
return IncludeApplicationConnections ?
DefaultStorage.Contains(connectionName) ||
GetConnectionsFromXml().Any(c => string.Equals(c.Name, connectionName)) :
GetConnectionsFromXml().Any(c => string.Equals(c.Name, connectionName));
}
IEnumerable<SqlDataConnection> IConnectionStorageService.GetConnections()
{
if (!File.Exists(FileName))
{
try
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement(xmlRootName);
doc.AppendChild(root);
doc.Save(FileName);
SqlDataConnection defaultConnection = new CustomSqlDataConnection("Default Connection",
new MsSqlConnectionParameters("localhost", "Northwind", null, null, MsSqlAuthorizationType.Windows));
SaveConnection(defaultConnection.Name, defaultConnection, true);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Cannot create '{0}' file because of exception:{1}{1}{2}", FileName,
Environment.NewLine, ex.Message));
return null;
}
}
return IncludeApplicationConnections ?
DefaultStorage.GetConnections().Union(GetConnectionsFromXml()) :
GetConnectionsFromXml();
}
public void SaveConnection(string connectionName, IDataConnection connection, bool saveCredentials)
{
try
{
XmlDocument doc = new XmlDocument();
XmlElement root = null;
if (File.Exists(FileName))
{
doc.Load(FileName);
root = doc.DocumentElement;
if (root != null)
{
if (root.Name != xmlRootName)
{
MessageBox.Show(string.Format("Document element is '{0}', '{1}' expected",
root.Name, xmlRootName));
return;
}
if (root.SelectSingleNode(string.Format("Connection[Name = '{0}']", connectionName)) != null)
return;
}
}
if (root == null)
{
root = doc.CreateElement(xmlRootName);
doc.AppendChild(root);
}
XmlElement nameElement = doc.CreateElement("Name");
nameElement.AppendChild(doc.CreateTextNode(connectionName));
XmlElement connectionStringElement = doc.CreateElement("ConnectionString");
connectionStringElement.AppendChild(doc.CreateTextNode(connection.CreateConnectionString(!saveCredentials)));
XmlElement connectionElement = doc.CreateElement("Connection");
connectionElement.AppendChild(nameElement);
connectionElement.AppendChild(connectionStringElement);
root.AppendChild(connectionElement);
doc.Save(FileName);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("Cannot save connection to '{0}' because of exception:{1}{1}{2}", FileName,
Environment.NewLine, ex.Message));
}
}
const string xmlRootName = "Connections";
public string FileName { get; set; } = "connections.xml";
public bool IncludeApplicationConnections { get; set; } = false;
protected ConnectionStorageService DefaultStorage { get; } = new ConnectionStorageService();
protected IEnumerable<SqlDataConnection> GetConnectionsFromXml()
{
var result = new List<SqlDataConnection>();
try
{
XmlDocument doc = new XmlDocument();
doc.Load(FileName);
foreach (XmlNode node in doc.SelectNodes("Connections/Connection[Name][ConnectionString]"))
result.Add(new CustomSqlDataConnection(node["Name"].InnerText,
new CustomStringConnectionParameters(node["ConnectionString"].InnerText)));
return result;
}
catch (Exception ex)
{
MessageBox.Show(
string.Format("Cannot get connections from '{0}' because of exception:{1}{1}{2}",
FileName, Environment.NewLine, ex.Message));
return new SqlDataConnection[0];
}
}
}
C#using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Native;
using DevExpress.DataAccess.Sql;
class CustomSqlDataConnection : SqlDataConnection, INamedItem
{
public CustomSqlDataConnection(string name, DataConnectionParametersBase connectionParameters)
: base(name, connectionParameters) { }
string INamedItem.Name
{
get => Name + " (Custom)";
set
{
Name = value;
}
}
}
C#using DevExpress.DataAccess.Wizard.Services;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace T119350
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
CustomConnectionStorageService connectionStorageService;
public Form1()
{
InitializeComponent();
connectionStorageService = new CustomConnectionStorageService()
{
FileName = "connections.xml",
IncludeApplicationConnections = true
};
ReplaceService(this.reportDesigner1,
typeof(IConnectionStorageService), connectionStorageService);
this.reportDesigner1.DesignPanelLoaded += DesignMdiControllerOnDesignPanelLoaded;
}
private void ReplaceService(IServiceContainer container,
Type serviceType,
object serviceInstance)
{
if (container.GetService(serviceType) != null)
container.RemoveService(serviceType);
container.AddService(serviceType, serviceInstance);
}
private void DesignMdiControllerOnDesignPanelLoaded(object sender,
DesignerLoadedEventArgs e)
{
ReplaceService(e.DesignerHost, typeof(IConnectionStorageService), connectionStorageService);
}
private void Form1_Load(object sender, EventArgs e)
{
XtraReport report = new XtraReport();
this.reportDesigner1.OpenReport(report);
}
}
}