/// <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); }
/// <summary> /// Gets the properties of the database. /// These properties include database status, owner, create date, file properties, and more. /// </summary> /// <returns> /// The properties of the database. /// </returns> public SqlDatabaseProperties GetDatabaseProperties() { string databaseStatus; switch (dmoDatabase.GetStatus()) { case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_EmergencyMode: databaseStatus = SR.GetString("SqlDatabase_Status_EmergencyMode"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Inaccessible: databaseStatus = SR.GetString("SqlDatabase_Status_Inaccessible"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Loading: databaseStatus = SR.GetString("SqlDatabase_Status_Loading"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Normal: databaseStatus = SR.GetString("SqlDatabase_Status_Normal"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Offline: databaseStatus = SR.GetString("SqlDatabase_Status_Offline"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Recovering: databaseStatus = SR.GetString("SqlDatabase_Status_Recovering"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Standby: databaseStatus = SR.GetString("SqlDatabase_Status_Standby"); break; case NativeMethods.SQLDMO_DBSTATUS_TYPE.SQLDMODBStat_Suspect: databaseStatus = SR.GetString("SqlDatabase_Status_Suspect"); break; default: databaseStatus = SR.GetString("SqlDatabase_Status_Unknown"); break; } NativeMethods.IDBFile dataFile = dmoDatabase.GetFileGroups().Item(1).GetDBFiles().Item(1); NativeMethods.ILogFile logFile = dmoDatabase.GetTransactionLog().GetLogFiles().Item(1); SqlFileProperties dataFileProps = new SqlFileProperties((dataFile.GetFileGrowthType() == NativeMethods.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB) ? SqlFileGrowthType.MB : SqlFileGrowthType.Percent, dataFile.GetFileGrowth(), dataFile.GetMaximumSize()); SqlFileProperties logFileProps = new SqlFileProperties((logFile.GetFileGrowthType() == NativeMethods.SQLDMO_GROWTH_TYPE.SQLDMOGrowth_MB) ? SqlFileGrowthType.MB : SqlFileGrowthType.Percent, logFile.GetFileGrowth(), logFile.GetMaximumSize()); SqlDatabaseProperties props = new SqlDatabaseProperties(dmoDatabase.GetName(), databaseStatus, dmoDatabase.GetOwner(), DateTime.Parse(dmoDatabase.GetCreateDate()), dmoDatabase.GetSize(), dmoDatabase.GetSpaceAvailable() / 1024F, dmoDatabase.GetUsers().GetCount(), dataFileProps, logFileProps); return(props); }
/// <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); } }