예제 #1
0
        /// <summary>
        /// Get a SMO Server object that is connected to the connection
        /// </summary>
        /// <param name="ci">Conenction info</param>
        /// <returns>Smo Server object for the connection</returns>
        public static Microsoft.SqlServer.Management.Smo.Server GetSmoServer(IManagedConnection mc)
        {
            SqlOlapConnectionInfoBase ci = mc.Connection;

            if (ci == null)
            {
                throw new ArgumentNullException("ci");
            }

            SMO.Server server = null;

            // see what type of connection we have been passed
            SqlConnectionInfoWithConnection ciWithCon = ci as SqlConnectionInfoWithConnection;

            if (ciWithCon != null)
            {
                server = new SMO.Server(ciWithCon.ServerConnection);
            }
            else
            {
                SqlConnectionInfo sqlCi = ci as SqlConnectionInfo;
                if (sqlCi != null)
                {
                    server = new SMO.Server(new ServerConnection(sqlCi));
                }
            }

            if (server == null)
            {
                throw new InvalidOperationException();
            }
            return(server);
        }
예제 #2
0
        /// <summary>
        /// create a new managed connection.
        /// </summary>
        /// <param name="connection">connection</param>
        /// <param name="attemptToPool">true if we are going to try and reuse the
        /// connection if possible</param>
        public ManagedConnection(SqlOlapConnectionInfoBase sourceConnection, bool attemptToPool)
        {
            // parameter check
            if (sourceConnection == null)
            {
                throw new ArgumentNullException("sourceConnection");
            }

            // see if the connection can restrict access (single user mode)
            IRestrictedAccess access = sourceConnection as IRestrictedAccess;
            // see if it is cloneable
            ICloneable cloneable = sourceConnection as ICloneable;

            lock (ActiveConnections)
            {
                // if it's not single user mode then we can see if the object can be cloned
                if (access == null || access.SingleConnection == false)
                {
                    // if we are going to attempt to pool, see if the connection is in use
                    if (attemptToPool && !ActiveConnections.Contains(SharedConnectionUtil.GetConnectionKeyName(sourceConnection)))
                    {
                        // add it to the hashtable to indicate use.
                        ActiveConnections.Add(SharedConnectionUtil.GetConnectionKeyName(sourceConnection), sourceConnection);
                        this.connection     = sourceConnection;
                        this.closeOnDispose = false;
                        this.connectionAddedToActiveConnections = true;
                    }
                    else if (cloneable != null)
                    {
                        this.connection     = (SqlOlapConnectionInfoBase)cloneable.Clone();
                        this.closeOnDispose = true;
                    }
                    else if (sourceConnection is SqlConnectionInfoWithConnection)
                    {
                        this.connection     = ((SqlConnectionInfoWithConnection)sourceConnection).Copy();
                        this.closeOnDispose = true;
                    }
                }
            }
            // if everything else has failed just use to passed in connection.
            if (this.connection == null)
            {
                this.connection = sourceConnection;
            }

            // always set the lock timeout to prevent the shell from not responding
            if (this.connection is SqlConnectionInfoWithConnection)
            {
                // set lock_timeout to 10 seconds
                ((SqlConnectionInfoWithConnection)this.connection).ServerConnection.LockTimeout = 10;
            }
        }
예제 #3
0
        /// <summary>
        /// stores specified connection info and performs some extra initialization steps
        /// that can only be done after we have the connection information
        /// </summary>
        /// <param name="ci"></param>
        private void ApplyConnectionInfo(SqlOlapConnectionInfoBase ci, bool ownConnection)
        {
            this.connectionInfo = ci;
            this.ownConnection  = ownConnection;

            //cache the cast value. It is OK that it is null for non SQL types
            this.sqlCiWithConnection = ci as SqlConnectionInfoWithConnection;

            if (this.sqlCiWithConnection != null)
            {
                //we want to be notified if it is closed
                this.sqlCiWithConnection.ConnectionClosed += new EventHandler(OnSqlConnectionClosed);
            }
        }
예제 #4
0
        public AgentAction(XmlDocument document, IServiceProvider source, object actionObject)
        {
            // parameter check
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            if (actionObject != null)
            {
                this.ActionObject = actionObject;
            }

            // get the managed connection
            managedConnection = source.GetService(typeof(IManagedConnection)) as IManagedConnection;

            // get the connection
            SqlOlapConnectionInfoBase ci = managedConnection.Connection;
            // get the server connection
            ServerConnection serverConnection =
                ((SqlConnectionInfoWithConnection)managedConnection.Connection).ServerConnection;

            smoServer = new Microsoft.SqlServer.Management.Smo.Server(serverConnection);

            // get the list or urn's that have been passed in
            param = new STParameters(document);
            StringCollection urnStrings = new StringCollection();

            // get a list of urns that have been passed in.
            param.GetParam("urn", urnStrings);

            // store the Urn's as real Urns
            urnParameters = new Urn[urnStrings.Count];
            for (int i = 0; i < urnStrings.Count; i++)
            {
                urnParameters[i] = new Urn(urnStrings[i]);
            }
        }
예제 #5
0
        private SqlConnectionInfo GetCurrentSqlConnectionInfo(string databaseName)
        {
            CurrentlyActiveWndConnectionInfo connectionInfo = ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo;

            databaseName = databaseName ?? connectionInfo.UIConnectionInfo.AdvancedOptions["DATABASE"];

            if (String.IsNullOrEmpty(databaseName))
            {
                throw new ConnectionInfoException("No database context");
            }

            SqlOlapConnectionInfoBase connectionBase = UIConnectionInfoUtil.GetCoreConnectionInfo(connectionInfo.UIConnectionInfo);

            SqlConnectionInfo sqlConnectionInfo = (SqlConnectionInfo)connectionBase;

            sqlConnectionInfo.DatabaseName = databaseName;

            return(sqlConnectionInfo);
        }
