I have developed c# WPF application, I am using MDF file for database, my connection string looks like :
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\appDataBase.mdf;Integrated Security=True;Connect Timeout=30
here I am replacing |DataDirectory| with user's appdata folder.
the database file is in user's appdata folder, I created a setup which
copies .mdf
file to _users appdata folder when I install it in
client's computer, it works perfectly
but when I uninstalls and
reinstall it, I am not able to connect that file, getting the error:
Can't open database "C:/users/myusername/appdata/roaming/myapplication/database/appDataBase.mdf" requested by login, login failed, login failed for user..
File is there, but somehow it is used by SQL server and can't open it. also while starting application every time I am creating backup of that file, that one also failing with error
System.IO.IOException: The process cannot access the file 'C:\Users\myusername\AppData\Roaming\myapplication\Database\appDataBase.mdf' because it is being used by another process.
Can anybody give me exact solution, because i have to use localdb only on my client computer , i don't want them let to do any extra configuration for installation of my app, i.am still facing iasue after i changed my code to close db connection on main window closing event.
This type of things happens because one of your object which is using that database is still running in the background so please dispose all the database related instance also dispose and close all the database related object in catch().
Try catch will help you to detect exact line number where you are getting error and also show due to which object you are getting error.
If you want to check why this is happening then open task manager and look out for services there you definitely find service which is holding object for database.