Copied to clipboard

Flag this post as spam?

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


  • David Tregoning 63 posts 236 karma points
    Mar 17, 2015 @ 16:47
    David Tregoning
    0

    Petapoco connection to non-umbraco database

    All examples of Using PetaPoco within Umbraco I have found reference the main Umbraco database using (or similar):

    var db = applicationContext.DatabaseContext.Database;
    

    I wish to connect to an addition database but I am having trouble.

    I have created a connection string within my web.config file with the name of productDbDSN. I have created the following controller within Visual Studio (based on the documentation on the toptensoftware website) but have the red line under PetaPoco:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Umbraco.Core.Persistence;
    
    namespace Products.Controllers
    {
        public class ProductsController : Controller
        {
    
            public ActionResult Index()
            {
                var dataContext = new PetaPoco.Database("productsDbDSN");
                var products = from p in dataContext
                                select p;
                return View(products.Take(20));
            }
        }
    }
    

    I thought by using umbraco.Core.Persistence I am accessing PetaPoco? Can anybody help?

  • Theo Paraskevopoulos 33 posts 122 karma points
    Mar 26, 2015 @ 18:01
    Theo Paraskevopoulos
    0

    Hi David

    I think all you need is this: 

    var dataContext =newDatabase("productsDbDSN");

    That will get you hooked up. Not sure about the rest though. I personally like to see my SQL:

    var products = dataContext ().Query<Product>("select * from products");

    Cheers,
    Theo 

  • David Tregoning 63 posts 236 karma points
    Mar 27, 2015 @ 16:12
    David Tregoning
    0

    Thanks Theo, I tried that but Visual Studio threw up an error for 'newDatabase' which was:

    The name 'newDatabase' does not esixt in the current context

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 27, 2015 @ 16:51
    Hendy Racher
    0

    Hi David,

    Looks like it's a typo, the class Database is in the Umbraco.Core.Persistence namespace.

    using Umbraco.Core.Persistence;
    ...
    var dataContext = new Database("productsDbSDN");
    var products = dataContext.Query<Product>("select * from products");

    HTH,

    Hendy

  • David Tregoning 63 posts 236 karma points
    Mar 27, 2015 @ 17:32
    David Tregoning
    0

    Thanks Hendy,

    thinking about it that typo makes more sense and is closer to my original code. However now I am getting an error that (the 2nd) dataContext reference is a variable but used as a method and that the Query<Product> is namespace but used as a type.

    Any suggestions?

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 27, 2015 @ 17:36
    Hendy Racher
    0

    Hi David,

    Do you have a class called Product ? or another model / class to represent a single row in the database table you'd like to query ?

  • David Tregoning 63 posts 236 karma points
    Mar 30, 2015 @ 13:12
    David Tregoning
    0

    Yes I have the following model called Products

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Umbraco.Core.Persistence;
    
    namespace Employee.Products
    {
        [TableName("Products")]
        [PrimaryKey("ID")]
        public class Employee
        {
            [Column("ProductID")]
            public Int ID { get; set; }
    
            [Column("Product")]
            public string Product{ get; set; }
    
        }
    }
    
  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 30, 2015 @ 13:18
    Hendy Racher
    0

    Hi David,

    Does the following work ?

    var products = dataContext.Query<Employee>("SELECT * FROM Products");
Please Sign in or register to post replies

Write your reply to:

Draft