Exemplo 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);
        }