0

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

S. B.
  • 186
  • 1
  • 12
  • 2
    Regular users can't write to files under the Program Files folder. You should put your data file elsewhere. Probably the AppData folder. be sure to account for that in your connection string. – John Aug 11 '22 at 09:11
  • Hi @John , thanks for your reply. I updated the connection string to AppData folder, but it gives error: An attempt to attach an auto-named database for file C:\Users\INTEL\AppData\Roaming\folder\dbUserRecords.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. Shall I create the .mdf file manually? Please check my edit in the question. If there is any better way, will you please share any references if possible? – S. B. Aug 11 '22 at 12:20
  • 1
    Are you sure you haven't used two different connection strings to attach two files containing the same database? That would account for a database writhe same name already being attached. – John Aug 11 '22 at 14:21
  • Hi @John, no I was using a single connection string, but that issue is now resolved by the code I added to copy the .mdf file from ProgramFiles to AppData. Please see my Edit in the question. – S. B. Aug 12 '22 at 05:59
  • You shouldn't need to copy the file in code. You're using an installer and it should be able to put the data file in the appropriate folder from the get-go. – John Aug 12 '22 at 06:02

0 Answers0