Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the Database class.
        /// </summary>
        /// <param name="instance">An initialized instance to be used with Database. The instance should have a database attached and ready to use.</param>
        /// <param name="ownsInstance">True if the instance handle passed into the constructur should be owned by the Database.</param>
        /// <param name="customConfig">A custom config set to use with the engine. The config set should atleast contain the attached database filename.</param>
        /// <remarks>Database will only manage the handle lifetime if ownsInstance is set to true. If its set to false, the caller is responsible for managing the teardown of the instance.</remarks>
        public Database(JET_INSTANCE instance, bool ownsInstance, IConfigSet customConfig)
        {
            this.config = new DatabaseConfig();
            this.config.Merge(customConfig);
            this.instance = instance;
            this.ownsInstance = ownsInstance;

            // Ensure that there is an attached database at a path specified by the config set
            using (var session = new Session(this.instance))
            {
                JET_DBID dbid;
                JET_wrn wrn = Api.JetOpenDatabase(session, this.config.DatabaseFilename, null, out dbid, OpenDatabaseGrbit.ReadOnly);
                Contract.Ensures(wrn == JET_wrn.Success);
                Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
            }

            // The config set is live now
            this.config.GetParamDelegate = this.TryGetParam;
            this.config.SetParamDelegate = this.SetParam;
        }
Exemple #2
0
        /// <summary>
        /// Initializes a new instance of the Database class.
        /// </summary>
        /// <param name="databaseFilename">Specifies a path to use for creating or opening a database file to use with the engine.</param>
        /// <param name="customConfig">A custom config set to use with the engine.</param>
        public Database(string databaseFilename, IConfigSet customConfig)
        {
            if (string.IsNullOrEmpty(databaseFilename) && customConfig == null)
            {
                throw new ArgumentException("Must specify a valid databaseFilename or customConfig");
            }

            this.config = new DatabaseConfig();
            if (!string.IsNullOrEmpty(databaseFilename))
            {
                this.config.DatabaseFilename = databaseFilename;
                string systemPath = Path.GetDirectoryName(databaseFilename);
                this.config.SystemPath = systemPath;
                this.config.LogFilePath = systemPath;
                this.config.TempPath = systemPath;
                this.config.AlternateDatabaseRecoveryPath = systemPath;
            }

            if (customConfig != null)
            {
                this.config.Merge(customConfig);    // throw on conflicts
            }

            if (string.IsNullOrEmpty(this.config.Identifier))
            {
                this.config.Identifier = Guid.NewGuid().ToString();
            }

            if (string.IsNullOrEmpty(this.config.DisplayName))
            {
                this.config.DisplayName = string.Format("Database Inst{0:D2}", Interlocked.Increment(ref Database.instanceCounter) - 1);
            }

            this.Start();
        }