0

I have a service based database called golds.mdf, when I take back up from it, it works fine but the back up does not contain any data.

This is my connection string.

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

This the code for taking back up.

DateTime date1 = DateTime.Today;

var calendar = new System.Globalization.PersianCalendar();
int year = calendar.GetYear(date1);
int month = calendar.GetMonth(date1);
int day = calendar.GetDayOfMonth(date1);
string date12 = string.Format(string.Format("{0}-{1}-{2}", year, month, day));
// string mybackup = "GolShop";
if (BackupPath.Text == string.Empty)
{
    MessageBox.Show("لطفا موقعیت نسخه را انتخاب کنید");
}
else
{
    int r = 0;
    string cmd = "BACKUP DATABASE golds TO Disk =  '" + BackupPath.Text + "\\" + "Dataase" + "-" + date12 + ".bak'";
    r = database.setData(cmd);
  
    database.closeConnection();
    this.Enabled = false;
    this.Enabled = true;

    MessageBox.Show("Backup done.....");
}

what is the solution to take backup and have the data.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • Why not just copy the file? – Daniel A. White May 28 '23 at 12:52
  • I take backup programmatically through my application. I do not understand what you mean copy the file? – Zubairjahan May 28 '23 at 12:54
  • my client takes backup every week, month or year, so I the backup button works fine, but the problem is that it does not contain any database. I can clearly see the backfile is created but no data at all. I do not know where it goes wrong. – Zubairjahan May 28 '23 at 12:58
  • The path, after the `=` is incorrect, this works: `BACKUP DATABASE test TO Disk = 'd:\temp\backup.bak'` (When you have a folder `d:\temp\`) – Luuk May 28 '23 at 12:58
  • The path is correct, I can clearly see the backup file is created but it does not have data at all. – Zubairjahan May 28 '23 at 13:01
  • Is `Dataase` in your code a cut and paste error or do you have this typo for real, maybe you are looking at the wrong file. – Stu May 28 '23 at 13:02
  • it is just a name for backup like Dataase-1401-02-06.bak. – Zubairjahan May 28 '23 at 13:08
  • what is the type of `database`? Because I don't know of a type with methods `setData` and `closeConnection`. – rene May 28 '23 at 13:42
  • setData is my function that executes the queries. and closeConnection is also a function that closes the connection. these two function are user-defined function not built-in functions, and database the objection of class Database. through object of class i call the function and execute queries such as setData(). – Zubairjahan May 28 '23 at 13:47
  • `AttachDbFilename` does weird stuff, it won't save the database back to its original location. You should instead just attach your database normally using `CREATE DATABASE FOR ATTACH`. See https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename – Charlieface May 28 '23 at 19:35
  • Does this answer your question? [what's the issue with AttachDbFilename](https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename) – Charlieface May 30 '23 at 10:49

0 Answers0