db_back_restore.cs

来自「主要实现仓库的入库与出库的基本功能」· CS 代码 · 共 65 行

CS
65
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace Elysian
{
    public class DB_back_restore
    {
        //备份
        public static void Back(string str,string filePath)
        {
            string strconn = DataSource.GetConnectionString();
            SqlConnection sqlconn = new SqlConnection(strconn);
            SqlCommand sqlcomm = new SqlCommand("backup database '" + str + "' to disk='" + filePath + "' ", sqlconn);
            sqlcomm.CommandType = CommandType.Text;
            try
            {
                sqlconn.Open();
                sqlcomm.ExecuteNonQuery();
                sqlconn.Close();
                MessageBox.Show("备份成功!");
            }
            catch (Exception ex)
            {
                //throw (ex);
                MessageBox.Show("备份失败!",ex.ToString());
            }
        }
        //还原
        public static void restore(string str, string filePath)
        {
            string strconn = DataSource.GetConnectionString();
            SqlConnection sqlconn = new SqlConnection(strconn);
            SqlCommand sqlcomm = new SqlCommand("restore database '" + str + "' from disk='" + filePath + "' ", sqlconn);
            sqlcomm.CommandType = CommandType.Text;
            try
            {
                sqlconn.Open();
                sqlcomm.ExecuteNonQuery();
                sqlconn.Close();
                MessageBox.Show("备份还原成功!");
            }
            catch (Exception ex)
            {
                //throw (ex);
                MessageBox.Show("备份还原失败!",ex.ToString());
            }
        }

    }
}


/*  BACKUP DATABASE Northwind 
    TO DISK = 'c:\Northwind.bak'
    RESTORE FILELISTONLY 
    FROM DISK = 'c:\Northwind.bak'
    RESTORE DATABASE TestDB 
    FROM DISK = 'c:\Northwind.bak'
    WITH MOVE 'Northwind' TO 'c:\test\testdb.mdf',
    MOVE 'Northwind_log' TO 'c:\test\testdb.ldf'
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?