当前位置: 代码迷 >> C# >> 数据库备份还原。该怎么处理
  详细解决方案

数据库备份还原。该怎么处理

热度:17   发布时间:2016-05-05 03:51:12.0
数据库备份还原。。。急急急急

备份部分代码:
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.Data.SqlClient;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }
        string backupPath = "";
        string restorePath = "";
        private void Form5_Load(object sender, EventArgs e)
        {
        }
        public static string connstr = "server=.;database=小区物业;uid=sky;pwd=";
        public static SqlConnection conn = new SqlConnection(connstr);

        private void button1_Click(object sender, EventArgs e)//查找button
        {
            saveFileDialog1.FilterIndex = 0;
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "Bak Files (*.bak)|*.bak|All Files(*.*)|*.*";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = saveFileDialog1.FileName.ToString();
                textBox1.ReadOnly = true;
            }
            backupPath = textBox1.Text.Trim();
        }

        private void button2_Click(object sender, EventArgs e)// 备份button
        {
            try
            {
                if (backupPath == "")
                {
                    MessageBox.Show("请先选择数据库备份路径", "提示");
                    return;
                }
                if (File.Exists(backupPath))
                {
                    File.Delete(backupPath);
                }
                string sqlstr;
                sqlstr = "backup database  小区物业 to disk='" + backupPath + "'";
                SqlCommand sqlCom = new SqlCommand(sqlstr, conn);
                conn.Open();
                sqlCom.ExecuteNonQuery();
                conn.Close();
                if (MessageBox.Show("数据库备份成功", "提示", MessageBoxButtons.OK) == DialogResult.OK)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }  
        }     
    }
}


求大神指点,我大后天就要交毕设了,手贱在毕设任务书上写了这个备份还原功能。
能成功的代码复制过来也行。
------解决思路----------------------
http://blog.sina.com.cn/s/blog_54c367d401017i2m.html
  相关解决方案