Assuming you have node and link tables in MySQL that contain following fields:
node: node_id, label, shape
link: link_id, origin_id, destination_id
and you are using the .NET entity framework, you can read diagrams from the database and save to the database with code like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MindFusion.Diagramming;
using MindFusion.Diagramming.Layout;
namespace MySqlTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
diagramView1.AllowInplaceEdit = true;
var nodeBounds = new RectangleF(0, 0, 30, 20);
var diagDB = new DiagramEntities();
// create diagram nodes corresponding to database records
foreach (Node nodeData in diagDB.Nodes)
{
var node = diagram.Factory.CreateShapeNode(nodeBounds);
node.Text = nodeData.label;
node.Shape = Shape.FromId(nodeData.shape);
node.Id = nodeData.node_id;
}
// create diagram links corresponding to database records
foreach (Link linkData in diagDB.Links)
{
var origin = diagram.FindNodeById(linkData.origin_id);
var dest = diagram.FindNodeById(linkData.destination_id);
var link = diagram.Factory.CreateDiagramLink(origin, dest);
link.Text = linkData.label;
link.Id = linkData.link_id;
}
// arrange the diagram
new LayeredLayout().Arrange(diagram);
}
private void btnSave_Click(object sender, EventArgs e)
{
var diagDB = new DiagramEntities();
// create or update node records
foreach (ShapeNode node in diagram.Nodes)
{
if (node.Id == null)
{
// create new record
var nodeData = new Node();
nodeData.label = node.Text;
nodeData.shape = node.Shape.Id;
diagDB.Nodes.AddObject(nodeData);
diagDB.SaveChanges();
node.Id = nodeData.node_id;
}
else
{
// update existing record
var nodeData = (from row in diagDB.Nodes
where row.node_id == (int)node.Id
select row).FirstOrDefault();
nodeData.label = node.Text;
nodeData.shape = node.Shape.Id;
diagDB.SaveChanges();
}
}
// create or update link records
foreach (DiagramLink link in diagram.Links)
{
if (link.Id == null)
{
// create new record
var linkData = new Link();
linkData.label = link.Text;
linkData.origin_id = (int)link.Origin.Id;
linkData.destination_id = (int)link.Destination.Id;
diagDB.Links.AddObject(linkData);
diagDB.SaveChanges();
link.Id = linkData.link_id;
}
else
{
// update existing record
var linkData = (from row in diagDB.Links
where row.link_id == (int)link.Id
select row).FirstOrDefault();
linkData.label = link.Text;
linkData.origin_id = (int)link.Origin.Id;
linkData.destination_id = (int)link.Destination.Id;
diagDB.SaveChanges();
}
}
}
}
}
The Id property of diagram items is set to the id of their respective database record, and is later used to determine if the item is a new or updated one. You could also handle events such as NodeDeleted and LinkDeleted to delete database records when items are removed.
I hope that helps,
Stoyan