Copied to clipboard

Flag this post as spam?

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


  • Jeff 74 posts 346 karma points
    Jul 23, 2015 @ 16:00
    Jeff
    1

    Running a custom SQL statement

    Hi,

    Does anyone know if you can use PetaPoco to run a SQL select statement/stored procedure from a Microsoft SQL server? All of the examples I've found seem to be for returning data from one table (or inserting data into a table).

    If PetaPoco can't do this does anyone know what can do it?

    Thanks, Jeff

  • Jason 122 posts 637 karma points
    Jul 23, 2015 @ 18:34
    Jason
    0

    It has been a little bit since I have done this with PetaPoco so I am going off of memory, but I believe that you can pass in a custom SQL statement and have the result populate a custom model that is not a table in the database.

    var results = DatabaseContext.Database.Fetch<MyCustomModel>("select col1, col2, col3 as [mycol] from mytable inner join blah blah blah");
    

    If I am recalling correctly, the key is that the properties in your custom model need to be named exactly the same as the columns names return from your query. Hope that helps.

  • Jason 122 posts 637 karma points
    Jul 23, 2015 @ 18:40
  • Jeff 74 posts 346 karma points
    Jul 24, 2015 @ 08:47
    Jeff
    0

    Thanks Jason, that's sorted it!

  • John Churchley 272 posts 1258 karma points c-trib
    Jul 24, 2015 @ 09:26
    John Churchley
    0

    Hi Jason,

    Are there any major advantages to using PetaPoco than WebMatrix.Data?

  • Jason 122 posts 637 karma points
    Jul 24, 2015 @ 20:50
    Jason
    0

    To be honest I am not familiar with WebMatrix.Data. Personally I would use PetaPoco since it is baked into Umbraco.

Please Sign in or register to post replies

Write your reply to:

Draft