資源簡介
用C#寫的實現數據庫備份與還原的工程(VS2008下可直接運行,低于08的版本可以拷貝代碼實現,記得要在COM中添加SQLSMO引用,本例數據庫為SuperMarket,請更改為你所需備份的數據庫),個人特意將兩種方法放在同一工程內,Form1是直接用SQL語句實現的,Form2是使用SQLSMO實現的,還有進度條控制,界面也挺漂亮的,因為為了個人存檔。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.IO;//File
using?System.Data.SqlClient;
using?System.Collections;//ArrayList
namespace?BFHY
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????public?void?beifenInfo()
????????{
????????????try
????????????{
????????????????saveFileDialog1.InitialDirectory?=?Application.StartupPath?+?“\\“;//默認路徑為D://
????????????????saveFileDialog1.FilterIndex?=?1; //默認值為第一個
????????????????saveFileDialog1.RestoreDirectory?=?true; //重新定位保存路徑
????????????????saveFileDialog1.Filter?=?“備份文件?(*.bak)|*.bak|所有文件?(*.*)|*.*“;//篩選器,定義文件類型
????????????????if?(saveFileDialog1.ShowDialog()?==?DialogResult.OK)
????????????????{
????????????????????if?(!File.Exists(saveFileDialog1.FileName.ToString()))
????????????????????{
????????????????????????SqlConnection?Con?=?new?SqlConnection(“Data?Source=.;Initial?Catalog=master;User?ID=sa;pwd?=xx“);
????????????????????????Con.Open();//k
????????????????????????SqlCommand?com?=?new?SqlCommand(“BACKUP?DATAbase??SuperMarket?TO?DISK?=?‘“?+?saveFileDialog1.FileName.ToString()?+?“‘“?Con);
????????????????????????this.textBox1.Text?=?saveFileDialog1.FileName.ToString();
????????????????????????com.ExecuteNonQuery();
????????????????????????Con.Close();//g??????????????????????
????????????????????????MessageBox.Show(“數據備份成功!“);
????????????????????}
????????????????????else
????????????????????{
????????????????????????MessageBox.Show(“請重新命名!“);
????????????????????}
????????????????}
????????????}
????????????catch?(Exception?k)
????????????{
????????????????MessageBox.Show(k.Message);
????????????????return;
????????????}
????????}
????????public?static?void?Restore(string?path)
????????{
????????????//殺死原來所有的數據庫連接進程
????????????SqlConnection?con?=?new?SqlConnection(“Data?Source=.;Initial?Catalog=master;User?ID=sa;pwd?=xx“);
????????????con.Open();//k
????????????SqlCommand?cmd?=?new?SqlCommand(“SELECT?spid?FROM?sysprocesses?sysdatabases?WHERE?sysprocesses.dbid=sysdatabases.dbid?AND?sysdatabases.Name=‘SuperMarket‘“?con);
????????????SqlDataReader?dr;
????????????ArrayList?list?=?new?ArrayList();
????????????try
????????????{
????????????????dr?=?cmd.ExecuteReader();
????????????????while?(dr.Read())
????????????????{
????????????????????list.Add(dr.GetInt16(0));
????????????????}
????????????????dr.Close();
????????????}
????????????catch?(SqlException?ex)
????????????{
????????????????MessageBox.Show(ex.ToString());
????????????}
????????????finally
????????????{
????????????????con.Close();//g
????????????}
????????????con.Open();//k
????????????for?(int?i?=?0;?i?????????????{
????????????????cmd?=?new?SqlCommand(string.Format(“KILL?{0}“?list[i]
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????4676??2009-05-09?16:20??BFHY\BFHY\BFHY.csproj
?????文件??????14328??2009-05-09?16:16??BFHY\BFHY\bin\Debug\BFHY.vshost.exe
?????文件????????490??2007-07-21?01:33??BFHY\BFHY\bin\Debug\BFHY.vshost.exe.manifest
?????文件???????4831??2009-05-09?16:17??BFHY\BFHY\Form1.cs
?????文件???????5582??2009-05-09?16:17??BFHY\BFHY\Form1.Designer.cs
?????文件?????253253??2009-05-09?16:17??BFHY\BFHY\Form1.resx
?????文件???????4896??2009-05-09?16:20??BFHY\BFHY\Form2.cs
?????文件??????10406??2009-05-09?16:20??BFHY\BFHY\Form2.Designer.cs
?????文件?????253253??2009-05-09?16:20??BFHY\BFHY\Form2.resx
?????文件????????187??2009-05-09?16:16??BFHY\BFHY\obj\Debug\BFHY.csproj.FileListAbsolute.txt
?????文件????????579??2009-05-09?16:02??BFHY\BFHY\obj\Debug\BFHY.csproj.ResolveComReference.cache
?????文件?????950272??2009-05-09?16:02??BFHY\BFHY\obj\Debug\Interop.SQLDMO.dll
?????文件???????5120??2009-05-09?16:20??BFHY\BFHY\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????485??2009-05-09?15:41??BFHY\BFHY\Program.cs
?????文件???????1364??2009-05-09?15:41??BFHY\BFHY\Properties\AssemblyInfo.cs
?????文件???????3818??2009-05-09?16:20??BFHY\BFHY\Properties\Resources.Designer.cs
?????文件???????6907??2009-05-09?16:20??BFHY\BFHY\Properties\Resources.resx
?????文件???????1089??2009-05-09?15:41??BFHY\BFHY\Properties\Settings.Designer.cs
?????文件????????249??2009-05-09?15:41??BFHY\BFHY\Properties\Settings.settings
?????文件???????6038??2009-05-09?16:19??BFHY\BFHY\Resources\bf.bmp
?????文件??????17478??2009-05-09?16:18??BFHY\BFHY\Resources\hh.bmp
?????文件???????6310??2009-05-09?16:20??BFHY\BFHY\Resources\hy.bmp
?????文件????3932214??2009-05-09?16:17??BFHY\BFHY\Resources\Wallpaper.bmp
?????文件????????902??2009-05-09?15:41??BFHY\BFHY.sln
????..A..H.?????15872??2009-05-09?16:21??BFHY\BFHY.suo
?????目錄??????????0??2009-05-09?16:20??BFHY\BFHY\obj\Debug\TempPE
?????目錄??????????0??2009-05-09?15:41??BFHY\BFHY\bin\Debug
?????目錄??????????0??2009-05-09?16:16??BFHY\BFHY\obj\Debug
?????目錄??????????0??2009-05-09?15:41??BFHY\BFHY\bin
?????目錄??????????0??2009-05-09?15:41??BFHY\BFHY\obj
............此處省略7個文件信息
- 上一篇:hadoop2.7.7@win64
- 下一篇:基于C#的題庫管理系統
評論
共有 條評論