Exemplo n.º 1
0
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <returns>string</returns>
        private string doBackUp()
        {
            // return conn + "|" + dbname + "|" + dbuser + "|" + dbpwd;
            if (dbtype.ToLower() != "dcsqlite")
            {
                SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
                SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
                try
                {

                    string bakname = IRequest.GetFormString("BackUp_Name");
                    string bakpath = "";
                    if (bakname.Contains(".bak"))
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname;
                    }
                    else
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname + ".bak";
                    }
                    oSQLServer.LoginSecure = false;
                    oSQLServer.Connect(conn, dbuser, dbpwd);
                    oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                    oBackup.Database = dbname;
                    oBackup.Files = bakpath;
                    oBackup.BackupSetName = bakname;
                    oBackup.BackupSetDescription = "数据库备份";
                    oBackup.Initialize = true;
                    oBackup.SQLBackup(oSQLServer);
                    return "备份成功!";

                }

                catch (Exception ex)
                {
                    return ex.Message;
                }
                finally
                {
                    oSQLServer.DisConnect();
                }
            }
            else if(dbtype.ToLower()=="dcsqlite")
            {
                try
                {
                    string sourcename=Request.MapPath("~/app_data")+"\\DcmsDataBase.db";
                    string bakname = IRequest.GetFormString("BackUp_Name");
                    string bakpath = "";
                    if (bakname.Contains(".db"))
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname;
                    }
                    else
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname + ".db";
                    }
                   // DirectoryInfo di = new DirectoryInfo(Request.MapPath("~/app_data"));
                    if(File.Exists(bakpath))//备份文件已存在
                    {
                        return "该备份文件已存在,请换个名称。";
                    }
                    File.Copy(sourcename, bakpath,false);
                    return "备份成功!";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
            return "找不到源数据库文件,备份失败!";
        }
Exemplo n.º 2
0
        /// <summary>
        /// 备份数据库
        /// </summary>
        /// <returns>string</returns>
        private string doBackUp()
        {
            // return conn + "|" + dbname + "|" + dbuser + "|" + dbpwd;
            if (dbtype.ToLower() != "dcsqlite")
            {
                SQLDMO.Backup    oBackup    = new SQLDMO.BackupClass();
                SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
                try
                {
                    string bakname = IRequest.GetFormString("BackUp_Name");
                    string bakpath = "";
                    if (bakname.Contains(".bak"))
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname;
                    }
                    else
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname + ".bak";
                    }
                    oSQLServer.LoginSecure = false;
                    oSQLServer.Connect(conn, dbuser, dbpwd);
                    oBackup.Action               = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                    oBackup.Database             = dbname;
                    oBackup.Files                = bakpath;
                    oBackup.BackupSetName        = bakname;
                    oBackup.BackupSetDescription = "数据库备份";
                    oBackup.Initialize           = true;
                    oBackup.SQLBackup(oSQLServer);
                    return("备份成功!");
                }

                catch (Exception ex)
                {
                    return(ex.Message);
                }
                finally
                {
                    oSQLServer.DisConnect();
                }
            }
            else if (dbtype.ToLower() == "dcsqlite")
            {
                try
                {
                    string sourcename = Request.MapPath("~/app_data") + "\\DcmsDataBase.db";
                    string bakname    = IRequest.GetFormString("BackUp_Name");
                    string bakpath    = "";
                    if (bakname.Contains(".db"))
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname;
                    }
                    else
                    {
                        bakpath = Request.MapPath("~/app_data") + "\\" + bakname + ".db";
                    }
                    // DirectoryInfo di = new DirectoryInfo(Request.MapPath("~/app_data"));
                    if (File.Exists(bakpath))//备份文件已存在
                    {
                        return("该备份文件已存在,请换个名称。");
                    }
                    File.Copy(sourcename, bakpath, false);
                    return("备份成功!");
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }
            return("找不到源数据库文件,备份失败!");
        }