Skip to main content

IDashboardStorage Interface

Custom storage of dashboards.

Namespace: DevExpress.DashboardWeb

Assembly: DevExpress.Dashboard.v23.2.Web.dll

NuGet Package: DevExpress.Web.Dashboard.Common

Declaration

public interface IDashboardStorage

Remarks

Use the DashboardConfigurator.SetDashboardStorage method to create storage for dashboards. You can use predefined dashboard storages (DashboardFileStorage or DashboardInMemoryStorage) or implement the IDashboardStorage/IEditableDashboardStorage interface to use your logic to manage dashboards. Review the following topic for more information: Manage Multi-Tenancy.

Note

The DashboardInMemoryStorage is used if you do not create the dashboard storage.

Example

The following example shows how to create a custom dashboard storage and to load and save dashboards to a database. In this example, the IEditableDashboardStorage interface is implemented to create a custom dashboard storage.

View Example

using DevExpress.DashboardWeb;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml.Linq;

namespace SaveDashboardDB {
    public class DataBaseEditaleDashboardStorage : IEditableDashboardStorage {
        private string connectionString;

        public DataBaseEditaleDashboardStorage(string connectionString) {
            this.connectionString = connectionString;
        }

        public string AddDashboard(XDocument document, string dashboardName) {
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                connection.Open();
                MemoryStream stream = new MemoryStream();
                document.Save(stream);
                stream.Position = 0;

                SqlCommand InsertCommand = new SqlCommand(
                    "INSERT INTO Dashboards (Dashboard, Caption) " +
                    "output INSERTED.ID " +
                    "VALUES (@Dashboard, @Caption)");
                InsertCommand.Parameters.Add("Caption", SqlDbType.NVarChar).Value = dashboardName;
                InsertCommand.Parameters.Add("Dashboard", SqlDbType.VarBinary).Value = stream.ToArray();
                InsertCommand.Connection = connection;
                string ID = InsertCommand.ExecuteScalar().ToString();
                connection.Close();
                return ID;
            }
        }

        public XDocument LoadDashboard(string dashboardID) {
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                connection.Open();
                SqlCommand GetCommand = new SqlCommand("SELECT  Dashboard FROM Dashboards WHERE ID=@ID");
                GetCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID);
                GetCommand.Connection = connection;
                SqlDataReader reader = GetCommand.ExecuteReader();
                reader.Read();
                byte[] data = reader.GetValue(0) as byte[];
                MemoryStream stream = new MemoryStream(data);
                connection.Close();
                return XDocument.Load(stream);
            }
        }

        public IEnumerable<DashboardInfo> GetAvailableDashboardsInfo() {
            List<DashboardInfo> list = new List<DashboardInfo>();
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                connection.Open();
                SqlCommand GetCommand = new SqlCommand("SELECT ID, Caption FROM Dashboards");
                GetCommand.Connection = connection;
                SqlDataReader reader = GetCommand.ExecuteReader();
                while (reader.Read()) {
                    string ID = reader.GetInt32(0).ToString();
                    string Caption = reader.GetString(1);
                    list.Add(new DashboardInfo() { ID = ID, Name = Caption });
                }
                connection.Close();
            }
            return list;
        }

        public void SaveDashboard(string dashboardID, XDocument document) {
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                connection.Open();
                MemoryStream stream = new MemoryStream();
                document.Save(stream);
                stream.Position = 0;

                SqlCommand InsertCommand = new SqlCommand(
                    "UPDATE Dashboards Set Dashboard = @Dashboard " +
                    "WHERE ID = @ID");
                InsertCommand.Parameters.Add("ID", SqlDbType.Int).Value = Convert.ToInt32(dashboardID);
                InsertCommand.Parameters.Add("Dashboard", SqlDbType.VarBinary).Value = stream.ToArray();
                InsertCommand.Connection = connection;
                InsertCommand.ExecuteNonQuery();

                connection.Close();
            }
        }
    }
}

The following examples are based on different platforms and show how to create a custom dashboard storage to load and save dashboards to a database:

View Example: ASP.NET Core View Example: ASP.NET MVC View Example: ASP.NET Web Forms

See Also