// check if database is of proper size private static DatabaseConfigurationResult CheckSqlDatabaseSize(string servername, string username, string password, string database) { DatabaseConfigurationResult result = new DatabaseConfigurationResult(); result.Success = true; result.Message = "Database meets size requirements"; // setup connection string connectionString = String.Format("user id={0};password={1};server={2};Initial Catalog={3};connection timeout=4", username, password, servername, database); SqlConnection testConnection = new SqlConnection(connectionString); SqlDataReader reader; // try connection try { testConnection.Open(); // test permissions by creating and dropping a table SqlCommand testCommand = new SqlCommand("sp_helpdb " + database, testConnection); reader = testCommand.ExecuteReader(); // get second data table reader.NextResult(); reader.Read(); string size = reader.GetValue(5).ToString(); if (size != "Unlimited") { if (size.Contains("KB")) { size = size.Replace(" KB", ""); int sizeInKB = Int32.Parse(size); int sizeInMB = sizeInKB / 1024; if (sizeInMB < minimumDatabaseSize) { result.Success = false; result.Message = String.Format("The database '{0}' is not large enough to properly support Rock. Please ensure it is at least {1}MB in size (current size is {2}MB).", database, minimumDatabaseSize.ToString(), sizeInMB.ToString()); } } } } catch (Exception ex) { // we'll default to everything is OK result.Message = "Assuming database size is acceptable as we were unable to determine its size."; return(result); } finally { testConnection = null; } return(result); }
// check sql server version private static DatabaseConfigurationResult CheckSqlServerVersion(string servername, string username, string password) { DatabaseConfigurationResult result = new DatabaseConfigurationResult(); result.Success = false; string version = "0"; string versionInfo = string.Empty; // setup connection string connectionString = String.Format("user id={0};password={1};server={2};connection timeout=4", username, password, servername); SqlConnection testConnection = new SqlConnection(connectionString); // try connection try { testConnection.Open(); SqlCommand versionCommand = new SqlCommand("SELECT SERVERPROPERTY('productversion'), @@Version", testConnection); SqlDataReader versionReader = versionCommand.ExecuteReader(); while (versionReader.Read()) { version = versionReader[0].ToString(); versionInfo = versionReader[1].ToString(); } string[] versionParts = version.Split('.'); int majorVersion = -1; Int32.TryParse(versionParts[0], out majorVersion); if (majorVersion >= 10) { result.Success = true; } result.Message = versionInfo.Split('-')[0].ToString().Replace("(RTM)", "").Trim(); } catch (Exception ex) { result.Success = false; } finally { testConnection = null; } return(result); }
// check if database is of proper size private static DatabaseConfigurationResult CheckSqlDatabaseSize( string servername, string username, string password, string database ) { DatabaseConfigurationResult result = new DatabaseConfigurationResult(); result.Success = true; result.Message = "Database meets size requirements"; // setup connection string connectionString = String.Format( "user id={0};password={1};server={2};Initial Catalog={3};connection timeout=4", username, password, servername, database ); SqlConnection testConnection = new SqlConnection( connectionString ); SqlDataReader reader; // try connection try { testConnection.Open(); // test permissions by creating and dropping a table SqlCommand testCommand = new SqlCommand( "sp_helpdb " + database, testConnection ); reader = testCommand.ExecuteReader(); // get second data table reader.NextResult(); reader.Read(); string size = reader.GetValue( 5 ).ToString(); if ( size != "Unlimited" ) { if ( size.Contains( "KB" ) ) { size = size.Replace( " KB", "" ); int sizeInKB = Int32.Parse( size ); int sizeInMB = sizeInKB / 1024; if ( sizeInMB < minimumDatabaseSize ) { result.Success = false; result.Message = String.Format( "The database '{0}' is not large enough to properly support Rock. Please ensure it is at least {1}MB in size (current size is {2}MB).", database, minimumDatabaseSize.ToString(), sizeInMB.ToString()); } } } } catch ( Exception ex ) { // we'll default to everything is OK result.Message = "Assuming database size is acceptable as we were unable to determine its size."; return result; } finally { testConnection = null; } return result; }
// check sql server version private static DatabaseConfigurationResult CheckSqlServerVersion( string servername, string username, string password ) { DatabaseConfigurationResult result = new DatabaseConfigurationResult(); result.Success = false; string version = "0"; string versionInfo = string.Empty; // setup connection string connectionString = String.Format( "user id={0};password={1};server={2};connection timeout=4", username, password, servername ); SqlConnection testConnection = new SqlConnection( connectionString ); // try connection try { testConnection.Open(); SqlCommand versionCommand = new SqlCommand( "SELECT SERVERPROPERTY('productversion'), @@Version", testConnection ); SqlDataReader versionReader = versionCommand.ExecuteReader(); while ( versionReader.Read() ) { version = versionReader[0].ToString(); versionInfo = versionReader[1].ToString(); } string[] versionParts = version.Split( '.' ); int majorVersion = -1; Int32.TryParse( versionParts[0], out majorVersion ); if ( majorVersion >= 10 ) { result.Success = true; } result.Message = versionInfo.Split( '-' )[0].ToString().Replace( "(RTM)", "" ).Trim(); } catch ( Exception ex ) { result.Success = false; } finally { testConnection = null; } return result; }
// check sql server public static EnvironmentCheckResult CheckSqlServer(string dbServer, string dbUsername, string dbPassword, string dbDatabase) { EnvironmentCheckResult result = new EnvironmentCheckResult(); result.Message = "Your database settings all look good."; result.DidPass = true; // check that user can login DatabaseConnectionResult connectResult = CheckSqlLogin(dbServer, dbUsername, dbPassword); if (!connectResult.CanConnect) { result.Message = connectResult.Message; result.DidPass = false; return(result); } // check sql version DatabaseConfigurationResult sqlVersionResult = CheckSqlServerVersion(dbServer, dbUsername, dbPassword); if (!sqlVersionResult.Success) { result.Message = string.Format("You are running SQL Server version: {0}", sqlVersionResult.Message); result.HelpLink = "http://www.rockrms.com/Rock/LetsFixThis#SqlServerWrongVersion"; result.DidPass = false; return(result); } // check if database exists if (!CheckSqlDatabaseExists(dbServer, dbUsername, dbPassword, dbDatabase)) { // try creating the database if (!CheckSqlPermissionsCreateDatabase(dbServer, dbUsername, dbPassword)) { result.Message = String.Format("The database '{0}' does not exist and the user '{1}' does not have permissions to create a database.", dbDatabase, dbUsername); result.HelpLink = "http://www.rockrms.com/Rock/LetsFixThis#NoDatabaseNoPermissions"; result.DidPass = false; return(result); } result.Message = String.Format("The '{0}' database does not exist on the server, but you have permissions to create it. Rock will create it for you as part of the install.", dbDatabase); } else { // check that we have permissions to create a table in the database if (!CheckSqlPermissionsCreateTable(dbServer, dbUsername, dbPassword, dbDatabase)) { result.Message = String.Format("The user '{0}' does not have write access to the database '{1}'.", dbUsername, dbDatabase); result.HelpLink = "http://www.rockrms.com/Rock/LetsFixThis#SqlServerPermissions"; result.DidPass = false; return(result); } // since the database exists make sure it's empty if (!CheckSqlServerEmpty(dbServer, dbUsername, dbPassword, dbDatabase)) { result.Message = String.Format("The database '{0}' is not empty. To protect your existing data Rock must be installed into a empty database.", dbDatabase); result.HelpLink = "http://www.rockrms.com/Rock/LetsFixThis#DatabaseNotEmpty"; result.DidPass = false; return(result); } } DatabaseConfigurationResult sizeResult = CheckSqlDatabaseSize(dbServer, dbUsername, dbPassword, dbDatabase); if (!sizeResult.Success) { result.Message = sizeResult.Message; result.HelpLink = "http://www.rockrms.com/Rock/LetsFixThis#DatabaseTooSmall"; result.DidPass = false; return(result); } return(result); }