예제 #1
0
        public IActionResult ExecuteQuery([FromBody] SSASQueryModel model)
        {
            //Check if the data was supplied right
            if (ModelState.IsValid)
            {
                //Get the connection from the config
                var connectionStr = Configuration.GetConnectionString("AnalysisServer");
                //Check that we have the connection
                if (!string.IsNullOrEmpty(connectionStr))
                {
                    //Create the connection
                    using (var connection = new AdomdConnection(connectionStr))
                    {
                        try
                        {
                            //Open the connection to the db
                            connection.Open();
                            //Create the command
                            using (var command = connection.CreateCommand())
                            {
                                //Set the command query
                                command.CommandText = model.Query;
                                //Get the reader
                                using (var reader = command.ExecuteReader())
                                {
                                    //holds each row and its column values
                                    var results = new List <Dictionary <string, string> >();
                                    //Start reading the data
                                    while (reader.Read())
                                    {
                                        //holds the results from the reader as row count and the filedName, value pair
                                        var dbReadResult = new Dictionary <string, string>();
                                        //loop through the filds
                                        for (int i = 0; i < reader.FieldCount; i++)
                                        {
                                            dbReadResult.Add(reader.GetName(i), reader.GetValue(i)?.ToString());
                                        }
                                        results.Add(dbReadResult);
                                    }

                                    return(Ok(results));
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            //Log the error
                            LoggingService.LogException(ex);

                            return(InternalServerError(new
                            {
                                message = ErrorMessages.AnalysisServer_UnableToOpenConnnection,
                                Exception = ex.GetBaseException()
                            }));
                        }
                        finally
                        {
                            connection.Clone();
                        }
                    }
                }
                return(InternalServerError(new
                {
                    message = ErrorMessages.AnalysisServer_NotFound
                }));
            }
            return(BadRequest());
        }