Copied to clipboard

Flag this post as spam?

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


  • John ben 78 posts 293 karma points
    Feb 27, 2017 @ 09:25
    John ben
    0

    Using Umbraco on multiple databases

    Hi All,

    I have a requirement that i need to work with multiple databases at a time, my requirement is i want to get data from other database and fetch that data into my content page, so i created a new connection in my web.config a tried to call it in my code, the probleme is using peta poco with which is installed with umbraco i think i have _db = ApplicationContext.Current.DatabaseContext.Database for my Umbraco BD ,but if i want to specify another db i don't know which method should i overrid or install another dll of petapoco which is not related to umbraco to specify a connexion but i think that's a bad idea.

    (I m using umbraco7.5.10 version and I am using visualstudio2015 and mvc5).

    Thanks&Regards

    Youness ben

  • Alex Skrypnyk 5363 posts 19800 karma points MVP 3x admin c-trib
    Feb 27, 2017 @ 11:17
    Alex Skrypnyk
    1

    Hi Youness

    You have to create new object of Database class with another connection string, but be aware about storing this object, we store it in httpContext. Try to use this static method:

        public static Database CurrentDb()
        {
            if (HttpContext.Current.Items["CurrentDb"] == null)
            {
                var retval = new Database(ConfigurationManager.ConnectionStrings["CustomDatabaseConnection"].ConnectionString, ConfigurationManager.ConnectionStrings["CustomDatabaseConnection"].ProviderName);
                HttpContext.Current.Items["CurrentDb"] = retval;
    
                return retval;
            }
    
            return (Database)HttpContext.Current.Items["CurrentDb"];
        }
    

    And then use it like:

    var result = CurrentDb().Fetch<Object>(sql);
    

    Hope it will help you.

    Thanks,

    Alex

  • John ben 78 posts 293 karma points
    Feb 27, 2017 @ 11:51
    John ben
    0

    Hi Alex,

    I would like to know how did you use httpcontext while ur heriting SurfaceController ?i tried ur solution but i m having an error with HttpContext that need a referencie.

    Thanks&Regards

    Youness ben

  • Alex Skrypnyk 5363 posts 19800 karma points MVP 3x admin c-trib
    Feb 27, 2017 @ 11:52
    Alex Skrypnyk
    0

    Please add "using System.Web;" to your controller.

  • Alex Skrypnyk 5363 posts 19800 karma points MVP 3x admin c-trib
    Feb 27, 2017 @ 11:53
    Alex Skrypnyk
    0

    Or share code your controller's code, we will look together.

  • John ben 78 posts 293 karma points
    Feb 27, 2017 @ 11:57
    John ben
    0

    Here is my code still having the same error

  • Alex Skrypnyk 5363 posts 19800 karma points MVP 3x admin c-trib
    Feb 27, 2017 @ 11:57
    Alex Skrypnyk
    0

    Youness, can you move this method outside Controllers class? It's not right way to use static methods, move it to static class.

  • Alex Skrypnyk 5363 posts 19800 karma points MVP 3x admin c-trib
    Feb 27, 2017 @ 11:59
    Alex Skrypnyk
    101
    public static class DbHelper
    {
        public static Database CurrentDb()
        {
            if (HttpContext.Current.Items["CurrentDb"] == null)
            {
                var retval =
                    new Database(ConfigurationManager.ConnectionStrings["CustomDatabaseConnection"].ConnectionString,
                        ConfigurationManager.ConnectionStrings["CustomDatabaseConnection"].ProviderName);
                HttpContext.Current.Items["CurrentDb"] = retval;
    
                return retval;
            }
    
            return (Database) HttpContext.Current.Items["CurrentDb"];
        }
    }
    

    And use it in Controller, example:

    DbHelper.CurrentDb().Fetch<Object>(sql);
    
  • John ben 78 posts 293 karma points
    Feb 27, 2017 @ 12:10
    John ben
    0

    It works very well thank you Alex!

  • Alex Skrypnyk 5363 posts 19800 karma points MVP 3x admin c-trib
    Feb 27, 2017 @ 12:27
    Alex Skrypnyk
    0

    Glad to help you! Have a nice day!

Please Sign in or register to post replies

Write your reply to:

Draft