Beispiel #1
0
        /// <summary>
        /// Adds a new database to the server using default properties.
        /// </summary>
        /// <param name="name">
        /// The name of the database to create.
        /// </param>
        /// <returns>
        /// If the operation succeeded, the return value is the database created.
        /// </returns>
        public SqlDatabase Add(string name)
        {
            if (name == null || name.Length == 0)
            {
                throw new ArgumentException(SR.GetString("SqlDatabaseCollection_MustHaveValidName"));
            }

            if (this[name] != null)
            {
                throw new ArgumentException(String.Format(SR.GetString("SqlDatabaseCollection_NameAlreadyExists"), name));
            }

            // Physically add database
            NativeMethods.IDatabase dmoDatabase = (NativeMethods.IDatabase) new NativeMethods.Database();
            dmoDatabase.SetName(name);
            server.dmoServer.GetDatabases().Add(dmoDatabase);

            SqlDatabase database = new SqlDatabase(dmoDatabase.GetName(), dmoDatabase.GetSize());

            // Set internal properties
            database.dmoDatabase = dmoDatabase;
            database.server      = this.server;

            // Add to private list
            databases.Add(database);

            return(database);
        }
Beispiel #2
0
        /// <summary>
        /// Updates the SqlDatabaseCollection with any changes made since the last call to Refresh.
        /// Refresh is automatically called once when the SqlServer.Databases collection is read.
        /// </summary>
        public void Refresh()
        {
            // Force internal refresh of tables
            server.dmoServer.GetDatabases().Refresh(false);

            // Clear out old list
            databases = new ArrayList();

            // List all databases and add them one by one
            for (int i = 0; i < server.dmoServer.GetDatabases().GetCount(); i++)
            {
                NativeMethods.IDatabase database = server.dmoServer.GetDatabases().Item(i + 1, "");

                string name = database.GetName();

                // To find out permissions we have to try to "use" the database
                try
                {
                    server.Query("use [" + name + "]");
                }
                catch
                {
                    // If an exception is thrown, go to the next database
                    continue;
                }

                // If we get here, we at least have permissions to look at the database's name

                // A size of -1 indicates "unknown" (due to security)
                int size = -1;
                try
                {
                    size = database.GetSize();
                }
                catch
                {
                }

                SqlDatabase db = new SqlDatabase(name, size);

                // Tell the database which DMO database it represents
                db.dmoDatabase = database;
                db.server      = this.server;

                databases.Add(db);
            }
        }