4

Im fairly new to ASP.net (making basic applications for about 2 weeks), and im struck by a big problem with my databases.

I was building a simple email application, with a login system. Everything was working just fine a few days ago.

But somehow, everytime i try to access a database file with my programs im getting the following error:

*Server Error in '/' Application. The database file may be corrupted. Run the repair utility to check the database file. [ Database name = C:\Users\ArvinAsus\Documents\Visual Studio 2010\Projects\mailer\App_Data\Database.mdf ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlServerCe.SqlCeException: The database file may be corrupted. Run the repair utility to check the database file. [ Database name = C:\Users\ArvinAsus\Documents\Visual Studio 2010\Projects\mailer\App_Data\Database.mdf ]*

This error is always displayed with every database. Wether i use a database that worked in the past or just create a new empty db.

Im having the feeling that this problem has nothing to do with db corruption but with another problem i am having:

When i have my local database connected in the server explorer, i am able to add tables - fill tables with data, test connection always succeeds. when i try to connect to the database through my program :

*Exception Details: System.Data.SqlServerCe.SqlCeException: There is a file sharing violation. A different process might be using the file. [ C:\Users\ArvinAsus\Documents\Visual Studio 2010\Projects\mailer\App_Data\Database.mdf ]*

I then close the connection in the server explorer, and the error vanishes, leaving me with the corruption error.

I am completely stuck with this. Anybody with an idea to get me going again?

thanks in advance.

PS: Im using Visual Studio 2010 Professional

Arvin
  • 115
  • 6
  • You can look at this: http://stackoverflow.com/questions/427888/why-do-i-receive-file-share-error-if-sql-compact-allows-multiple-connections – James Black Nov 12 '12 at 14:17

3 Answers3

3

A .MDF file is NOT a SQL Compact Edition Database file but a SQL Server data file. You should use SQL Server (Express, Professional or any other version) to access your database and conect to it using SQL Native Client, OleDB or any other Data Connector instead of SQLServerCE.

Yván Ecarri
  • 1,661
  • 18
  • 39
  • Thanks i will try that, only thing i don't understand is why it has worked in the exact same way a few days ago? Visual studio installed SQLserverCE, so then why does it make .mdf databases if that doesn't work with SQL Compact edition? – Arvin Nov 12 '12 at 14:28
  • No idea! :-/ I've seen several cases where people can read MDF files while the SQL Server is DOWN so I guess both file formats share some basic structure. – Yván Ecarri Nov 12 '12 at 14:32
2

Your exception tells you what's going on: There is a file sharing violation. A different process might be using the file. Many processes don't let go of files even after they're done using them. You should be using an actual SQL server instance (even if it's express) to be able to manage connections into the database.

I upvoted @Y. Ecarri's answer because I think he has the right solution, but the most likely reason you're getting a file sharing conflict is the development environment itself. If you're using the built-in Cassini server (likely) that comes with the IDE debugger, it will keep the development web server alive between debugging sessions. Since the first session keeps the instance of your .mdf in memory alive and taken, subsequent debugging sessions will not be able to access it even though it should be available. In order to release the file you'll need to stop debugging, then right-click on any icons in the system tray representing development web server instances and tell them to Stop.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • Great explanation! That solves the mystery I mentioned in my previous comment. – Yván Ecarri Nov 12 '12 at 14:35
  • thank you for your answer. pressing stop debugging does not fix the file sharing violation error though. Stopping the servers in my system tray doesnt help either! I will try using SQL server express and use an .sdf file instead to see if that will work – Arvin Nov 12 '12 at 14:40
  • The only way for me to fix the file share violation for me is to goto the server explorer and close the database connection. – Arvin Nov 12 '12 at 14:43
  • @user1818243: Not just stop debugging. You need to stop all of the web server instances related to that environment session. However, if you're concerned that an external source has your file locked, closing the IDE and reopening it may tell you if that's the cause. – Joel Etherton Nov 12 '12 at 14:43
  • @user1818243: Actually, yes, you've found yet another way the IDE will hold your file "hostage". This is the main reason it is recommended that you always have some form of SQL instance to use rather than a file. – Joel Etherton Nov 12 '12 at 14:44
  • Thanks a lot for your help, i will try to fix it now. – Arvin Nov 12 '12 at 14:51
0

The problem with me was that my web.config file was modified. The physical location of the database was changed and it was throwing this error. The database was still there but it was not pointing to the right location.

To assure this is not the problem, double check your web.config file. I hope this helps someone.

Termato
  • 1,556
  • 1
  • 16
  • 33