Saving and loading an empty database

Jun 9, 2011 at 6:14 PM

Hi there, do you know why I get a Sterling Database Not Found Exception when I try to

  • Load,
  • Save,
  • restart process,
  • then Load and Save 

...the database using the following test code:

public void SaveDatabase(string fileName)
        {
            FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.ReadWrite, FileShare.Write);
            using (var binaryWriter = new BinaryWriter(fs))
            {
                _engine.SterlingDatabase.Backup<MyDatabaseInstance>(binaryWriter);
            }
            fs.Close();
            fs.Dispose();
            _existsOnDisk = true;
        }

        public void LoadDatabase(string fileName)
        {
            if (File.Exists(fileName) && new FileInfo(fileName).Length > 0)
            {
                if (_engine != null) _engine.Dispose();
                _engine = new SterlingEngine();
                var fs = File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
                _engine.Activate();
                _databaseInstance = _engine.SterlingDatabase.RegisterDatabase<MyDatabaseInstance>();
                _engine.SterlingDatabase.Restore<MyDatabaseInstance>(new BinaryReader(fs));
                _engine.Dispose();

                _engine = new SterlingEngine();
                _engine.Activate();
                _existsOnDisk = true;
                fs.Close();
                fs.Dispose();
            }
        }

 

_model.LoadDatabase(

"TESTTESTTEST.STER"

);

_model.SaveDatabase(

"TESTTESTTEST.STER"

);

Generally speaking what I'm trying to achieve is being able to create a new database, save info to that db, persist it, close the process and (maybe some days later) load the database, add/update records and then persist those changes to disk.  Do I need to do this via the backup and restore methods?  I understand that Sterling uses files in Isolated Storage... however I'm running inside a console app for the database creation side.  I have tried to find the location of these files, but couldn't even using Process Monitor!

Later I want to export the database to WP7 and in preparation for this, I have the model classes inside a separate Silverlight 3 assembly...

thanks

Kris




Jun 9, 2011 at 6:15 PM

oh the actual exception occurs when I try to backup a database that I have previously restored... at line:

_engine.SterlingDatabase.Backup<MyDatabaseInstance>(binaryWriter);

 



thanks!

Jun 9, 2011 at 6:39 PM

it's OK!  I'm an idiot!  I did this

_databaseInstance = _engine.SterlingDatabase.RegisterDatabase<MyDatabaseInstance>();

instead of this:

_databaseInstance = _engine.SterlingDatabase.RegisterDatabase<MyDatabaseInstance>(new FileSystemDriver())

It now persists as expected!