GetDataSourceFilePath() private static méthode

private static GetDataSourceFilePath ( DbConnectionStringBuilder builder, string connectionString ) : string
builder System.Data.Common.DbConnectionStringBuilder
connectionString string
Résultat string
Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VistaDBErrorLog"/> class
        /// using a dictionary of configured settings.
        /// </summary>

        public VistaDBErrorLog(IDictionary config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _connectionString = ConnectionStringHelper.GetConnectionString(config);

            //
            // If there is no connection string to use then throw an
            // exception to abort construction.
            //

            if (_connectionString.Length == 0)
            {
                throw new ApplicationException("Connection string is missing for the VistaDB error log.");
            }

            _databasePath = ConnectionStringHelper.GetDataSourceFilePath(_connectionString);
            InitializeDatabase();

            string appName = Mask.NullString((string)config["applicationName"]);

            if (appName.Length > _maxAppNameLength)
            {
                throw new ApplicationException(string.Format(
                                                   "Application name is too long. Maximum length allowed is {0} characters.",
                                                   _maxAppNameLength.ToString("N0")));
            }

            ApplicationName = appName;
        }
Exemple #2
0
        private void InitializeDatabase()
        {
            string connectionString = ConnectionString;

            Debug.AssertStringNotEmpty(connectionString);

            string dbFilePath = ConnectionStringHelper.GetDataSourceFilePath(connectionString);

            if (File.Exists(dbFilePath))
            {
                return;
            }

            //
            // Make sure that we don't have multiple threads all trying to create the database
            //

            lock (_lock)
            {
                //
                // Just double check that no other thread has created the database while
                // we were waiting for the lock
                //

                if (File.Exists(dbFilePath))
                {
                    return;
                }

                SQLiteConnection.CreateFile(dbFilePath);

                const string sql = @"
                CREATE TABLE Error (
                    ErrorId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
                    Application TEXT NOT NULL,
                    Host TEXT NOT NULL,
                    Type TEXT NOT NULL,
                    Source TEXT NOT NULL,
                    Message TEXT NOT NULL,
                    User TEXT NOT NULL,
                    StatusCode INTEGER NOT NULL,
                    TimeUtc TEXT NOT NULL,
                    AllXml TEXT NOT NULL
                )";

                using (SQLiteConnection connection = new SQLiteConnection(connectionString))
                    using (SQLiteCommand command = new SQLiteCommand(sql, connection))
                    {
                        connection.Open();
                        command.ExecuteNonQuery();
                    }
            }
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="VistaDBErrorLog"/> class
        /// to use a specific connection string for connecting to the database.
        /// </summary>

        public VistaDBErrorLog(string connectionString)
        {
            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }

            if (connectionString.Length == 0)
            {
                throw new ArgumentException(null, "connectionString");
            }

            _connectionString = connectionString;
            _databasePath     = ConnectionStringHelper.GetDataSourceFilePath(_connectionString);

            InitializeDatabase();
        }