예제 #6
0
        /// <summary>
        /// Close the current connection if applicable.
        /// </summary>
        public void Close()
        {
            if (this.closed)
            {
                return;
            }

            if (this.closeOnDispose)
            {
                IDisposable disp = this.connection as IDisposable;
                if (disp != null)
                {
                    disp.Dispose();
                }
            }
            else
            {
                // if we are not closing the connection and it is a sql connection then ensure it
                // is left in the master database.
                SqlConnectionInfoWithConnection sqlConnection = this.connection as SqlConnectionInfoWithConnection;
                if (sqlConnection != null && sqlConnection.ServerConnection.DatabaseEngineType == DatabaseEngineType.Standalone)
                {
                    try
                    {
                        sqlConnection.ServerConnection.ExecuteNonQuery("use [master]");
                    }
                    // don't error if this fails
                    catch
                    { }
                }
            }
            if (this.connectionAddedToActiveConnections)
            {
                lock (ActiveConnections)
                {
                    ActiveConnections.Remove(SharedConnectionUtil.GetConnectionKeyName(connection));
                }
            }

            this.connection = null;
            this.closed     = true;
        }
예제 #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dataContainer">Data container</param>
        /// <param name="xmlParameters">XML string with parameters</param>
        public CDataContainer(CDataContainer dataContainer, string xmlParameters)
        {
            Server              = dataContainer.Server;
            this.serverName     = dataContainer.serverName;
            this.serverType     = dataContainer.serverType;
            this.connectionInfo = dataContainer.connectionInfo;
            this.ownConnection  = dataContainer.ownConnection;

            this.sqlCiWithConnection = dataContainer.connectionInfo as SqlConnectionInfoWithConnection;
            if (this.sqlCiWithConnection != null)
            {
                //we want to be notified if it is closed
                this.sqlCiWithConnection.ConnectionClosed += new EventHandler(OnSqlConnectionClosed);
            }

            if (xmlParameters != null)
            {
                XmlDocument doc = GenerateXmlDocumentFromString(xmlParameters);
                this.Init(doc);
            }
        }
예제 #8
0
 /// <summary>
 /// Create a new managed connection
 /// </summary>
 /// <param name="connection">connection wish to manage</param>
 public ManagedConnection(SqlOlapConnectionInfoBase connection)
     : this(connection, false)
 {
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ci"></param>
        /// <returns></returns>
        public static string GetConnectionKeyName(SqlOlapConnectionInfoBase ci)
        {
            //// Note that these strings are not localized. The returned string is used by OE in a
            //// hash of connections so it can tell if it already has such a connection open. This
            //// string is never seen by the user. For the string seen by the user, see
            //// ServerNameHandler.cs.
            string displayName = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} (", ci.ServerName);

            if (!string.IsNullOrEmpty(ci.DatabaseName))
            {
                displayName += ", " + ci.DatabaseName;
            }

            return(displayName);

            //switch (ci.ServerType)
            //{
            //    case ConnectionType.AzureStorage:
            //        AzureStorageConnectionInfo azureCI = ci as AzureStorageConnectionInfo;
            //        displayName = "AzureStorage," + azureCI.BlobClient.BaseUri;
            //        break;
            //    case ConnectionType.AzureAccount:
            //        if (ci is CertificateBasedAuthenticationInfo)
            //        {
            //            displayName = "AzureSubscription," + (ci as CertificateBasedAuthenticationInfo).SubscriptionId;
            //        }
            //        else
            //        {
            //            displayName = "AzureSubscription";
            //        }
            //        break;
            //    case ConnectionType.Sql:
            //        displayName += "SQLServer";
            //        SqlConnectionInfo sqlCi = ci as SqlConnectionInfo;
            //        if (sqlCi.UseIntegratedSecurity == true)
            //        {
            //            displayName += ", trusted";
            //        }
            //        else
            //        {
            //            displayName += String.Format(System.Globalization.CultureInfo.InvariantCulture, ", user = {0}", sqlCi.UserName);
            //            //In Cloud a user can have access to only a few UDBs without access to master DB
            //            // and hence need to show different OE hierarchy trees for each DB
            //            //Same is the case with a contained user.


            //            if (ServerInfoCache.GetDatabaseEngineType(ci.ServerName) == DatabaseEngineType.SqlAzureDatabase
            //                || SFC.ExecuteSql.GetDatabaseEngineType(ci) == DatabaseEngineType.SqlAzureDatabase
            //                || SFC.ExecuteSql.IsContainedAuthentication(ci))
            //            {
            //                if (!string.IsNullOrEmpty(ci.DatabaseName))
            //                {
            //                    displayName += ", " + ci.DatabaseName;
            //                }
            //            }
            //        }
            //        break;
            //    case ConnectionType.Olap:
            //        displayName += "OLAP";
            //        break;
            //    case ConnectionType.SqlCE:
            //        displayName += "SqlCE";
            //        break;
            //    case ConnectionType.ReportServer:
            //        displayName += "Rs";
            //        displayName += String.Format(System.Globalization.CultureInfo.InvariantCulture, ", connection = {0}", ci.ConnectionString);
            //        break;
            //    case ConnectionType.IntegrationServer:
            //        displayName += "SSIS";
            //        break;
            //}
            //displayName += ")";
            //return displayName;
        }