Exemplo n.º 1
0
        /// <summary>
        /// Use a ConnectionInfo item to create a connected binding context
        /// </summary>
        /// <param name="connInfo">Connection info used to create binding context</param>
        /// <param name="overwrite">Overwrite existing context</param>
        public virtual string AddConnectionContext(ConnectionInfo connInfo, string featureName = null, bool overwrite = false)
        {
            if (connInfo == null)
            {
                return(string.Empty);
            }

            // lookup the current binding context
            string connectionKey = GetConnectionContextKey(connInfo);

            if (BindingContextExists(connectionKey))
            {
                if (overwrite)
                {
                    RemoveBindingContext(connectionKey);
                }
                else
                {
                    // no need to populate the context again since the context already exists
                    return(connectionKey);
                }
            }
            IBindingContext bindingContext = this.GetOrCreateBindingContext(connectionKey);

            if (bindingContext.BindingLock.WaitOne())
            {
                try
                {
                    bindingContext.BindingLock.Reset();
                    SqlConnection sqlConn = connectionOpener.OpenSqlConnection(connInfo, featureName);

                    // populate the binding context to work with the SMO metadata provider
                    bindingContext.ServerConnection = new ServerConnection(sqlConn);

                    if (this.needsMetadata)
                    {
                        bindingContext.SmoMetadataProvider         = SmoMetadataProvider.CreateConnectedProvider(bindingContext.ServerConnection);
                        bindingContext.MetadataDisplayInfoProvider = new MetadataDisplayInfoProvider();
                        bindingContext.MetadataDisplayInfoProvider.BuiltInCasing =
                            this.CurrentSettings.SqlTools.IntelliSense.LowerCaseSuggestions.Value
                                ? CasingStyle.Lowercase : CasingStyle.Uppercase;
                        bindingContext.Binder = BinderProvider.CreateBinder(bindingContext.SmoMetadataProvider);
                    }

                    bindingContext.BindingTimeout = ConnectedBindingQueue.DefaultBindingTimeout;
                    bindingContext.IsConnected    = true;
                }
                catch (Exception)
                {
                    bindingContext.IsConnected = false;
                }
                finally
                {
                    bindingContext.BindingLock.Set();
                }
            }

            return(connectionKey);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Use a ConnectionInfo item to create a connected binding context
        /// </summary>
        /// <param name="connInfo"></param>
        public virtual string AddConnectionContext(ConnectionInfo connInfo)
        {
            if (connInfo == null)
            {
                return(string.Empty);
            }

            // lookup the current binding context
            string          connectionKey  = GetConnectionContextKey(connInfo);
            IBindingContext bindingContext = this.GetOrCreateBindingContext(connectionKey);

            if (bindingContext.BindingLock.WaitOne())
            {
                try
                {
                    bindingContext.BindingLock.Reset();

                    // increase the connection timeout to at least 30 seconds and and build connection string
                    // enable PersistSecurityInfo to handle issues in SMO where the connection context is lost in reconnections
                    int? originalTimeout             = connInfo.ConnectionDetails.ConnectTimeout;
                    bool?originalPersistSecurityInfo = connInfo.ConnectionDetails.PersistSecurityInfo;
                    connInfo.ConnectionDetails.ConnectTimeout      = Math.Max(DefaultMinimumConnectionTimeout, originalTimeout ?? 0);
                    connInfo.ConnectionDetails.PersistSecurityInfo = true;
                    string connectionString = ConnectionService.BuildConnectionString(connInfo.ConnectionDetails);
                    connInfo.ConnectionDetails.ConnectTimeout      = originalTimeout;
                    connInfo.ConnectionDetails.PersistSecurityInfo = originalPersistSecurityInfo;

                    // open a dedicated binding server connection
                    SqlConnection sqlConn = new SqlConnection(connectionString);
                    sqlConn.Open();

                    // populate the binding context to work with the SMO metadata provider
                    ServerConnection serverConn = new ServerConnection(sqlConn);
                    bindingContext.SmoMetadataProvider         = SmoMetadataProvider.CreateConnectedProvider(serverConn);
                    bindingContext.MetadataDisplayInfoProvider = new MetadataDisplayInfoProvider();
                    bindingContext.MetadataDisplayInfoProvider.BuiltInCasing =
                        this.CurrentSettings.SqlTools.IntelliSense.LowerCaseSuggestions.Value
                            ? CasingStyle.Lowercase : CasingStyle.Uppercase;
                    bindingContext.Binder           = BinderProvider.CreateBinder(bindingContext.SmoMetadataProvider);
                    bindingContext.ServerConnection = serverConn;
                    bindingContext.BindingTimeout   = ConnectedBindingQueue.DefaultBindingTimeout;
                    bindingContext.IsConnected      = true;
                }
                catch (Exception)
                {
                    bindingContext.IsConnected = false;
                }
                finally
                {
                    bindingContext.BindingLock.Set();
                }
            }

            return(connectionKey);
        }