开发者

How to create and restore backup of database using winform?

I'm making an application and I want to give a button to create Backup of database on the users desired location; like we save any file on any destination on computer and one button to restore the backup of whole database. How to accomplish this task?

I'm making backup through this code:

SqlCommand cmd = new SqlCommand(@"backup database StockDB to disk开发者_运维知识库 ='d:\StockDBBackUp1.bak' with init,stats=10", ConnectionClass.OpenConnection());  
cmd.ExecuteNonQuery(); 

Now if I give a folder name d:\Backup\StockDBBackUp1.bak then it should check that is the folder is there already. If not, then it should to create the folder and create the backup.


You can use System.IO functions to accomplish creating the directory:

if (!Directory.Exists(@"D:\Backup"))
    Directory.CreateDirectory(@"D:\Backup");


    private void button11_Click(object sender, EventArgs e)
  {
            DateTime PD = new DateTime(DateTime.Today);
            saveFileDialog1.FileName = PD.ToString("yyyy-MM-dd");
            saveFileDialog1.Filter = " files|*.bak;*.BAK";
            saveFileDialog1.InitialDirectory = "d:";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    string path = string.Empty;
                    path = saveFileDialog1.FileName;

                    path = path.Replace("\\", "//");
                    bool bBackUpStatus = true;
                    Cursor.Current = Cursors.WaitCursor;
                    if (System.IO.File.Exists(path))
                    {
                        if (MessageBox.Show(@"do you want a new one?", "the file is existing", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            System.IO.File.Delete(path);
                        }
                        else
                            bBackUpStatus = false;
                    }
                    if (bBackUpStatus)
                    {
                        //Connect to DB
                        SqlConnection Conn = new SqlConnection("Server=(local); Data Source=LocalHost; DataBase=Exchange; UID=sa; Pwd=sql");
                        //string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector  y|\SRVCARD.mdf;Integrated Security=True;User Instance=True";
                        if (Conn.State.ToString() == "Open")
                        {
                            Conn.Close();
                        }
                        Conn.Open();
                        SqlCommand command = new SqlCommand(@"backup database Exchange To Disk='" + path + "' with stats=10", Conn);
                        command.ExecuteNonQuery();
                        Conn.Close();
                        MessageBox.Show("Backup successfully", "Backup", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("please change the path");
                    //MessageBox.Show(ex.ToString());
                }
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