在.Net中进行SQL Server数据库备份与还原操作实用类

类说明#region 类说明
//-----------------------------------------------------------------------------
//
// 项目名称:***
// 文件名称:DBBakManager.cs
// 文件说明:对数据库备份文件进行管理。提供备份、还原、备份文件管理操作。
//
// 开始日期:2007年04月04日
// 开发人员:***
//
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SQLDMO;       //添加引用C:Program FilesMicrosoft SQL Server80ToolsBinnSQLDMO.dll

namespace GSIM.StuManager
{
    /// <summary>
    /// 对SQL Server数据库备份文件进行管理。提供备份、还原、备份文件管理操作。
    /// </summary>
    public class DBBakManager
    {

        字段#region 字段

        private string _serverName;
        private string _userName;
        private string _password;
        private string _dbName;

        #endregion

        构造#region 构造

        /// <summary>

        /// <param name="serverName">数据库服务器名</param>
        /// <param name="userName">数据库用户名</param>
        /// <param name="password">用户密码</param>
        /// <param name="dbName">数据库名</param>
        public DBBakManager(string serverName, string userName, string password,string dbName)
        {
            _serverName = serverName;
            _userName = userName;
            _password = password;
            _dbName = dbName;
        }

        #endregion

        方法#region 方法

        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <param name="saveFileName">保存的备份文件路径(包括文件名,文件名格式为:"DBName(yyyyMMddHHmm).bak")</param>
        /// <returns>操作成功返回true,否则返回false</returns>
        public bool BackUpDB(string saveFileName)
        {
            SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();
            try
            {
                svr.Connect(_serverName, _userName, _password);
                SQLDMO.Backup bak = new SQLDMO.BackupClass();
                bak.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                bak.Initialize = true;
                bak.Database = _dbName;
                bak.Files = saveFileName;
                bak.SQLBackup(svr);
                return true;
            }
            catch (Exception err)
            {
                return false;
            }
            finally
            {
                svr.Close();
            }
        }

        /// <summary>
        /// 还原数据库备份
        /// </summary>
        /// <param name="fileName">还原的备份文件路径(包括文件名,文件名格式为:"DBName(yyyyMMddHHmm).bak")</param>
        /// <returns>操作成功返回true,否则返回false</returns>
        public bool RestoreDB(string fileName)
        {
            SQLDMO.SQLServer svr = new SQLDMO.SQLServerClass();
            try
            {
                svr.Connect(_serverName, _userName, _password);
                //取得所有的进程列表
                SQLDMO.QueryResults qr = svr.EnumProcesses(-1);
                int iColPIDNum = -1;
                int iColDbName = -1;
                //找到和要恢复数据库相关的进程
                for (int i = 1; i <= qr.Columns; i++)
                {
                    string strName = qr.get_ColumnName(i);
                    if (strName.ToUpper().Trim() == "SPID")
                    {
                        iColPIDNum = i;
                    }
                    else if (strName.ToUpper().Trim() == "DBNAME")
                    {
                        iColDbName = i;
                    }
                    if (iColPIDNum != -1 && iColDbName != -1)
                        break;
                }
                //将相关进程关闭              
                for (int i = 1; i <= qr.Rows; i++)
                {
                    int lPID = qr.GetColumnLong(i, iColPIDNum);
                    string strDBName = qr.GetColumnString(i, iColDbName);
                    if (strDBName.ToUpper() == _dbName)
                        svr.KillProcess(lPID);
                }

                SQLDMO.Restore res = new SQLDMO.RestoreClass();

                res.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                res.Files = fileName;

                res.Database = _dbName;
                res.FileNumber = 1;

                res.ReplaceDatabase = true;
                res.SQLRestore(svr);

                return true;
            }
            catch (Exception err)
            {
                return false;
            }
            finally
            {
                svr.Close();
            }
        }

        /// <summary>
        /// 获得所有数据库备份文件信息,如:200704041120.
        /// </summary>
        /// <param name="filePath">保存备份文件的文件夹路径(不包括文件名)</param>
        /// <returns>包含文件信息的字符串组</returns>
        public string[] GetAllDBBak(string filePath)
        {
            string[] DBBakInfo = Directory.GetFiles(filePath, "BMIS*");
            int start = DBBakInfo[0].IndexOf("(");
            int end = DBBakInfo[0].IndexOf(")");
            //除去多余部分,返回名字中的时间信息,如:200704041120.
            for (int i = 0; i < DBBakInfo.Length; i++)
            {
                DBBakInfo[i] = DBBakInfo[i].Substring(start + 1, end - start - 1);
            }
            return DBBakInfo;
        }

        /// <summary>
        /// 删除一个数据库备份文件
        /// </summary>
        /// <param name="fileName">删除的备份文件路径(包括文件名,文件名格式为:"DBName(yyyyMMddHHmm).bak")</param>
        /// <returns>操作成功返回true,否则返回false</returns>
        public bool DeleteDBBak(string fileName)
        {
            if (File.Exists(fileName) == true)
            {
                File.Delete(fileName);
                return true;
            }
            else
            {
                return false;
            }

        }

        #endregion
    }
}

posted @ 2009-04-03 16:30  arangly  阅读(229)  评论(0编辑  收藏  举报