Ejemplo n.º 1
0
        public bool StoreDatabase(string sFile, string sPath, string sDataBase)
        {
            bool      flag         = false;
            SQLServer serverObject = new SQLServerClass();

            SQLDMO.Restore restore = new RestoreClass();
            try
            {
                int num3;
                serverObject.LoginSecure = false;
                serverObject.Connect(this.Server, this.UserID, this.Password);
                QueryResults results = serverObject.EnumProcesses(-1);
                int          column  = -1;
                int          num2    = -1;
                for (num3 = 1; num3 < results.Columns; num3++)
                {
                    string str = results.get_ColumnName(num3);
                    if (str.ToUpper().Trim() == "SPID")
                    {
                        column = num3;
                    }
                    else if (str.ToUpper().Trim() == "DBNAME")
                    {
                        num2 = num3;
                    }
                    if ((column != -1) && (num2 != -1))
                    {
                        break;
                    }
                }
                num3 = 1;
                while (num3 < results.Rows)
                {
                    int columnLong = results.GetColumnLong(num3, column);
                    if (results.GetColumnString(num3, num2).ToUpper() == sDataBase.ToUpper())
                    {
                        serverObject.KillProcess(columnLong);
                    }
                    num3++;
                }
                QueryResults results2 = serverObject.ExecuteWithResults(string.Format("RESTORE FILELISTONLY FROM DISK = '{0}'", sFile), null);
                int          num5     = 0;
                int          num6     = 0;
                for (num3 = 1; num3 < results2.Columns; num3++)
                {
                    if ("LOGICALNAME" == results2.get_ColumnName(num3).ToUpper())
                    {
                        num5 = num3;
                    }
                    if ("PHYSICALNAME" == results2.get_ColumnName(num3).ToUpper())
                    {
                        num6 = num3;
                    }
                    if ((num6 != 0) && (num5 != 0))
                    {
                        break;
                    }
                }
                StringBuilder builder = new StringBuilder();
                for (num3 = 1; num3 <= results2.Rows; num3++)
                {
                    builder.Append(string.Format(@"[{0}],[{1}\{2}]", results2.GetColumnString(num3, num5), sPath, Path.GetFileName(results2.GetColumnString(num3, num6))));
                    if (num3 != results2.Rows)
                    {
                        builder.Append(",");
                    }
                }
                restore.Action          = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                restore.Database        = sDataBase;
                restore.Files           = sFile;
                restore.FileNumber      = 1;
                restore.ReplaceDatabase = false;
                restore.RelocateFiles   = builder.ToString();
                restore.SQLRestore(serverObject);
                Databases databases = serverObject.Databases;
                foreach (SQLDMO.Database database in databases)
                {
                    if (database.Name == sDataBase)
                    {
                        return(true);
                    }
                }
                return(flag);
            }
            catch (Exception exception)
            {
                Log.WriteLog(string.Format("还原数据库[{0}]:StoreDatabase(string sFile,string sPath,string sDataBase)", sDataBase), exception.Message);
                return(flag);
            }
            finally
            {
                serverObject.DisConnect();
            }
            return(flag);
        }
Ejemplo n.º 2
0
		public string RestoreDatabase(string backuppath, string ServerName, string UserName, string Password, string strDbName, string strFileName)
		{
			#region 数据库的恢复的代码

			SQLServer svr = new SQLServerClass();
			try
			{
				svr.Connect(ServerName, UserName, Password);
				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() == strDbName.ToUpper())
						svr.KillProcess(lPID);
				}


				Restore res = new RestoreClass();
				res.Action = 0;
				string path = backuppath + strFileName + ".config";
				res.Files = path;

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

				return string.Empty;
			}
			catch (Exception err)
			{
				string message = err.Message.Replace("'", " ");
				message = message.Replace("\n", " ");
				message = message.Replace("\\", "/");
                
				return message;
			}
			finally
			{
				svr.DisConnect();
			}

			#endregion
		}
Ejemplo n.º 3
0
        // Token: 0x0600027C RID: 636 RVA: 0x00008E34 File Offset: 0x00007034
        public static string RestoreDatabase(string backupfile)
        {
            string result;

            if (!File.Exists(backupfile))
            {
                result = "备份文件已不存在。";
            }
            else
            {
                DbConfigInfo dbConfig = DbConfigs.GetDbConfig();
                if (dbConfig.dbtype == FangPage.Data.DbType.SqlServer)
                {
                    SQLServer sqlserver = new SQLServerClass();
                    if (Path.GetExtension(backupfile) == ".zip" || Path.GetExtension(backupfile) == ".config")
                    {
                        FPZip.UnZipFile(backupfile, "");
                    }
                    backupfile = string.Concat(new string[]
                    {
                        Path.GetDirectoryName(backupfile),
                        "\\",
                        Path.GetFileName(backupfile),
                        "\\",
                        dbConfig.dbname,
                        ".bak"
                    });
                    if (!File.Exists(backupfile))
                    {
                        return("备份文件已不存在。");
                    }
                    try
                    {
                        sqlserver.Connect(dbConfig.dbpath, dbConfig.userid, dbConfig.password);
                        QueryResults queryResults = sqlserver.EnumProcesses(-1);
                        int          num          = -1;
                        int          num2         = -1;
                        for (int i = 1; i <= queryResults.Columns; i++)
                        {
                            string text = queryResults.get_ColumnName(i);
                            if (text.ToUpper().Trim() == "SPID")
                            {
                                num = i;
                            }
                            else if (text.ToUpper().Trim() == "DBNAME")
                            {
                                num2 = i;
                            }
                            if (num != -1 && num2 != -1)
                            {
                                break;
                            }
                        }
                        for (int i = 1; i <= queryResults.Rows; i++)
                        {
                            int    columnLong   = queryResults.GetColumnLong(i, num);
                            string columnString = queryResults.GetColumnString(i, num2);
                            if (columnString.ToUpper() == dbConfig.dbname.ToUpper())
                            {
                                sqlserver.KillProcess(columnLong);
                            }
                        }
                        ((_Restore) new RestoreClass
                        {
                            Action = SQLDMO_RESTORE_TYPE.SQLDMORestore_Database,
                            Files = backupfile,
                            Database = dbConfig.dbname,
                            ReplaceDatabase = true
                        }).SQLRestore(sqlserver);
                        File.Delete(backupfile);
                        return(string.Empty);
                    }
                    catch (Exception ex)
                    {
                        return(ex.Message);
                    }
                    finally
                    {
                        sqlserver.DisConnect();
                    }
                }
                string mapPath = FPUtils.GetMapPath(WebConfig.WebPath + Path.GetDirectoryName(dbConfig.dbpath));
                try
                {
                    if (!Directory.Exists(mapPath))
                    {
                        Directory.CreateDirectory(mapPath);
                    }
                    if (Path.GetExtension(backupfile) == ".zip" || Path.GetExtension(backupfile) == ".config")
                    {
                        FPZip.UnZipFile(backupfile, mapPath);
                    }
                    else
                    {
                        File.Copy(backupfile, mapPath + "\\" + dbConfig.dbname);
                    }
                    result = string.Empty;
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
            }
            return(result);
        }