Exemple #4
0
        private void InitializeDatabase()
        {
            string connectionString = ConnectionString;

            Debug.AssertStringNotEmpty(connectionString);

            string dbFilePath = ConnectionStringHelper.GetDataSourceFilePath(connectionString);

            if (File.Exists(dbFilePath))
            {
                return;
            }

            //
            // Make sure that we don't have multiple instances trying to create the database.
            //

            lock (_mdbInitializationLock)
            {
                //
                // Just double-check that no other thread has created the database while
                // we were waiting for the lock.
                //

                if (File.Exists(dbFilePath))
                {
                    return;
                }

                //
                // Create a temporary copy of the mkmdb.vbs script.
                // We do this in the same directory as the resulting database for security permission purposes.
                //

                string scriptPath = Path.Combine(Path.GetDirectoryName(dbFilePath), _scriptResourceName);

                using (FileStream scriptStream = new FileStream(scriptPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    ManifestResourceHelper.WriteResourceToStream(scriptStream, _scriptResourceName);
                }

                //
                // Run the script file to create the database using batch
                // mode (//B), which suppresses script errors and prompts
                // from displaying.
                //

                ProcessStartInfo psi = new ProcessStartInfo(
                    "cscript", "\"" + scriptPath + "\" \"" + dbFilePath + "\" //B //NoLogo");

                psi.UseShellExecute = false;    // i.e. CreateProcess
                psi.CreateNoWindow  = true;     // Stay lean, stay mean

                try
                {
                    using (Process process = Process.Start(psi))
                    {
                        //
                        // A few seconds should be plenty of time to create the database.
                        //

                        TimeSpan tolerance = TimeSpan.FromSeconds(2);
                        if (!process.WaitForExit((int)tolerance.TotalMilliseconds))
                        {
                            //
                            // but it wasn't, so clean up and throw an exception!
                            // Realistically, I don't expect to ever get here!
                            //

                            process.Kill();

                            throw new Exception(string.Format(
                                                    "The Microsoft Access database creation script took longer than the allocated time of {0} seconds to execute. "
                                                    + "The script was terminated prematurely.",
                                                    tolerance.TotalSeconds));
                        }

                        if (process.ExitCode != 0)
                        {
                            throw new Exception(string.Format(
                                                    "The Microsoft Access database creation script failed with exit code {0}.",
                                                    process.ExitCode));
                        }
                    }
                }
                finally
                {
                    //
                    // Clean up after ourselves!!
                    //

                    File.Delete(scriptPath);
                }
            }
        }
Exemple #5
0
        private void InitializeDatabase()
        {
            string connectionString = ConnectionString;

            Debug.AssertStringNotEmpty(connectionString);

            string dbFilePath = ConnectionStringHelper.GetDataSourceFilePath(connectionString);

            if (File.Exists(dbFilePath))
            {
                return;
            }
            using (SqlCeEngine engine = new SqlCeEngine(ConnectionString))
            {
                engine.CreateDatabase();
            }

            using (SqlCeConnection conn = new SqlCeConnection(ConnectionString))
            {
                using (SqlCeCommand cmd = new SqlCeCommand())
                {
                    conn.Open();
                    SqlCeTransaction transaction = conn.BeginTransaction();

                    try
                    {
                        cmd.Connection  = conn;
                        cmd.Transaction = transaction;

                        cmd.CommandText = @"
                        SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'ELMAH_Error'";

                        object obj = cmd.ExecuteScalar();
                        if (obj == null)
                        {
                            cmd.CommandText = @"
                            CREATE TABLE ELMAH_Error (
                                [ErrorId] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT newid(),
                                [Application] NVARCHAR(60) NOT NULL,
                                [Host] NVARCHAR(50) NOT NULL,
                                [Type] NVARCHAR(100) NOT NULL,
                                [Source] NVARCHAR(60) NOT NULL,
                                [Message] NVARCHAR(500) NOT NULL,
                                [User] NVARCHAR(50) NOT NULL,
                                [StatusCode] INT NOT NULL,
                                [TimeUtc] DATETIME NOT NULL,
                                [Sequence] INT IDENTITY (1, 1) NOT NULL,
                                [AllXml] NTEXT NOT NULL
                            )";
                            cmd.ExecuteNonQuery();

                            cmd.CommandText = @"
                            CREATE NONCLUSTERED INDEX [IX_Error_App_Time_Seq] ON [ELMAH_Error] 
                            (
                                [Application]   ASC,
                                [TimeUtc]       DESC,
                                [Sequence]      DESC
                            )";
                            cmd.ExecuteNonQuery();
                        }
                        transaction.Commit(CommitMode.Immediate);
                    }
                    catch (SqlCeException)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Exemple #6
0
        private void InitializeDatabase()
        {
            string connectionString = ConnectionString;

            Debug.AssertStringNotEmpty(connectionString);

            string dbFilePath = ConnectionStringHelper.GetDataSourceFilePath(connectionString);

            if (File.Exists(dbFilePath))
            {
                return;
            }

            //
            // Make sure that we don't have multiple threads all trying to create the database
            //

            lock (_lock)
            {
                //
                // Just double check that no other thread has created the database while
                // we were waiting for the lock
                //

                if (File.Exists(dbFilePath))
                {
                    return;
                }

                using (SqlCeEngine engine = new SqlCeEngine(ConnectionString))
                {
                    engine.CreateDatabase();
                }

                const string sql1 = @"
                CREATE TABLE ELMAH_Error (
                    [ErrorId] UNIQUEIDENTIFIER NOT NULL PRIMARY KEY DEFAULT newid(),
                    [Application] NVARCHAR(60) NOT NULL,
                    [Host] NVARCHAR(50) NOT NULL,
                    [Type] NVARCHAR(100) NOT NULL,
                    [Source] NVARCHAR(60) NOT NULL,
                    [Message] NVARCHAR(500) NOT NULL,
                    [User] NVARCHAR(50) NOT NULL,
                    [StatusCode] INT NOT NULL,
                    [TimeUtc] DATETIME NOT NULL,
                    [Sequence] INT IDENTITY (1, 1) NOT NULL,
                    [AllXml] NTEXT NOT NULL
                )";

                const string sql2 = @"
                CREATE NONCLUSTERED INDEX [IX_Error_App_Time_Seq] ON [ELMAH_Error] 
                (
                    [Application]   ASC,
                    [TimeUtc]       DESC,
                    [Sequence]      DESC
                )";

                using (SqlCeConnection conn = new SqlCeConnection(ConnectionString))
                {
                    using (SqlCeCommand cmd = new SqlCeCommand())
                    {
                        conn.Open();
                        cmd.Connection = conn;

                        cmd.CommandText = sql1;
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = sql2;
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }