-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?

4
  • You want to backup a database by a connection string? Commented Jan 10, 2016 at 8:25
  • Try following Backup SQL Server Database using ADO.NET Commented Jan 10, 2016 at 8: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# Commented Jan 10, 2016 at 8:38
  • Thank you The problem was solved Commented Jan 10, 2016 at 14:38

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.