Take you class and alter the [ClassOverview] attribute to read
/// <summary>class representing a television programme</summary>
[ClassOverview("television_programme", "television_programme_id", "xml")]
public class TelevisionProgramme
This tells the content entry system that this class is to be stored in a database table called "television_programme", using "television_programme_id" as the id column and storing the XML information in a column called "xml".
Create a table in your database with the script:
create table television_programme (xml text NOT NULL, television_programme_id BIGINT NOT NULL)
ALTER TABLE television_programme ADD CONSTRAINT PK_television_programme PRIMARY KEY (television_programme_id)
Now we are ready to store the class in the database. Add a reference to your project:
Ant.ContentEntry.dll, Ant.Shared.dll, Ant.Security.Windows.dll
Add the following using statements to the MainClass
using Microsoft.Win32;
using Ant.ContentEntry.Persistence;
using Ant.Security;
using Ant.Security.Windows;
using Ant.Shared;
using Ant.Database;
using Ant.Licensing;
Now we have to initialise (once only per executable) the managers, and ask the user to logon, so add the following code:
{
ConfigurationKey key = new RegistryConfigurationKey(Registry.LocalMachine.OpenSubKey("Software\\Ant"));
Logon logon=null;
LicenseManager.Init(key);
SecurityManager.Init(key);
if (SecurityManager.Get().HasRemoteConfiguration())
{
logon = ShowLogon();
if (logon == null)
return;
key = logon.GetRemoteConfigurationKey();
}
Ant.Database.DatabaseManager.Init(key);
Ant.ContentEntry.ContentEntryManager.Init(key);
if (!SecurityManager.Get().HasRemoteConfiguration())
{
logon = ShowLogon();
if (logon == null)
return;
}
Application.Run(new ViewHierarchy(logon.AuthenticatedUser));
}
static private Logon ShowLogon()
{
Logon logon = new Logon();
DialogResult res;
try
{
res = logon.ShowDialog();
}
catch (SecurityException)
{
MessageBox.Show("The supplied logon credentials did not match the user database.", "Logon failed");
return null;
}
if (res != DialogResult.OK)
return null;
return logon;
}
Finally add the code to store the class:
XmlPersistenceDatabase db = new XmlPersistenceDatabase(DatabaseManager.Get().GetDatabase("Main"));
object databaseKey = db.PutNewInstanceIntoStorage(def, form.InstanceData);
The entire code should now look like this:
using System;
using System.Xml;
using System.Windows.Forms;
using Microsoft.Win32;
using Ant.Security;
using Ant.Security.Windows;
using Ant.Shared;
using Ant.Database;
using Ant.ContentEntry;
using Ant.ContentEntry.Windows;
using Ant.ContentEntry.Persistence;
namespace HowTos
{
/// <summary>
/// Class which is run to start the application, edit the class and trace the results
/// </summary>
public class MainClass
{
static public int Main(string[] args)
{
ConfigurationKey key = new RegistryConfigurationKey(Registry.LocalMachine.OpenSubKey("Software\\Ant"));
Logon logon=null;
SecurityManager.Init(key);
if (SecurityManager.Get().HasRemoteConfiguration())
{
logon = ShowLogon();
if (logon == null)
return 1;
key = logon.GetRemoteConfigurationKey();
}
Ant.Database.DatabaseManager.Init(key);
Ant.ContentEntry.ContentEntryManager.Init(key);
if (!SecurityManager.Get().HasRemoteConfiguration())
{
logon = ShowLogon();
if (logon == null)
return 1;
}
// Gets the content definition for our class, using default (null) user
ContentDefinition def = new ContentDefinitionFactory(logon.AuthenticatedUser, "ENGLISH", 0).ForClass(typeof(TelevisionProgramme));
// Construct a version of our class to edit
TelevisionProgramme programme = new TelevisionProgramme();
// Construct a form off this object
ContentEntryWindowsForm form = new ContentEntryWindowsForm("Test", def, XmlHelper.GetInstanceFromObject(def, programme));
// Show the form and check the OK button was checked
if (form.ShowDialog() == DialogResult.OK)
{
// Map the values back in to the object
programme = XmlHelper.GetObjectFromInstance(def, form.InstanceData) as TelevisionProgramme;
XmlPersistenceDatabase db = new XmlPersistenceDatabase(DatabaseManager.Get().GetDatabase("Main"));
object databaseKey = db.PutNewInstanceIntoStorage(def, form.InstanceData);
//Trace out the results
System.Diagnostics.Trace.WriteLine("Programme name:"+programme.ProgrammeName);
System.Diagnostics.Trace.WriteLine("Channel:"+programme.Channel.ToString());
System.Diagnostics.Trace.WriteLine("Date:"+programme.Start.ToString("dd-MM-yyyy HH:mm"));
System.Diagnostics.Trace.WriteLine("Duration:"+programme.Duration.ToString());
}
return 0;
}
static private Logon ShowLogon()
{
Logon logon = new Logon();
DialogResult res;
try
{
res = logon.ShowDialog();
}
catch (SecurityException)
{
MessageBox.Show("The supplied logon credentials did not match the user database.", "Logon failed");
return null;
}
if (res != DialogResult.OK)
return null;
return logon;
}
}
}
Now list the table in the database to see that your values have been saved. At the moment all the information is stored in one XML blob. In the next step we will show how to generate more complex data.