Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I developed a Windows Form Application by using C# and SQL Server 2005. It is running and functioning properly on the client machine. Now the client told me to update the application by applying a facility of "Creating Backup of database". I want to apply this facility programmatically or by writing some code. I don't know how to do this. Please someone help me.
Posted

Hi
Yes you can do it really easily just take a look follow of link please
SQL Server 2005 Database Backup and Restore using C# and .NET 2.0[^]
Best regards.
 
Share this answer
 
v2
you have to have the following references

using Microsoft.SqlServer.Management.Smo;


what's more the namespace Microsoft.sqlserver.smoextended is actualized in the get together called Microsoft.sqlserver.smoextended.dll which ought to be discovered in the catalog C:\program Files\microsoft SQL Server\100\sdk\assemblies\ in the event that you have SMO installed.

now the code is
private void btn_Start_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(saveFileDialog1.FileName))
            {
                MessageBox.Show("Select file first!");
            }
            else
            {
               // selection.restoreDatabase();
                //int num = selection.backupDatabase(saveFileDialog1.FileName);
                //this.Close();
               // MessageBox.Show(num.ToString());
                //MessageBox.Show("DONE");
                BackupDb();
            }
        }

private void btnBackupDb_Click(object sender, EventArgs e)
       {
           if (string.IsNullOrEmpty(saveFileDialog1.FileName))
           {
               MessageBox.Show("Select file first!");
           }
           else
           {
               BackupDb();
           }
       }

private void BackupDb()
        {
            dbName = ((Database)cmbBackupDb.SelectedItem).Name;
            Backup dbBackup = new Backup();
            try
            {
                dbBackup.Action = BackupActionType.Database;
                dbBackup.Database = dbName;
                dbBackup.BackupSetName = string.Format("{0} backup set.", dbName);
                dbBackup.BackupSetDescription = string.Format("Database: {0}. Date: {1}.", dbName, DateTime.Now.ToString("dd.MM.yyyy hh:m"));
                dbBackup.MediaDescription = "Disk";

                BackupDeviceItem device = new BackupDeviceItem(saveFileDialog1.FileName, DeviceType.File);
                dbBackup.Devices.Add(device);

                txtBackupSql.Text = dbBackup.Script(sqlServer);

                progBar.Visible = true;
                progBar.Value = 0;

                

                //get server id
                string myHost = System.Net.Dns.GetHostName();

                var ip_Address = Dns.GetHostEntry(myHost).AddressList.First(ipAddress => ipAddress.AddressFamily == AddressFamily.InterNetwork);

                if(ip_Address.ToString()=="192.168.1.10".ToString())
                {
                    dbBackup.Complete += new ServerMessageEventHandler(dbBackup_Complete);
                    dbBackup.PercentCompleteNotification = 5;
                    dbBackup.PercentComplete += new PercentCompleteEventHandler(dbBackup_PercentComplete);
                    dbBackup.SqlBackup(sqlServer);

                    progBar.Visible = false;
                }
                else
                {
                    MessageBox.Show("You can only create or restore backup only from server computer!");
                }
                this.Close();
                
            }
            catch (Exception exc)
            {
                dbBackup.Abort();
                MessageBox.Show(string.Format("Exception occured.\nMessage: {0}", exc.Message));
            }
            finally
            {
                sqlConn.Close();
            }

        }

        void dbBackup_PercentComplete(object sender, PercentCompleteEventArgs e)
        {
            if (progBar.Value < progBar.Maximum)
            {
                if ((progBar.Value + e.Percent) <= 100)
                {
                    progBar.Value += e.Percent;
                }
                else
                    progBar.Value = 100;
            }
        }

        void dbBackup_Complete(object sender, ServerMessageEventArgs e)
        {
            MessageBox.Show("Backup complete");
            lbl_confirm.Text = "DATABASE SUCCESSFULLY BACKED UP";
            lbl_confirm.Visible = true;
            lbl_confirm.ForeColor = Color.Green;
        }
 
Share this answer
 
v3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900