public static void Process()
        {
            TargetProcessor   tp           = new TargetProcessor();
            AsyncUpdateStatus updateStatus = new AsyncUpdateStatus(TargetAsyncUpdateStatusHandler);
            string            sqlToExecute = CommonFunc.GetTextFromFile(_FileToProcess);

            _OutputResultsDirectory = _OutputResultsFile.Substring(0, _OutputResultsFile.LastIndexOf('\\') + 1);

            TargetServerInfo tsi = new TargetServerInfo();

            tsi.ServerInstance = _TargetServerName;
            tsi.TargetDatabase = _TargetDatabase;

            if (Regex.IsMatch(_TargetServerName, CommonFunc.GetAppSettingsStringValue("RegexSearchForAzureServer"), RegexOptions.IgnoreCase))
            {
                tsi.ServerType = ServerTypes.SQLAzure;
            }
            else
            {
                tsi.ServerType = ServerTypes.SQLServer;
            }

            if (_bTargetConnectNTAuth == true)
            {
                // Use Windows authentication
                tsi.LoginSecure = true;
            }
            else
            {
                // Use SQL Server authentication
                tsi.LoginSecure = false;
                tsi.Login       = _TargetUserName;
                tsi.Password    = _TargetPassword;
            }

            //AsyncProcessingStatus.FinishedProcessingJobs = true;
            AsyncQueueBCPJob queueBCPJob = new AsyncQueueBCPJob(AsyncQueueJobHandler);

            tp.CreateDatabase(tsi, _Collation, _TargetEdition, _TargetDatabaseSize, _bDropExistingDatabase);
            tp.ExecuteSQLonTarget(tsi, updateStatus, queueBCPJob, sqlToExecute);
        }
Exemplo n.º 2
0
        public static void Process()
        {
            TargetProcessor   tp           = new TargetProcessor();
            AsyncUpdateStatus updateStatus = new AsyncUpdateStatus(TargetAsyncUpdateStatusHandler);
            string            sqlToExecute = CommonFunc.GetTextFromFile(_FileToProcess);

            _OutputResultsDirectory = _OutputResultsFile.Substring(0, _OutputResultsFile.LastIndexOf('\\') + 1);

            TargetServerInfo tsi = new TargetServerInfo();

            tsi.ServerInstance = _TargetServerName;
            tsi.TargetDatabase = _TargetDatabase;

            if (_bTargetConnectNTAuth == true)
            {
                // Use Windows authentication
                tsi.LoginSecure = true;
            }
            else
            {
                // Use SQL Server authentication
                tsi.LoginSecure = false;
                tsi.Login       = _TargetUserName;
                tsi.Password    = _TargetPassword;
            }

            tsi.Version = "";
            try
            {
                Retry.ExecuteRetryAction(() =>
                {
                    using (SqlConnection con = new SqlConnection(tsi.ConnectionStringRootDatabase))
                    {
                        ScalarResults sr = SqlHelper.ExecuteScalar(con, CommandType.Text, "SELECT @@VERSION");
                        string version   = (string)sr.ExecuteScalarReturnValue;
                        if (version.IndexOf("Azure") > 0)
                        {
                            tsi.ServerType = ServerTypes.AzureSQLDB;
                        }
                        else
                        {
                            tsi.ServerType = ServerTypes.SQLServer;
                        }
                        Match result = Regex.Match(version, @"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+");
                        if (result.Success)
                        {
                            tsi.Version = result.Value;
                        }
                        else
                        {
                            Console.WriteLine(CommonFunc.FormatString(Properties.Resources.ErrorGettingSQLVersion, version));
                            return;
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return;
            }

            int dbMajor = Convert.ToInt32(tsi.Version.Substring(0, 2));

            //ok, we don't want to break the old version where they just passed in an int value without the "MB" or "GB".  So, we
            //can guess based on the edition what it should be.

            //web = 100 MB, 1 GB, 5 GB
            //business = 10 GB, 20 GB, 30 GB, 40 GB, 50 GB, 100 GB, or 150 GB
            //basic = 100 MB, 500 MB, 1 GB, 2 GB
            //standard = 100 MB, 500 MB, 1 GB, 2 GB, 5 GB, 10 GB, 20 GB, 30 GB, 40 GB, 50 GB, 100 GB, 150 GB, 200 GB, or 250 GB
            //premium = 100 MB, 500 MB, 1 GB, 2 GB, 5 GB, 10 GB, 20 GB, 30 GB, 40 GB, 50 GB, 100 GB, 150 GB, 200 GB, 250 GB, 300 GB, 400 GB, or 500 GB

            if (tsi.ServerType == ServerTypes.AzureSQLDB)
            {
                if (_TargetEdition == "web")
                {
                    if (dbMajor > 11)
                    {
                        Console.WriteLine(CommonFunc.FormatString(Properties.Resources.InvalidDatabaseEdition1V12, _TargetEdition));
                        return;
                    }

                    if (_TargetDatabaseSize[_TargetDatabaseSize.Length - 1] != 'B')
                    {
                        if (_TargetDatabaseSize == "1")
                        {
                            _TargetDatabaseSize = "1 GB";
                        }
                        else if (_TargetDatabaseSize == "5")
                        {
                            _TargetDatabaseSize = "5 GB";
                        }
                        else
                        {
                            _TargetDatabaseSize = "100 MB";
                        }
                    }
                }
                else if (_TargetEdition == "business")
                {
                    if (dbMajor > 11)
                    {
                        Console.WriteLine(CommonFunc.FormatString(Properties.Resources.InvalidDatabaseEdition1V12, _TargetEdition));
                        return;
                    }

                    if (_TargetDatabaseSize[_TargetDatabaseSize.Length - 1] != 'B')
                    {
                        _TargetDatabaseSize = _TargetDatabaseSize + " GB";
                    }
                }
                else if (_TargetEdition == "standard")
                {
                    if (dbMajor > 11)
                    {
                        if (!Regex.IsMatch(_TargetDatabasePerforance, "S[0-3]"))
                        {
                            Console.WriteLine(CommonFunc.FormatString(Properties.Resources.InvalidPerformanceLevelV12, _TargetDatabasePerforance, _TargetEdition));
                            return;
                        }
                    }
                    else
                    {
                        if (!Regex.IsMatch(_TargetDatabasePerforance, "S[0-2]"))
                        {
                            Console.WriteLine(CommonFunc.FormatString(Properties.Resources.InvalidPerformanceLevel, _TargetDatabasePerforance, _TargetEdition));
                            return;
                        }
                    }
                }
                else if (_TargetEdition == "premium")
                {
                    if (!Regex.IsMatch(_TargetDatabasePerforance, "P[1-3]"))
                    {
                        Console.WriteLine(CommonFunc.FormatString(Properties.Resources.InvalidPerformanceLevel, _TargetDatabasePerforance, _TargetEdition));
                        return;
                    }
                }
            }

            if (dbMajor == 12)
            {
                // Note with dbMajor 12 and above, disk size no longer matters
                _TargetDatabaseSize = "";
            }
            else
            {
                if (!ValidateDatabaseSize(_TargetEdition, _TargetDatabaseSize))
                {
                    Console.WriteLine(CommonFunc.FormatString(Properties.Resources.InvalidDatabaseSize, _TargetEdition, _TargetDatabaseSize));
                    return;
                }
            }

            //AsyncProcessingStatus.FinishedProcessingJobs = true;
            AsyncQueueBCPJob     queueBCPJob = new AsyncQueueBCPJob(AsyncQueueJobHandler);
            DatabaseCreateStatus dcs         = tp.CreateDatabase(tsi, _Collation, _TargetEdition, _TargetDatabaseSize, _TargetDatabasePerforance, _bDropExistingDatabase);

            if (dcs == DatabaseCreateStatus.Waiting)
            {
                Console.Write(Properties.Resources.WaitingForDBCreation);
                while (!tp.DoesDatabaseExist(tsi))
                {
                    Thread.Sleep(2000);
                    Console.Write(".");
                }
                Thread.Sleep(2000);
                Console.Write(Properties.Resources.DBCreated + Environment.NewLine);
            }
            else if (dcs == DatabaseCreateStatus.Failed)
            {
                Console.WriteLine(Properties.Resources.DBCreateFailed);
                return;
            }

            tp.ExecuteSQLonTarget(tsi, updateStatus, queueBCPJob, sqlToExecute);
        }