3

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?

h bob
  • 3,310
  • 3
  • 29
  • 50
  • 1
    Have you looked at: http://stackoverflow.com/questions/25637076/is-it-possible-to-determine-the-size-of-a-sql-server-database or http://www.c-sharpcorner.com/UploadFile/0f68f2/how-to-get-whole-database-size-using-C-Sharp-in-sql-server/ – Greg Jun 12 '15 at 13:40
  • 1
    You could parse the connection string and read the file size on disk based on the file name – Johann Blais Jun 12 '15 at 13:43
  • @Greg The SO link not so useful, but that blog article is very useful! – h bob Jun 12 '15 at 13:45
  • @JohannBlais No that's the point, in shared hosting I don't have such privileges (I think!)... – h bob Jun 12 '15 at 13:46
  • 1
    Since you are FTP-ing manually, you could use a .NET FTP client to do the same from the application :) – Johann Blais Jun 12 '15 at 13:48
  • 1
    @hbob If you have shared hosting I doubt you have disk privileges. – Greg Jun 12 '15 at 13:48
  • @JohannBlais Yes we thought of that! It's sneaky but it works... Hope I can find something more robust though. – h bob Jun 12 '15 at 13:48

1 Answers1

3

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;
Yuri
  • 2,772
  • 4
  • 26
  • 39
  • Didn't think this would work for me. Then I discovered our shared host actually runs our app in full trust. So it works. – h bob Jun 15 '15 at 08:14