Asp.net 备份和还原SQL Server及压缩Access数据…

2009-05-12 22:04:53来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

 *
 * 功能说明:备份和恢复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 name="ServerIP">SQL服务器IP或(Localhost)</param>
       /// <param name="LoginName">数据库登录名</param>
       /// <param name="LoginPass">数据库登录密码</param>
       /// <param name="DBName">数据库名</param>
       /// <param name="BackPath">备份到的路径</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 name="ServerIP">SQL服务器IP或(Localhost)</param>
        /// <param name="LoginName">数据库登录名</param>
        /// <param name="LoginPass">数据库登录密码</param>
        /// <param name="DBName">要还原的数据库名</param>
        /// <param name="BackPath">数据库备份的路径</param>

        public static void SQLDbRestore(string ServerIP,string LoginName,string LoginPass,string DBName,string BackPath)
        {
          
            SQLDMO.Restore orestore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP, LoginName, LoginPass);
                orestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                orestore.Database = DBName;
                orestore.Files = BackPath;
                orestore.FileNumber = 1;
                orestore.ReplaceDatabase = true;
                orestore.SQLRestore(oSQLServer);

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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:VS 2008和.NET 3.5 Beta2常见问题的解决方案

下一篇:Asp.Net中动态页面转静态页面