Click here to Skip to main content
15,897,151 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a windows service running using a local .mdf file and also have MVC projects which is also accesing same local .mdf file. When i run both the service and Project simultaneously i am getting error that "Datatbase is already used by another process or present on UNC Share". How do i run both both applications simultaneously using same .mdf file. Any help in this regard is highly appreciated.

What I have tried:

I have tried using transactions locks on to the database yet same error
Posted
Updated 24-Nov-17 21:16pm
Comments
José Amílcar Casimiro 25-Nov-17 6:34am    
Describe your solution architecture

1 solution

If you are connecting directly to MDF Files via OleDbConnection objects, then the problem with MDF files is that when you open a connection to them, they take an exclusive lock on the file so that it can be written to - which means that no other application can connect to the file until you release the connection. If they try, they will get the error message you have experienced.

You have two choices:
1) Change all applications to open the connection, do the work, and close the connection as fast as possible, while pausing and retrying if the connection fails.
2) Change all applications to use SQL Server (or MySql) instead of direct connection to MDF files, and let it handle the multiple accesses (that is what it is there for!)

The second sounds like a lot of work, but it's actually simpler than the first, and eliminates a lot of the problems that multiple access to MDF files gives that you haven't met yet. Pretty much, it's a case of replacing OleDbConnection, OleDbCommand, and such like objects with SqlConnection, SqlCommand, and so on.

If you are already using SQL Server, then the problem is probably your connections string: none of your apps should be attaching the database - it should be permanently held and processed by Sql Server. Attaching is a special Express edition mode for debugging only, and should not be used in "live" code.
 
Share this answer
 
Comments
Spring Boot 28-Aug-21 10:03am    
@OriginalGriff i have the same problem i have specified the mdf file path in connection string ... can u tell me how exactly to use in two different applications same DB
OriginalGriff 28-Aug-21 10:33am    
Then read what I said four years ago!
Spring Boot 28-Aug-21 12:03pm    
Not understanding how to do it without attaching the file path... any links which shows how ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900