在C#中自动执行SQL Server备份

时间:2016-11-22 07:30:57

标签: c# sql-server

我正在编写一个Windows窗体应用程序来备份我的SQL Server数据库。我使用以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SQL_Server_DB_Backup_and_Restore
{
    public partial class Form1 : Form
    {
        private SqlConnection conn;
        private SqlCommand command;
        private SqlDataReader reader;
        string sql = "";
        string connectionString = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();

            if(dlg.ShowDialog()==DialogResult.OK)
            {
                txtBackupFileLocation.Text = dlg.SelectedPath;
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                connectionString = "Data Source= " + txtDataSource.Text + "; User Id=" + txtUserId.Text + "; Password=" + txtPassword.Text + "";
                conn = new SqlConnection(connectionString);
                conn.Open();
                //sql = "EXEC sp_databases";
                sql = "SELECT * FROM sys.databases d WHERE d.database_id>4";
                command = new SqlCommand(sql, conn);
                reader = command.ExecuteReader();
                cmbDatabases.Items.Clear();

                while(reader.Read())
                {
                    cmbDatabases.Items.Add(reader[0].ToString());
                }

                txtDataSource.Enabled = false;
                txtUserId.Enabled = false;
                txtPassword.Enabled = false;
                btnConnect.Enabled = false;
                btnDisconnect.Enabled = true;

                btnBackup.Enabled = true;
                btnRestore.Enabled = true;
                cmbDatabases.Enabled = true;
            }

            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            txtDataSource.Enabled = true;
            txtUserId.Enabled = true;
            txtPassword.Enabled = true;
            cmbDatabases.Enabled = false;
            btnBackup.Enabled = false;
            btnRestore.Enabled = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            btnDisconnect.Enabled = false;
            cmbDatabases.Enabled = false;
            btnBackup.Enabled = false;
            btnRestore.Enabled = false;
        }

        private void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                if(cmbDatabases.Text.CompareTo("")==0)
                {
                    MessageBox.Show("Please select a database");
                    return;
                }
                conn = new SqlConnection(connectionString);
                conn.Open();
                sql = "BACKUP DATABASE " + cmbDatabases.Text + " TO DISK = '" + txtBackupFileLocation.Text + "\\" + cmbDatabases.Text + "-" + DateTime.Now.Ticks.ToString() + ".bak'";
                command = new SqlCommand(sql, conn);
                command.ExecuteNonQuery();
                MessageBox.Show("Database backup is completed successfully");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

现在我必须按一个按钮进行备份。如何自动化流程以便我的应用程序可以每周进行备份?

1 个答案:

答案 0 :(得分:1)

MS SQL Server具有自动执行/计划数据库备份的功能。在对象资源管理器上。转到

管理 - >维护计划 - >新的维护计划
    并在维护计划任务上拖动备份数据库任务     然后单击上面的日历图标以设置备份时间。这适用于上面的SQL Server 2008。