asp.net 备份和恢复数据库的方法示例


/**********************************************************************************

*
 * 功能说明:备份和恢复SQL Server数据库
 * 作者: 刘功勋;
 * 版本:V0.1(C#2.0);时间:2007-1-1
 * 当使用SQL Server时,请引用 COM组件中的,SQLDMO.dll组件
 * 当使用Access中,请浏览添加引用以下两个dll
 *          引用C:\Program Files\Common Files\System\ado\msadox.dll,该DLL包含ADOX命名空间
 *          引用C:\Program Files\Common Files\System\ado\msjro.dll,该DLL包含JRO命名空间
 * *******************************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ADOX;//该命名空间包含创建ACCESS的类(方法)--解决方案 ==> 引用 ==> 添加引用 ==> 游览找到.dll
using JRO;//该命名空间包含压缩ACCESS的类(方法)

namespace EC
{
    /// <summary>
    /// 数据库恢复和备份
    /// </summary>
    public class SqlBackObject
    {
        public SqlBackObject()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

#region SQL数据库备份
       /// <summary>
        /// SQL数据库备份
       /// </summary>
       /// <param>SQL服务器IP或(Localhost)</param>
       /// <param>数据库登录名</param>
       /// <param>数据库登录密码</param>
       /// <param>数据库名</param>
       /// <param>备份到的路径</param>
        public static void SQLBACK(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
        {
            SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP, LoginName, LoginPass);
                oBackup.Database = DBName;
                oBackup.Files = BackPath;
                oBackup.BackupSetName = DBName;
                oBackup.BackupSetDescription = "数据库备份";
                oBackup.Initialize = true;
                oBackup.SQLBackup(oSQLServer);

}
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
            finally
            {
                oSQLServer.DisConnect();
            }
        }
        #endregion

#region SQL恢复数据库
        /// <summary>
        /// SQL恢复数据库
        /// </summary>
        /// <param>SQL服务器IP或(Localhost)</param>
        /// <param>数据库登录名</param>
        /// <param>数据库登录密码</param>
        /// <param>要还原的数据库名</param>
        /// <param>数据库备份的路径</param>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzxfx.html