⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db_back_restore.cs

📁 主要实现仓库的入库与出库的基本功能
💻 CS
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -