Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中自动化SQL Server备份#_C#_Sql Server - Fatal编程技术网

C# 在C中自动化SQL Server备份#

C# 在C中自动化SQL Server备份#,c#,sql-server,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.Win

我正在编写一个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);
            }
        }
    }

现在我必须按下一个按钮来备份。如何使该过程自动化,以便我的应用程序每周进行备份?

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

管理-->维护计划->新维护计划
并在维护计划任务上拖动备份数据库任务。

然后单击上面的日历图标设置备份时间。这适用于上面的SQL Server 2008。

windows计划程序可以在您需要的时候运行您的程序。只需选择exe并安排运行时间。在这种情况下,您不需要表单应用程序。控制台应用程序可以做到这一点。在Main方法中调用您的方法。这个关于以编程方式添加计划任务的问题可能很有用。