Beispiel #1
0
        public static bool CheckDependencies(ref string message)
        {
            // Create an instance of dependent classes in a new AppDomain
            try
            {
                string rulesEngineConfigFile = CommonFunc.GetAppSettingsStringValue("NotSupportedByAzureSQLDb");
                if (rulesEngineConfigFile.Length > 0 && !File.Exists(rulesEngineConfigFile))
                {
                    message = CommonFunc.FormatString(Properties.Resources.ErrorFileNotFound, rulesEngineConfigFile);
                    return(false);
                }

                AppDomain newAppDomain = AppDomain.CreateDomain("DependencyChecker");
                foreach (Dependency depOn in Dependencies)
                {
                    newAppDomain.CreateInstance(depOn.Assembly, depOn.Type);
                }
            }
            catch (Exception ex)
            {
                Assembly     assem     = Assembly.GetEntryAssembly();
                AssemblyName assemName = assem.GetName();

                message = CommonFunc.FormatString(Properties.Resources.ErrorMissingDependencies, assemName.Name, ex.Message);
                return(false);
            }
            return(true);
        }
        public static bool IsTransient(Exception ex)
        {
            if (ex != null)
            {
                if (_sqlErrorCodes == null)
                {
                    string temp  = CommonFunc.GetAppSettingsStringValue("BCPSQLAzureErrorCodesRetry");
                    int    start = temp.IndexOf('(');
                    int    end   = temp.IndexOf(')');
                    _sqlErrorCodes = temp.Substring(start + 1, end - start - 1).Split('|');
                    if (_sqlErrorCodes == null || _sqlErrorCodes.Length < 1)
                    {
                        _sqlErrorCodes = "64|233|08001|08S01|10053|10054|10060|40001|40143|40174|40197|40501|40544|40549|40550|40551|40552|40553|40613|40615".Split('|');
                    }
                }

                SqlException sqlException;
                if ((sqlException = ex as SqlException) != null)
                {
                    // Enumerate through all errors found in the exception.
                    foreach (SqlError err in sqlException.Errors)
                    {
                        foreach (string errorCode in _sqlErrorCodes)
                        {
                            if (err.Number.ToString().Equals(errorCode))
                            {
                                return(true);
                            }
                        }
                    }
                }
                else if (ex is TimeoutException)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
        public static SMOScriptOptions CreateFromConfig()
        {
            var smoScriptOpts = new SMOScriptOptions();
            LocalizedDefaultValueAttribute target = new LocalizedDefaultValueAttribute(CommonFunc.GetAppSettingsStringValue("TargetServerType"));

            if (target != null && target.Value.ToString().Length > 0)
            {
                smoScriptOpts.TargetServer = (string)target.Value;
            }

            smoScriptOpts.ScriptDefaults     = CommonFunc.GetAppSettingsBoolValue("ScriptDefaults");
            smoScriptOpts.ScriptHeaders      = CommonFunc.GetAppSettingsBoolValue("ScriptHeaders");
            smoScriptOpts.IncludeIfNotExists = CommonFunc.GetAppSettingsBoolValue("IncludeIfNotExists");

            LocalizedDefaultValueAttribute sdc = new LocalizedDefaultValueAttribute(ConfigurationManager.AppSettings["ScriptDropCreate"]);

            smoScriptOpts.ScriptDropCreate = (string)sdc.Value;

            smoScriptOpts.ScriptCheckConstraints = CommonFunc.GetAppSettingsBoolValue("ScriptCheckConstraints");
            smoScriptOpts.ScriptCollation        = CommonFunc.GetAppSettingsBoolValue("ScriptCollation");
            smoScriptOpts.ScriptForeignKeys      = CommonFunc.GetAppSettingsBoolValue("ScriptForeignKeys");
            smoScriptOpts.ScriptPrimaryKeys      = CommonFunc.GetAppSettingsBoolValue("ScriptPrimaryKeys");
            smoScriptOpts.ScriptUniqueKeys       = CommonFunc.GetAppSettingsBoolValue("ScriptUniqueKeys");
            smoScriptOpts.ScriptIndexes          = CommonFunc.GetAppSettingsBoolValue("ScriptIndexes");

            LocalizedDefaultValueAttribute stad = new LocalizedDefaultValueAttribute(ConfigurationManager.AppSettings["ScriptTableAndOrData"]);

            smoScriptOpts.ScriptTableAndOrData = (string)stad.Value;

            if (smoScriptOpts.ScriptTableAndOrData.Length == 0)
            {
                if (CommonFunc.GetAppSettingsBoolValue("ScriptData"))
                {
                    smoScriptOpts.ScriptTableAndOrData = Properties.Resources.ScriptOptionsTableSchemaData;
                }
                else
                {
                    smoScriptOpts.ScriptTableAndOrData = Properties.Resources.ScriptOptionsTableSchema;
                }
            }

            smoScriptOpts.ScriptTableTriggers = CommonFunc.GetAppSettingsBoolValue("ScriptTableTriggers");

            smoScriptOpts.ActiveDirectorySP      = CommonFunc.GetAppSettingsBoolValue("ActiveDirectorySP");
            smoScriptOpts.BackupandRestoreTable  = CommonFunc.GetAppSettingsBoolValue("BackupandRestoreTable");
            smoScriptOpts.ChangeDataCapture      = CommonFunc.GetAppSettingsBoolValue("ChangeDataCapture");
            smoScriptOpts.ChangeDataCaptureTable = CommonFunc.GetAppSettingsBoolValue("ChangeDataCaptureTable");

            LocalizedDefaultValueAttribute cc = new LocalizedDefaultValueAttribute(ConfigurationManager.AppSettings["CompatibilityChecks"]);

            smoScriptOpts.CompatibilityChecks = (string)cc.Value;

            smoScriptOpts.DatabaseEngineSP         = CommonFunc.GetAppSettingsBoolValue("DatabaseEngineSP");
            smoScriptOpts.DatabaseMailSP           = CommonFunc.GetAppSettingsBoolValue("DatabaseMailSP");
            smoScriptOpts.DatabaseMaintenancePlan  = CommonFunc.GetAppSettingsBoolValue("DatabaseMaintenancePlan");
            smoScriptOpts.DataControl              = CommonFunc.GetAppSettingsBoolValue("DataControl");
            smoScriptOpts.DistributedQueriesSP     = CommonFunc.GetAppSettingsBoolValue("DistributedQueriesSP");
            smoScriptOpts.FullTextSearchSP         = CommonFunc.GetAppSettingsBoolValue("FullTextSearchSP");
            smoScriptOpts.GeneralExtendedSPs       = CommonFunc.GetAppSettingsBoolValue("GeneralExtendedSPs");
            smoScriptOpts.GeneralTSQL              = CommonFunc.GetAppSettingsBoolValue("GeneralTSQL");
            smoScriptOpts.IntegrationServicesTable = CommonFunc.GetAppSettingsBoolValue("IntegrationServicesTable");
            smoScriptOpts.LogShipping              = CommonFunc.GetAppSettingsBoolValue("LogShipping");
            smoScriptOpts.MetadataFunction         = CommonFunc.GetAppSettingsBoolValue("MetadataFunction");
            smoScriptOpts.OLEAutomationSP          = CommonFunc.GetAppSettingsBoolValue("OLEAutomationSP");
            smoScriptOpts.OLEDBTable                = CommonFunc.GetAppSettingsBoolValue("OLEDBTable");
            smoScriptOpts.ProfilerSP                = CommonFunc.GetAppSettingsBoolValue("ProfilerSP");
            smoScriptOpts.ReplicationSP             = CommonFunc.GetAppSettingsBoolValue("ReplicationSP");
            smoScriptOpts.ReplicationTable          = CommonFunc.GetAppSettingsBoolValue("ReplicationTable");
            smoScriptOpts.RowsetFunction            = CommonFunc.GetAppSettingsBoolValue("RowsetFunction");
            smoScriptOpts.SecurityFunction          = CommonFunc.GetAppSettingsBoolValue("SecurityFunction");
            smoScriptOpts.SecuritySP                = CommonFunc.GetAppSettingsBoolValue("SecuritySP");
            smoScriptOpts.SQLMailSP                 = CommonFunc.GetAppSettingsBoolValue("SQLMailSP");
            smoScriptOpts.SQLServerAgentSP          = CommonFunc.GetAppSettingsBoolValue("SQLServerAgentSP");
            smoScriptOpts.SQLServerAgentTable       = CommonFunc.GetAppSettingsBoolValue("SQLServerAgentTable");
            smoScriptOpts.SystemCatalogView         = CommonFunc.GetAppSettingsBoolValue("SystemCatalogView");
            smoScriptOpts.SystemFunction            = CommonFunc.GetAppSettingsBoolValue("SystemFunction");
            smoScriptOpts.SystemStatisticalFunction = CommonFunc.GetAppSettingsBoolValue("SystemStatisticalFunction");
            smoScriptOpts.Unclassified              = CommonFunc.GetAppSettingsBoolValue("Unclassified");

            return(smoScriptOpts);
        }
        public void ExecuteBCPCommand()
        {
            long rowsCopied    = 0;
            long totalUploaded = 0;
            long chunkSize     = Convert.ToInt32(ConfigurationManager.AppSettings["ChunkSize"], CultureInfo.InvariantCulture);
            int  sleep         = Convert.ToInt32(ConfigurationManager.AppSettings["TimeBetweenChunks"], CultureInfo.InvariantCulture);
            int  cnt           = 0;

            lock (_stringBuilderLock)
            {
                _bcpOutput.Remove(0, _bcpOutput.Length);
            }

            AsyncBCPJobEventArgs args = new AsyncBCPJobEventArgs(null, CurrentJobInfo.JobStatus, null, "", Color.DarkSlateBlue);

            CurrentJobInfo.JobStatus = CommandStatus.InProcess;
            args.ClearRTB            = true;

            NameValueCollection englishLanguage = (NameValueCollection)ConfigurationManager.GetSection("en-US");
            NameValueCollection defaultLanguage = (NameValueCollection)ConfigurationManager.GetSection(Thread.CurrentThread.CurrentCulture.Name);

            if (defaultLanguage == null)
            {
                defaultLanguage = englishLanguage;
                if (defaultLanguage == null)
                {
                    args.DisplayText  = CommonFunc.FormatString(Properties.Resources.ErrorLanguageSectionNotFound, Application.ExecutablePath);
                    args.DisplayColor = Color.Red;
                    args.Status       = CurrentJobInfo.JobStatus = CommandStatus.Failed;
                    UpdateBCPJobStatus(args);
                    return;
                }
            }

            string uploadTo = CurrentJobInfo.TableName;

            if (CurrentJobInfo.FederationInfo != null)
            {
                Match db = Regex.Match(CurrentJobInfo.BCPUploadCommand, "system[\\-a-z0-9]*");
                foreach (FederationMemberDistribution member in CurrentJobInfo.FederationInfo.Members)
                {
                    if (member.DatabaseName.Equals(db.Value))
                    {
                        uploadTo = CommonFunc.FormatString(Properties.Resources.FederationMemberInfo, CurrentJobInfo.TableName, CurrentJobInfo.FederationInfo.FederationName, member.Member_ID, member.ToString());
                        break;
                    }
                }
            }

            args.DisplayText  = DateTime.Now.ToString(CultureInfo.CurrentCulture) + Environment.NewLine + CommonFunc.FormatString(Properties.Resources.BCPUploadingData, uploadTo, CurrentJobInfo.BCPUploadCommand) + Environment.NewLine;
            args.DisplayColor = Color.DarkSlateBlue;
            UpdateBCPJobStatus(args);
            args.ClearRTB = false;

            for (long i = 0; i < (CurrentJobInfo.NumberOfRows / chunkSize) + 1; i++)
            {
                string errorMsg = "";
                bool   retry    = true;
                int    retryCnt = 0;
                long   start    = i * chunkSize + 1;
                long   end      = Math.Min((i + 1) * chunkSize, CurrentJobInfo.NumberOfRows);

                // Don't sleep on first chunk.
                if (i > 0)
                {
                    Thread.Sleep(sleep);
                }

                int endOfBCPCmd = CurrentJobInfo.BCPUploadCommand.IndexOf(ConfigurationManager.AppSettings["BCPExe"] + " ");
                if (endOfBCPCmd > -1)
                {
                    endOfBCPCmd = ConfigurationManager.AppSettings["BCPExe"].Length;
                }
                else
                {
                    System.Text.RegularExpressions.Match bcpExe = Regex.Match(CurrentJobInfo.BCPUploadCommand, @"\s[""[]");
                    if (bcpExe.Success)
                    {
                        endOfBCPCmd = bcpExe.Index;
                    }
                    else
                    {
                        endOfBCPCmd = CurrentJobInfo.BCPUploadCommand.IndexOf(" ");
                    }
                }

                while (retry && start <= end)
                {
                    using (Process p = new Process())
                    {
                        p.StartInfo.FileName = CurrentJobInfo.BCPUploadCommand.Substring(0, endOfBCPCmd);
                        if (CurrentJobInfo.NumberOfRows < chunkSize)
                        {
                            p.StartInfo.Arguments = CurrentJobInfo.BCPUploadCommand.Substring(endOfBCPCmd + 1);
                        }
                        else
                        {
                            p.StartInfo.Arguments = CurrentJobInfo.BCPUploadCommand.Substring(endOfBCPCmd + 1) + @" -F" + start.ToString(CultureInfo.InvariantCulture) + @" -L" + end.ToString(CultureInfo.InvariantCulture);
                        }

                        p.StartInfo.CreateNoWindow         = true;
                        p.StartInfo.UseShellExecute        = false;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(defaultLanguage["BCPCodePage"]);
                        p.SynchronizingObject = this;
                        p.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputHandler);
                        p.Start();
                        p.BeginOutputReadLine();

                        while (!p.HasExited)
                        {
                            if (AsyncProcessingStatus.CancelProcessing)
                            {
                                p.Kill();
                                p.Close();
                                CurrentJobInfo.JobStatus = CommandStatus.Failed;
                                return;
                            }

                            // This is the keep alive for SQL Azure connection.  SQL Azure times out after 5 min if it sees no use.

                            if (cnt++ > 60)
                            {
                                args.DisplayText = "*" + Environment.NewLine;
                                UpdateBCPJobStatus(args);
                                cnt = 0;
                            }
                            else
                            {
                                args.DisplayColor = args.DisplayColor == Color.Red ? Color.DarkSlateBlue : Color.Red;
                                args.DisplayText  = "*";
                                UpdateBCPJobStatus(args);
                            }
                            p.WaitForExit(1000);
                        }
                        p.Close();
                    }
                    args.DisplayColor = Color.DarkSlateBlue;

                    // Look for error.

                    Match error = Regex.Match(GetThreadSafeBCPOutputString(), defaultLanguage["BCPSQLState"]);
                    if (!error.Success)
                    {
                        error = Regex.Match(GetThreadSafeBCPOutputString(), englishLanguage["BCPSQLState"]);
                    }

                    if (error.Success)
                    {
                        // Check for Connection-Loss Errors.  If it is not a connection loss error, then don't bother to retry

                        if (!Regex.IsMatch(GetThreadSafeBCPOutputString(), CommonFunc.GetAppSettingsStringValue("BCPSQLAzureErrorCodesRetry")))
                        {
                            retry             = false;
                            args.DisplayColor = Color.Red;
                            args.DisplayText  = Environment.NewLine + DateTime.Now.ToString(CultureInfo.CurrentCulture) + Properties.Resources.Error1 + Environment.NewLine + Environment.NewLine + GetThreadSafeBCPOutputString() + Environment.NewLine;
                            args.Status       = CurrentJobInfo.JobStatus = CommandStatus.Failed;
                            UpdateBCPJobStatus(args);
                            continue;
                        }

                        if (retryCnt == 0)
                        {
                            // Save off origial error message

                            errorMsg = GetThreadSafeBCPOutputString();
                        }

                        // Ok, found error.  Lets see if any data was sent

                        // Example Error
                        //  5000 rows sent to SQL Server. Total sent: 155000
                        //  SQLState = S1000, NativeError = 21
                        //  Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Warning: Fatal error 40501 occurred at Oct 30 2009  4:15PM. Note the error and time, and contact your system administrator.
                        //  BCP copy in failed

                        Match sent = Regex.Match(GetThreadSafeBCPOutputString(), defaultLanguage["BCPTotalSent"]);
                        if (sent.Success)
                        {
                            Match recordsSent = Regex.Match(sent.Value, defaultLanguage["BCPNumber"]);
                            if (recordsSent.Success)
                            {
                                int totalSent = Convert.ToInt32(recordsSent.Value, CultureInfo.InvariantCulture); // Ok, we had data sent.  Get the number of rows sent.
                                start      += totalSent;                                                          // Now, we need to ofset start to start after last successful batch
                                rowsCopied += totalSent;                                                          // Add totalSent this batch to rows copied so far.
                            }
                        }
                        else
                        {
                            sent = Regex.Match(GetThreadSafeBCPOutputString(), englishLanguage["BCPTotalSent"]);
                            if (sent.Success)
                            {
                                Match recordsSent = Regex.Match(sent.Value, englishLanguage["BCPNumber"]);
                                if (recordsSent.Success)
                                {
                                    int totalSent = Convert.ToInt32(recordsSent.Value, CultureInfo.InvariantCulture); // Ok, we had data sent.  Get the number of rows sent.
                                    start      += totalSent;                                                          // Now, we need to ofset start to start after last successful batch
                                    rowsCopied += totalSent;                                                          // Add totalSent this batch to rows copied so far.
                                }
                            }
                        }

                        if (retryCnt++ > 2)
                        {
                            // Give up
                            retry             = false;
                            args.DisplayColor = Color.Red;
                            args.Status       = CurrentJobInfo.JobStatus = CommandStatus.Failed;
                            args.DisplayText  = Environment.NewLine + DateTime.Now.ToString(CultureInfo.CurrentCulture) + Properties.Resources.BCPProcessFailed + Environment.NewLine + Environment.NewLine + errorMsg + Environment.NewLine;
                            UpdateBCPJobStatus(args);
                        }
                        else
                        {
                            Thread.Sleep(sleep);
                        }
                    }
                    else
                    {
                        string bcpSum  = Environment.NewLine;
                        Match  summary = Regex.Match(GetThreadSafeBCPOutputString(), defaultLanguage["BCPSummary"]);
                        if (summary.Success)
                        {
                            bcpSum = Environment.NewLine + summary.Value + Environment.NewLine;
                        }
                        else
                        {
                            summary = Regex.Match(GetThreadSafeBCPOutputString(), englishLanguage["BCPSummary"]);
                            if (summary.Success)
                            {
                                bcpSum = Environment.NewLine + summary.Value + Environment.NewLine;
                            }
                        }

                        retry         = false;
                        totalUploaded = end;

                        rowsCopied += end - start + 1;

                        args.DisplayText = Environment.NewLine + DateTime.Now.ToString(CultureInfo.CurrentCulture) + " --> " +
                                           string.Format(Properties.Resources.BCPProcessingUpdate + bcpSum,
                                                         rowsCopied, CurrentJobInfo.NumberOfRows, decimal.Round((decimal)((float)rowsCopied / (float)CurrentJobInfo.NumberOfRows * 100.0), 2));
                        UpdateBCPJobStatus(args);
                    }

                    lock (_stringBuilderLock)
                    {
                        _bcpOutput.Length = 0;
                    }
                }
            }

            lock (_stringBuilderLock)
            {
                _bcpOutput.Length = 0;
            }

            args.DisplayText = Environment.NewLine;

            if (CurrentJobInfo.JobStatus == CommandStatus.InProcess)
            {
                args.Status = CurrentJobInfo.JobStatus = CommandStatus.Success;

                NotifyJobIsFinished();
            }

            UpdateBCPJobStatus(args);
        }