Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Tom Roberts 4 posts 94 karma points
    Jan 14, 2022 @ 21:50
    Tom Roberts
    0

    Migrating from 7 to 8 to 9. Accessing external database.

    I've been accessing an external database from a Partial View using Razor in Umbraco v7. Trying to get caught up and moving to v9. The following does not work and I'm having a hard time finding any code samples that might get me closer. I realize I should probably re-write this using Surface Controllers, but I really need something quick and dirty. Any help is appreciated.

        using (var db = new Database("ApplicationServicesBETA"))
        {
            return db.Query<RRContact>("SELECT DISTINCT Title,Initial,Featured FROM RRContacts ORDER BY Featured DESC, Initial").ToList();
        }
    
  • Tom Roberts 4 posts 94 karma points
    Jan 17, 2022 @ 15:32
    Tom Roberts
    0

    I managed to access a table within Umbraco from a partial view following https://our.umbraco.com/documentation/Implementation/Services/#using-the-siteservice-inside-a-view. Still trying to access an external database using this method. Any help would be greatly appreciated.

  • Tom Roberts 4 posts 94 karma points
    Jan 19, 2022 @ 13:25
    Tom Roberts
    100

    The goal was to supply records to a jQuery DataTables (https://datatables.net/) by providing an API endpoint. Hence TParameters and DTResult, which mimics the datatables parameters). The data was stored in a separate database and I really didn't want to import it every week. Key issues this may help with: Routing, Getting a connection string from appsettings, adding "[FromForm]", and finally how to connect to the external database and query. The rest is pretty specific to jQuery DataTables Here's the Controller I came up with:

        [Route("/DT/DTApi/[action]")]
    public class DTApiController : UmbracoApiController
    {
        private readonly string _connectionString;
        public DTApiController(IConfiguration configuration)
        {
            _connectionString = configuration.GetConnectionString("ApplicationServicesBETA");
        }
    
        [HttpPost]
        public object StationsJunctions([FromForm]DTParameters param)
        {
            try
            {
                var dtsource = new List<StationJunction>();
                using (var db = new Database(_connectionString, DatabaseType.SqlServer2012, SqlClientFactory.Instance))
                {
                    var query = new Sql().Select("State, OPName, Railroad, Name, R260Code, SPLC, FSAC, GeoArea, BEA, RateZipCode, SCAC, IntermodalRamp").From("v_Stations_Junctions");
                    dtsource = db.Fetch<StationJunction>(query);
                }
    
                List<String> columnSearch = new List<string>();
    
                foreach (var col in param.Columns)
                {
                    columnSearch.Add(col.Search.Value);
                }
    
                List<StationJunction> data = new StationJunctionResult().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, dtsource, columnSearch);
                int count = new StationJunctionResult().Count(param.Search.Value, dtsource, columnSearch);
                DTResult<StationJunction> result = new DTResult<StationJunction>
                {
                    draw = param.Draw,
                    data = data,
                    recordsFiltered = count,
                    recordsTotal = count
                };
                return result;
            }
            catch (Exception ex)
            {
                return new { error = ex.Message };
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft