internal static SqlDataSourceInformation Parse(string dataSource) { if (string.IsNullOrEmpty(dataSource)) { throw new Exception("Invalid empty datasource name."); } SqlDataSourceInformation information = new SqlDataSourceInformation(); try { int index = dataSource.IndexOf(','); if (index != -1) { information._portNumber = int.Parse(dataSource.Substring(index + 1)); dataSource = dataSource.Substring(0, index); } int length = dataSource.IndexOf('\\'); if (length > -1) { information._instanceName = dataSource.Substring(length + 1); dataSource = dataSource.Substring(0, length); } } catch { throw new Exception("The datasource name was not in the format expected."); } information._serverName = dataSource; return(information); }
internal static bool ProcessException(SqlConnection connection, SqlException ex) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connection.ConnectionString); SqlDataSourceInformation information = SqlDataSourceInformation.Parse(builder.DataSource); if ((!string.IsNullOrEmpty(information.ServerName) && information.VerifyIsLocal()) && !string.IsNullOrEmpty(information.InstanceName)) { string serviceName = "MSSQL$" + information.InstanceName; ServiceStatusError error = CheckServiceError(serviceName); string str2 = null; switch (error) { case ServiceStatusError.Notinstalled: str2 = "The service '" + serviceName + "' is not installed."; break; case ServiceStatusError.Notstarted: { str2 = "The service '" + serviceName + "' is not started."; string str3 = ""; Console.WriteLine(str2); if (!ScriptEngine.IsExecutingScript) { while (!str3.Equals("y", StringComparison.OrdinalIgnoreCase) && !str3.Equals("n", StringComparison.OrdinalIgnoreCase)) { str3 = ConsoleUtil.PromptUser("Would you like to start the service (y/n)?"); Console.WriteLine(""); if (str3.Equals("y", StringComparison.OrdinalIgnoreCase)) { ServiceController controller = new ServiceController(serviceName); Console.Write("Starting service..."); try { controller.Start(); controller.WaitForStatus(ServiceControllerStatus.Running); Thread.Sleep(0x1388); } catch { Console.WriteLine(""); } if (controller.Status == ServiceControllerStatus.Running) { Console.WriteLine(" Done."); return(true); } Console.WriteLine(" Failed."); return(false); } } } break; } } if (str2 != null) { throw new Exception(ex.Message + Environment.NewLine + Environment.NewLine + "Additional information:" + Environment.NewLine + str2, ex); } } throw ex; }