-1

My connection string is :

 @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + Application.StartupPath + @"\DataDirectory\sch.mdf;Integrated Security=True"

How to backup this database in a C# Windows Forms application?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Keramat
  • 63
  • 3
  • You want to backup a database by a connection string? – Ghasem Jan 10 '16 at 08:25
  • Try following [Backup SQL Server Database using ADO.NET](http://stackoverflow.com/questions/3142171/backup-sql-server-database-with-progress) – tchelidze Jan 10 '16 at 08:25
  • 3
    When you use the `AttachDbFileName=` approach (which I personally **DO NOT** recommend at all), then the database in question will have a logical name that is identical to the **entire path and file name** once it's attached to `LocalDB` - use that name to perform a normal `BACKUP DATABASE [YourDatabaseNameHere]` from C# – marc_s Jan 10 '16 at 08:38
  • Thank you The problem was solved – Keramat Jan 10 '16 at 14:38

1 Answers1

0
       folderBrowserDialog1.ShowDialog();

        string path1 = folderBrowserDialog1.SelectedPath;
        //MessageBox.Show(path1);
        DateTime dmiladi = DateTime.Now;
        PersianCalendar p = new PersianCalendar();

        string strdate = p.GetYear(dmiladi).ToString() + "/" + p.GetMonth(dmiladi).ToString() + "/" + p.GetDayOfMonth(dmiladi).ToString();

        string BackUpLocation = path1;
        string BackUpFileName = "backup_sch";
        string DatabaseName = Application.StartupPath + @"\DataDirectory\sch.mdf";
        string ServerName = "(LocalDB)\\v11.0";
        DatabaseName = "[" + DatabaseName + "]";
        string fileUNQ = p.GetYear(dmiladi).ToString() + "_" + p.GetMonth(dmiladi).ToString() + "_" + p.GetDayOfMonth(dmiladi).ToString() + "_time" + DateTime.Now.Hour.ToString() + "_" + DateTime.Now.Minute.ToString() + "_" + DateTime.Now.Second.ToString();

        BackUpFileName = BackUpFileName + fileUNQ + ".bak";
        string SQLBackUp = @"BACKUP DATABASE " + DatabaseName + " TO DISK = N'" + BackUpLocation + @"\" + BackUpFileName + @"'";

        string svr = "Server=" + ServerName + ";Database=master;Integrated Security=True";
        SqlConnection cnBk;
        SqlCommand cmdBkUp;
        cnBk = new SqlConnection(svr);

        cmdBkUp = new SqlCommand(SQLBackUp, cnBk);

        cnBk.Open();
        cmdBkUp.ExecuteNonQuery();
Keramat
  • 63
  • 3