• 大小: 540KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-27
  • 语言: C#
  • 标签: C#  备份  还原  数据库  

资源简介

用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 < list.Count; 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个文件信息

评论

共有 条评论