public ActionResult SqlServerDiagnostics()

in windows/aspnet-gmsa/dotnetdemoappMVC/Controllers/NetworkAccessController.cs [32:69]


        public ActionResult SqlServerDiagnostics(string sqlServer)
        {
            var result = new StringBuilder();
            var sqlConnection = new SqlConnection();
            try
            {
                sqlConnection.ConnectionString = $"Server={sqlServer};Integrated Security=true;Initial Catalog=master";
                sqlConnection.Open();
                result.AppendLine("[Pass] Connection to SQL Server was successfull. Authentication with gMSA suceeded.");
            }
            catch (Exception e)
            {
                result.AppendLine("[Error] Error while connecting to SQL Server. Error is: " + e.Message);
            }

            try
            {
                var sqlCommand = sqlConnection.CreateCommand();
                var query = "SELECT name FROM sys.databases";
                sqlCommand.CommandText = query;
                SqlDataReader reader = sqlCommand.ExecuteReader();

                result.AppendLine("[Pass] Executed the query 'SELECT name FROM sys.databases' and found the following databases:");
                while (reader.Read())
                {

                    result.AppendLine(reader.GetString(0));
                }
                reader.Close();
                sqlConnection.Close();
            }
            catch (Exception e)
            {
                result.AppendLine("[Error] Error while querying SQL Server. Error is: " + e.Message);
            }

            return Content(result.ToString());
        }