Failed to update database "C:\Program Files (x86)\folder\dbRecords.mdf" because database is read-only

343 views Asked by At

I am creating a C# WinForms Application along with SQL LocalDB. I am new to SQL. I have also added a setup project to create install the application to other PCs. When I installed the application, it is giving the error:

enter image description here

I do not want user to manually change the permissions and I am not in control of the location whether user will install on C: or any other drive. Is there any way I can update the settings while creating the setup file? The ReadOnly property of the database in Setup is already False.

enter image description here

Is there any other way I can achieve this?

I have checked many posts regarding the same issue which are not working for me:

Unable to update database .MDF is Read Only

Failed to update .mdf database because the database is read-only (Windows application)

Edit: Following @John's solution I added the below code. Please let me know if this is the right way or there is anything more optimized.

        var path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\AgeCalculator";

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
            File.Copy(Directory.GetCurrentDirectory() + "\\dbUserRecords.mdf", path + "\\dbUserRecords.mdf");
        }
        
        string conn = Properties.Settings.Default.dbUserRecordsConnectionString;
        //Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\dbUserRecords.mdf;Integrated Security=True
        conn = conn.Split('|')[0] + path + conn.Split('|')[2];

Edit 2:

I found the better way to do the above Edit :

connectionstring to Access sql localDB in appdata folder

https://stackoverflow.com/a/8354765/10975845

0

There are 0 answers