Search code examples
c#asp.netsql-serverasp.net-mvcsql-server-ce

Programmatically determine the size of a SQL Server Compact sdf file


I'm running an ASP.NET MVC app, in shared hosting. My data is in a SQL Server Compact database "sdf" file.

I have a limited amount of diskspace. I'm worried that I'll go over that limit and my site will break. I can FTP into my account, and check the sdf file's size, but that is a slow manual annoying process.

So, during runtime, given nothing but the connection string, can I determine the database's size on disk?


Solution

  • yes you can.

    FileInfo fi = new FileInfo(pathTosdfFile);
    long fs = fi.Length; //your file size in bytes
    

    btw, you can in your connection string restrict max size of your sdf file

    Data Source=your.sdf;Max Database Size=256;
    

    this will restrict grows over 256MB - default if I remember correctly 128MB

    and path to your database should be like this:

     string path =   System.IO.Path.GetDirectoryName
       (System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\your.sdf;