Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic persist data into database (Read 4830 times)
ricardo
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 12
Joined: Mar 5th, 2013
persist data into database
Mar 14th, 2013 at 2:00pm
Print Post  
Hi

what is the best way to persist the data into a database?

can you provide a sample?
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: persist data into database
Reply #1 - Mar 14th, 2013 at 3:45pm
Print Post  
Hi,

The simplest way would be to serialize the entire calendar as a string (using the Schedule.SaveTo method) and then write this string to the database. To serialize the calendar as a string use the following approach:

Code
Select All
using (MemoryStream s = new MemoryStream())
{
	calendar.Schedule.SaveTo(s);
	s.Flush();
	s.Seek(0, SeekOrigin.Begin);

	StreamReader reader = new StreamReader(s);
	string xml = reader.ReadToEnd();
	// Write 'xml' to the database
} 


Regards,
Meppy
  
Back to top
 
IP Logged
 
ricardo
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 12
Joined: Mar 5th, 2013
Re: persist data into database
Reply #2 - Mar 14th, 2013 at 5:51pm
Print Post  
three question more:

1) how to load it?

2) do you have any idea if the calendar is used very offen how much big will be the xml? this is why we where thinking in sql.

3) Can we restrict the amount of information to be saved (lets says only from 1 month ago and further)
  
Back to top
 
IP Logged
 
Lyubo
God Member
*****
Offline


MindFusion team

Posts: 511
Joined: Jun 17th, 2010
Re: persist data into database
Reply #3 - Mar 15th, 2013 at 10:39am
Print Post  
Hi,

1) To load the schedule from string you can use something like this (you may need to use different encoding):
Code
Select All
// Read the 'xml' string from database...
using (MemoryStream s = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml))
{
    calendar.Schedule.LoadFrom(s);
} 



2) An Appointment object in the calendar results to about 1K uncompressed xml text when saved. You can decrease the size by compressing the stream before saving with the help of the DeflateStream class (http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx).

3) The easiest way to do this is to save the existing Schedule to a Stream, load the stream into another Schedule object, modify the Items collection to remove the older items, and then save the modified schedule. Something like:
Code
Select All
private Schedule CloneSchedule(Schedule original)
{
    Schedule clone = new Schedule();
    using (MemoryStream s = new MemoryStream())
    {
        original.SaveTo(s);
        s.Flush();
        s.Seek(0, SeekOrigin.Begin);
        clone.LoadFrom(s);
        for (int i = clone.Items.Count - 1; i >= 0; i--)
        {
            Appointment app = clone.Items[i] as Appointment;
            if (app.StartTime < DateTime.Today.AddMonths(-1))
                clone.Items.Remove(app);
        }
    }
    return clone;
} 


Note that the above approach doesn't consider recurrent items.

Regards,
Lyubo
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint