• 大小: 65KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: C#
  • 标签: C#备份  

资源简介

C#远程备份、单机备份自写Demo,为了客户方便快捷使用!

资源截图

代码片段和文件信息

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

namespace 远程备份
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void backups1_Click(object sender EventArgs e)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[“ConnectionString“]);

            string path = txtPath1.Text;    //备份数据库文件存放的路径
            string dbname = txtName1.Text;   // 要备份的数据库的名字
            // 设置备份SQL语句
            string backupSql = “use master;“;
            backupSql += “BACKUP DATAbase @dbname TO DISK=@path;“;

            SqlCommand myCommand = new SqlCommand(backupSql conn); // 这里的conn你要先初始化
            myCommand.Parameters.Add(“@dbname“ dbname);
            myCommand.Parameters.Add(“@path“ path);
            try
            {
                myCommand.Connection.Open();
                myCommand.ExecuteNonQuery();
                lbl备份状态.Text = “备份状态:备份成功“;
            }
            catch (Exception ex)
            {
                lbl备份状态.Text = “备份状态:备份失败“;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myCommand.Connection.Close();
            }
        }

        private void recovery1_Click(object sender EventArgs e)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[“ConnectionStringZZ“]);

            string path = txtPath1.Text;    //恢复数据库文件存放的路径
            string dbname = txtName1.Text;  //要恢复的数据库的名字
            // 设置恢复SQL语句
            string backupSql = “use master;“;
            backupSql += “RESTORE DATAbase @dbname FROM DISK=@path;“;

            SqlCommand myCommand = new SqlCommand(backupSql conn);
            myCommand.Parameters.Add(“@dbname“ dbname);
            myCommand.Parameters.Add(“@path“ path);
            try
            {
                myCommand.Connection.Open();
                myCommand.ExecuteNonQuery();
                lbl恢复状态.Text = “恢复状态:恢复成功“;
            }
            catch (Exception ex)
            {
                lbl恢复状态.Text = “请先删除当前数据在进行恢复!“;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myCommand.Connection.Close();
            }
        }

        private void backups2_Click(object sender EventArgs e)
        {
            string saveAway = this.txtPath2.Text.ToString().Trim();
            string saveName = this.txtName2.Text.ToString().Trim();
            string cmdText = @“backup database “ + saveName + “ to disk=‘“ + saveAway + “‘“;
            BakReductSql(cmdText true);
        }

       

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-06-06 15:26  远程备份\
     目录           0  2017-06-06 15:26  远程备份\远程备份\
     文件         527  2017-06-06 15:45  远程备份\远程备份\App.config
     文件       13046  2017-06-06 15:46  远程备份\远程备份\Main.Designer.cs
     文件        5933  2017-06-06 15:46  远程备份\远程备份\Main.cs
     文件        5817  2017-06-06 15:46  远程备份\远程备份\Main.resx
     文件         523  2017-05-06 09:40  远程备份\远程备份\Program.cs
     目录           0  2017-06-06 15:26  远程备份\远程备份\Properties\
     文件        1362  2017-05-06 09:40  远程备份\远程备份\Properties\AssemblyInfo.cs
     文件        2876  2017-05-06 09:40  远程备份\远程备份\Properties\Resources.Designer.cs
     文件        5612  2017-05-06 09:40  远程备份\远程备份\Properties\Resources.resx
     文件        1099  2017-05-06 09:40  远程备份\远程备份\Properties\Settings.Designer.cs
     文件         249  2017-05-06 09:40  远程备份\远程备份\Properties\Settings.settings
     目录           0  2017-06-06 15:26  远程备份\远程备份\bin\
     目录           0  2017-06-06 15:26  远程备份\远程备份\bin\Debug\
     文件       14848  2017-06-06 15:44  远程备份\远程备份\bin\Debug\远程备份.exe
     文件         453  2017-06-06 15:44  远程备份\远程备份\bin\Debug\远程备份.exe.config
     文件       38400  2017-06-06 15:44  远程备份\远程备份\bin\Debug\远程备份.pdb
     文件       22984  2017-06-06 15:44  远程备份\远程备份\bin\Debug\远程备份.vshost.exe
     文件         453  2017-06-06 15:44  远程备份\远程备份\bin\Debug\远程备份.vshost.exe.config
     文件         490  2012-06-06 02:06  远程备份\远程备份\bin\Debug\远程备份.vshost.exe.manifest
     目录           0  2017-06-06 15:47  远程备份\远程备份\bin\Release\
     目录           0  2017-06-06 15:26  远程备份\远程备份\obj\
     目录           0  2017-06-06 15:44  远程备份\远程备份\obj\Debug\
     文件         863  2017-05-06 09:40  远程备份\远程备份\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7003  2017-06-06 15:26  远程备份\远程备份\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2017-06-06 15:47  远程备份\远程备份\obj\Debug\TempPE\
     文件           0  2017-05-06 09:40  远程备份\远程备份\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2017-05-06 09:40  远程备份\远程备份\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2017-05-06 09:40  远程备份\远程备份\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     文件         180  2017-06-06 15:44  远程备份\远程备份\obj\Debug\远程备份.Main.resources
............此处省略9个文件信息

评论

共有 条评论

相关资源