Showing posts with label Sql Server Compact. Show all posts
Showing posts with label Sql Server Compact. Show all posts

Thursday, January 1, 2009

Reduce Sql Server Compact Database Size

In most of the cases, the use of Sql Server Compact edition has to do with a Desktop Application where size of the redistributed data do matter.
Having that in mind, we need a way to reduce the size of the database file, so that deployment procedures (possibly through ClickOnce Deployment) wont't need to download a huge amount of data.

As Microsoft states here data Compression on Sql Server Compact is not supported.

Trying to find an alternate way to reduce the database's size i ended up on these functions that Sql Server Compact engine offers through the SqlCeEngine class.
These are the Shrink and the Compact functions.
According to Microsoft:

Shrink
: Reclaims wasted space in the SQL Server Mobile database by moving empty pages to the end of the file, and then truncating the file.

Compact:Reclaims wasted space in the SQL Server Mobile database by creating a new database file from the existing file. This method is also used to change the collating order, encryption, or password settings of the database.

Also:
Unlike the Compact method, the Shrink method does not create a temporary database file. Instead, all empty and unallocated pages are moved to the end of the file and are then truncated, reducing the overall database size.

One must note though that these functions do not compress data, they will just remove unnecessary space claimed from the database system when querying or changing database's schema.

Having that in mind here is a snippet of both the functions

            string sDirectory = Path.GetDirectoryName(txtFileName.Text);
            string sFileName = Path.GetFileName(txtFileName.Text);
            string sNewFilePath = txtFileName.Text+"_old";
            try
            {
                File.Delete(sNewFilePath);
            }
            catch
            {
            }
 
            File.Move(txtFileName.Text,sNewFilePath);
 
 
            SqlCeEngine eng = new SqlCeEngine("Data Source=" + sNewFilePath);
            eng.Shrink();
            eng.Verify(VerifyOption.Enhanced);
            eng.Compact("Data Source=" + txtFileName.Text);

Sunday, December 21, 2008

Sql Server Compact 3.5 Sp1 with Entity Framework

Writing for C# and .Net Framework 3.5, recently i needed a local database for a Desktop Application.

I tried to figure out which Database system is best for my case.
My needs was a small footprint database system with good interaction with Visual Studio or with good Tools which should have a .Net Sql provider and it would be Free or cost minimum.
There was a good article (i will update here when i find it again) referring
Sql Server Compact (CE),
Sql Server Express,
Vista DB,
Firebird,
SqlClient and more.

I ended to the 2 Microsoft's database systems, Sql Server Compact and Sql Express.
There is a good article (MS Word Document) from MSDN comparing these two database systems.

After reading that too i ended up to the Sql Server Compact as it runs in process with my desktop app, it uses a small memory footprint, it does not need an installation procedure (X-Copy) and it seems fast enough for my needs.
One think it doesn't have is stored procedures and views.
As i needed a factor of abstraction into my database system i searched for some DAL (OR/M) components to sit on top of my Database.
Currently there are these three choices:
NHibernate seems to be a good solution and compared to Entity Framework it is more mature, but for me being a Microsoft lover (Why use the Entity Framework?), i preferred to give a chance to Entity Framework with the add on of the Linq queries i like very much.
Linq to Sql seems more like a "Queries simplifier" to me than a real OR/M system.

So, i ended up using Sql Server Compact with Entity Framework and Linq queries.
Trying to harness these beasts the following links seemed most useful to me:

Regarding Sql Server Compact
Regarding Entity Framework: