Ejemplo n.º 1
0
        public int GetNumOfOpenConnection(string dbName)
        {
            int outVal = 0;

            lock (_dbConnection)
            {
                if (!_dbConnection.IsDisposed)
                {
                    string sqlCommandStr = _dBScriptsProvider.GetNumOfOpenConnectionScript(dbName);

                    using (DataTable resultsTable = _dbConnection.GetSelectCommand(sqlCommandStr, 10))
                    {
                        if (resultsTable.Rows.Count > 0)
                        {
                            DataRow statusRow = resultsTable.Rows[0];
                            outVal = Convert.ToInt32(statusRow["NumberOfConnections"], CultureInfo.InvariantCulture);
                        }
                    }
                }
            }

            return(outVal);
        }
Ejemplo n.º 2
0
        public DataSet GetScriptsExecutionHistoryTableStructureFromDB()
        {
            DataSet dsExecutionHistory = new DataSet();

            string    sqlCmdStr = _dBScriptsProvider.GetEmptyTableScript(DBCommandsConsts.DBScriptsExecutionHistoryFullTableName);
            DataTable dbScriptsExecutionHistoryTable = _dbConnection.GetSelectCommand(sqlCmdStr);

            dbScriptsExecutionHistoryTable.TableName = DBCommandsConsts.DBScriptsExecutionHistoryFullTableName;
            dsExecutionHistory.Tables.Add(dbScriptsExecutionHistoryTable);


            sqlCmdStr = _dBScriptsProvider.GetEmptyTableScript(DBCommandsConsts.DBScriptsExecutionHistoryFilesFullTableName);
            DataTable dbScriptsExecutionHistoryFilesTable = _dbConnection.GetSelectCommand(sqlCmdStr);

            dbScriptsExecutionHistoryFilesTable.TableName = DBCommandsConsts.DBScriptsExecutionHistoryFilesFullTableName;
            dsExecutionHistory.Tables.Add(dbScriptsExecutionHistoryFilesTable);


            return(dsExecutionHistory);
        }
        private void ResolveDBInSingleUserMode(string dbName, string dbFilesBasePath)
        {
            bool isDBInSigleUserMode = false;

            string sqlCommandStr = _dBScriptsProvider.GetDBAccessStateScript(dbName);

            using (DataTable dbStateTable = _dbConnection.GetSelectCommand(sqlCommandStr))
            {
                isDBInSigleUserMode = dbStateTable.Rows.Count > 0;
            }

            if (isDBInSigleUserMode)
            {
                sqlCommandStr = _dBScriptsProvider.GetDBSessionsScript(dbName);

                using (DataTable sessionsTable = _dbConnection.GetSelectCommand(sqlCommandStr))
                {
                    foreach (DataRow rowSession in sessionsTable.Rows)
                    {
                        int seesionID = Convert.ToInt32(rowSession["SessionID"], CultureInfo.InvariantCulture);

                        sqlCommandStr = _dBScriptsProvider.KillSessionScript(seesionID.ToString(CultureInfo.InvariantCulture));

                        _dbConnection.ExecSQLCommandStr(sqlCommandStr);
                    }
                }

                //Comment: we prefer to drop db and create again becauase if the db stuck on restore state we couldnt change it to MULTI_USER
                //sqlCmdStr2 = $"ALTER DATABASE [{dbName}] SET MULTI_USER ";
                //_sqlServerConnectionManager.ExecSQLCommandStr(sqlCmdStr2);

                DropDB(dbName);

                CreateDB(dbName, dbFilesBasePath);
            }
            else
            {
                sqlCommandStr = _dBScriptsProvider.GetIsDBExsitScript(dbName);

                using (DataTable dtIsDBExsit = _dbConnection.GetSelectCommand(sqlCommandStr))
                {
                    if (dtIsDBExsit.Rows.Count == 0)
                    {
                        //sqlCmdStr2 = @"DECLARE @dataFilePath NVARCHAR(MAX) = CAST(SERVERPROPERTY('InstanceDefaultDataPath') AS NVARCHAR) + FORMATMESSAGE('\%s.mdf', 'MASTER'); SELECT @dataFilePath ;";
                        //DataTable DT = _sqlServerConnectionManager.GetSelectCommand(sqlCmdStr2);

                        if (!string.IsNullOrWhiteSpace(dbFilesBasePath))
                        {
                            string mdfFilePath = Path.Combine(dbFilesBasePath, $"{dbName}.mdf");
                            if (File.Exists(mdfFilePath))
                            {
                                File.Delete(mdfFilePath);
                            }

                            string ldfFilePath = Path.Combine(dbFilesBasePath, $"{dbName}.ldf");
                            if (File.Exists(ldfFilePath))
                            {
                                File.Delete(ldfFilePath);
                            }
                        }

                        CreateDB(dbName, dbFilesBasePath);
                    }
                }
            }
        }