コード例 #1
0
        /// <summary>
        /// Attempts a connection to the ePrintMonitor database.
        /// If the connection fails, attempts to create the database, then retries the connection.
        /// </summary>
        /// <param name="hostName"></param>
        private void CheckMonitorDatabase(string hostName)
        {
            bool connected = false;
            SqlConnectionStringBuilder connectionBuilder = GetConnectionStringBuilder(hostName, Resources.MonitorDatabase);

            TraceFactory.Logger.Debug("Testing connection to " + connectionBuilder.DataPath());
            try
            {
                connected = ConnectToDatabase(connectionBuilder);
                if (!connected)
                {
                    TraceFactory.Logger.Debug("Unable to connect to " + connectionBuilder.DataPath());
                    TraceFactory.Logger.Debug("Running Creation script.");

                    connectionBuilder.InitialCatalog = "master";
                    ExecuteSql(connectionBuilder.ToString(), Resources.CreateMonitorDatabase);
                    TraceFactory.Logger.Debug("Database creation successful.");

                    //Connect to the newly created database.  It make take a few seconds for it to establish a connection.
                    connectionBuilder.InitialCatalog = Resources.MonitorDatabase;
                    for (int i = 0; i < 10; i++)
                    {
                        TraceFactory.Logger.Debug("Attempting connection...  {0}".FormatWith(i));
                        connected = ConnectToDatabase(connectionBuilder);
                        if (connected)
                        {
                            break;
                        }

                        Thread.Sleep(5000);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                TraceFactory.Logger.Error(sqlEx);
            }

            if (connected)
            {
                return;
            }

            // All connection attempts failed
            throw new InvalidOperationException("Could not connect to {0} database.".FormatWith(Resources.MonitorDatabase));
        }
コード例 #2
0
        /// <summary>
        /// Attempts a connection to the ePrint database.
        /// </summary>
        /// <param name="hostName"></param>
        private void CheckCloudPrintDatabase(string hostName)
        {
            SqlConnectionStringBuilder connectionBuilder = GetConnectionStringBuilder(hostName, Resources.ePrintDatabase);

            TraceFactory.Logger.Debug("Testing connection to " + connectionBuilder.DataPath());
            try
            {
                ExecuteSql(connectionBuilder.ToString(), Resources.CloudPrintTrigger);
                TraceFactory.Logger.Debug("Connected to " + connectionBuilder.DataPath());
                CacheConnectionString(connectionBuilder);
                return;
            }
            catch (SqlException sqlEx)
            {
                TraceFactory.Logger.Error(sqlEx);
            }

            // connection attempt failed
            throw new InvalidOperationException("Could not connect to {0} database.".FormatWith(Resources.ePrintDatabase));
        }
コード例 #3
0
        /// <summary>
        /// Attempts a connection to a database using the provided <see cref="SqlConnectionStringBuilder"/>.
        /// If connection is successful, caches the connection string for later use.
        /// </summary>
        /// <param name="connectionBuilder"></param>
        /// <returns></returns>
        private bool ConnectToDatabase(SqlConnectionStringBuilder connectionBuilder)
        {
            bool result = false;

            using (SqlAdapter adapter = new SqlAdapter(connectionBuilder.ToString()))
            {
                if (ConnectToDatabase(adapter))
                {
                    TraceFactory.Logger.Debug("Connected to " + connectionBuilder.DataPath());
                    CacheConnectionString(connectionBuilder);

                    result = true;
                }
            }

            return(result);
        }