資源簡介
C#遠(yuǎn)程備份、單機(jī)備份自寫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?遠(yuǎn)程備份
{
????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;????//備份數(shù)據(jù)庫文件存放的路徑
????????????string?dbname?=?txtName1.Text;???//?要備份的數(shù)據(jù)庫的名字
????????????//?設(shè)置備份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備份狀態(tài).Text?=?“備份狀態(tài):備份成功“;
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????lbl備份狀態(tài).Text?=?“備份狀態(tài):備份失敗“;
????????????????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;????//恢復(fù)數(shù)據(jù)庫文件存放的路徑
????????????string?dbname?=?txtName1.Text;??//要恢復(fù)的數(shù)據(jù)庫的名字
????????????//?設(shè)置恢復(fù)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恢復(fù)狀態(tài).Text?=?“恢復(fù)狀態(tài):恢復(fù)成功“;
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????lbl恢復(fù)狀態(tài).Text?=?“請先刪除當(dāng)前數(shù)據(jù)在進(jìn)行恢復(fù)!“;
????????????????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??遠(yuǎn)程備份\
?????目錄???????????0??2017-06-06?15:26??遠(yuǎn)程備份\遠(yuǎn)程備份\
?????文件?????????527??2017-06-06?15:45??遠(yuǎn)程備份\遠(yuǎn)程備份\App.config
?????文件???????13046??2017-06-06?15:46??遠(yuǎn)程備份\遠(yuǎn)程備份\Main.Designer.cs
?????文件????????5933??2017-06-06?15:46??遠(yuǎn)程備份\遠(yuǎn)程備份\Main.cs
?????文件????????5817??2017-06-06?15:46??遠(yuǎn)程備份\遠(yuǎn)程備份\Main.resx
?????文件?????????523??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\Program.cs
?????目錄???????????0??2017-06-06?15:26??遠(yuǎn)程備份\遠(yuǎn)程備份\Properties\
?????文件????????1362??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\Properties\AssemblyInfo.cs
?????文件????????2876??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\Properties\Resources.Designer.cs
?????文件????????5612??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\Properties\Resources.resx
?????文件????????1099??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\Properties\Settings.Designer.cs
?????文件?????????249??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\Properties\Settings.settings
?????目錄???????????0??2017-06-06?15:26??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\
?????目錄???????????0??2017-06-06?15:26??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\
?????文件???????14848??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\遠(yuǎn)程備份.exe
?????文件?????????453??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\遠(yuǎn)程備份.exe.config
?????文件???????38400??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\遠(yuǎn)程備份.pdb
?????文件???????22984??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\遠(yuǎn)程備份.vshost.exe
?????文件?????????453??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\遠(yuǎn)程備份.vshost.exe.config
?????文件?????????490??2012-06-06?02:06??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Debug\遠(yuǎn)程備份.vshost.exe.manifest
?????目錄???????????0??2017-06-06?15:47??遠(yuǎn)程備份\遠(yuǎn)程備份\bin\Release\
?????目錄???????????0??2017-06-06?15:26??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\
?????目錄???????????0??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\
?????文件?????????863??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????7003??2017-06-06?15:26??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????目錄???????????0??2017-06-06?15:47??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\TempPE\
?????文件???????????0??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件???????????0??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件???????????0??2017-05-06?09:40??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件?????????180??2017-06-06?15:44??遠(yuǎn)程備份\遠(yuǎn)程備份\obj\Debug\遠(yuǎn)程備份.Main.resources
............此處省略9個文件信息
評論
共有 條評論