Copied to clipboard

Flag this post as spam?

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


  • Karl Svensson 9 posts 29 karma points
    Oct 16, 2014 @ 18:06
    Karl Svensson
    0

    Connecting with external sql server in Umbraco 7 MVC

    Hello, I am newby with Umbraco and it has been hard for me to get updated info about how to connect with external database and render non-umbraco info into my umbraco views.

    May I use a secondary connectionstring and ApplicationContex? Or make another MVC project in the same solution and share the DLL? Or must use web services to do that?

    How do I approach to this need?

    Thx, Karl

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 17, 2014 @ 10:48
    Dave Woestenborghs
    1

    Hi Karl,

    Welcome to our.

    There are several answers to your question. You can solve this issue in a way that feels most comfortable to you.

    You can add a connection string and read your data using SQL queries or using ORM like NHibernate, Entity Framework or PetaPoco (which ships with Umbraco).

    Or you can create a seperate webservice or WebApi application that you consume from Umbraco. 

    In the views you can load the data.

    Dave

  • Karl Svensson 9 posts 29 karma points
    Oct 18, 2014 @ 22:02
    Karl Svensson
    0

    Thanks Dave,

    Could yo refer me to a sample with a secondary SQL connection string and retrieve data from Models to Umbraco Partial View?

    Karl

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 19, 2014 @ 13:08
    Dave Woestenborghs
    0

    Hi Karl,

    In this code I created for a DUUG presentation is an example of getting data from a custom table (in this case is in the same database) using PetaPoco and exposed as through WebAPI.

    You can use these models in your views.

    Maybe it is a good starting point ?

    Code can be found here : https://bitbucket.org/dawoe/duugcustomsections

    Dave

  • Karl Svensson 9 posts 29 karma points
    Oct 21, 2014 @ 19:14
    Karl Svensson
    0

    Dave, I have need some time to make it work, but now I can use your sample and it looks great!

    These are the steps I did, it will be useful for future readers of this post. It has been also a way to learn Umbraco installation

    • Downlaod Umbraco 7.1.6 binary files and rewrite lost references in both projects http://our.umbraco.org/ReleaseDownload?id=131659
    • Delete the hatefully Newtonsoft.Json all references and packages and reinstall updated version from Nuget
    • Run Umbraco installation and enter to backoffice
    • Edit my user to allow acces to Customer plugin
    • Logout and Login and voila! I got it

    Thanks for your support Karl

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Oct 21, 2014 @ 19:21
    Dave Woestenborghs
    0

    Glad to see it helped you further. Strange that you needed to download the 7.1.6 binary files by the way. I believe I enabled nuget package restore in the solution.

    Dave

  • Bendik Engebretsen 105 posts 202 karma points
    Jan 08, 2016 @ 14:35
    Bendik Engebretsen
    0

    Hi Karl and David,

    I'm facing the same challenge as Karl now, I want to be able to show data from an external database in an Umbraco partial view. I have followed this receipe for showing data from custom tables in the same database as Umbraco (in my case Umbraco.mdb) using PetaPoco, and this works fine. However, I now want to access data in a different database, assuming I can use the same Technology (PetaPoco).

    So the question is: how do I connect the models and controllers to a different database than the Umbraco database? Where goes the secondary database connection string and where do I refer to it? From what I understand, this was what you were trying to do, Karl?

    Best, Bendik

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Jan 08, 2016 @ 14:41
    Dirk De Grave
    1

    Just add your connnection string in web.config, you can have as many connections as you want.

  • Bendik Engebretsen 105 posts 202 karma points
    Jan 11, 2016 @ 12:42
    Bendik Engebretsen
    0

    Thanks Dirk!

    Yes, I figured this out, and I also figured out how to use it with PetaPoco: In your XyzRepository class:

    Instead of

        private readonly UmbracoDatabase _database;
    
        public XyzRepository()
        {
            _database = ApplicationContext.Current.DatabaseContext.Database;
        }
    

    you use

        private readonly Database _database;
    
        public XyzRepository()
        {
           _database = new Database("<The name of your connection string>");
        }
    

    Simple as that! I also figured out the connection string to an MS Access database, in case anyone is interested:

    connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\YourMSAccessDatabase.accdb" providerName="System.Data.OleDb"
    
Please Sign in or register to post replies

Write your reply to:

Draft