0

I have localdb database with the name of golds.mdf, when I take backup of it, it says it does not exist, even though CRUD operations work completely fine. But when taking backup, I get the error that it does not exits.

This is my connection string:

<add name="cs" 
     connectionString="Data Source(LocalDB)\MSSQLLocalDB;AttachDbFilename='|DataDirectory|\golds.mdf';Integrated Security=True"/>

Here is the code for taking the backup:

string fullPath = "";

string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;

fullPath = (System.IO.Path.GetDirectoryName(executable));
AppDomain.CurrentDomain.SetData("DataDirectory", fullPath);

fullPath = fullPath + "\golds.mdf";
string insert = @"backup database ['" + fullPath + "'] to disk ='" + BackupPath.Text + "\" + currentdateInPer + ".bak'";
int r = database.setData(insert);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • For debugging try to use absolute file paths. localDB might store the DB file in a directory you do not expect. Try to find the DB file using Windows File Explorer, – rachri May 14 '23 at 10:27
  • I suggest stopping combing paths manually and instead use "Path.Combine". Doing it manually sometimes introduces errors, which are difficult to trace and sometimes you can't spot any difference, but they can occur. Path.Combine is guaranteed to work on all OSes. – jason.kaisersmith May 14 '23 at 10:29
  • I am not familiar with Path.Combine. any suggestion where I read it. – Zubairjahan May 14 '23 at 10:31
  • I'd advise you not to use `AttachDbFilename` see [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename), instead attach your database normally using `CREATE DATABASE FOR ATTACH` and connect using a normal connection string – Charlieface May 14 '23 at 10:59

0 Answers0