PetaPoco with stored procedures

Posted by Jalpesh P. Vadgama on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jalpesh P. Vadgama
Published on Sun, 19 Jun 2011 20:28:15 GMT Indexed on 2011/06/20 16:24 UTC
Read the original article Hit count: 704

In previous post I have written that How we can use PetaPoco with the asp.net MVC. One of my dear friend Kirti asked me that How we can use it with Stored procedure. So decided to write a small post for that. So let’s first create a simple stored procedure for customer table which I have used in my previous post. I have written  simple code a single query that will return customers. Following is a code for that.

CREATE PROCEDURE mysp_GetCustomers
AS
SELECT * FROM [dbo].Customer

Now our stored procedure is ready so I just need to change my CustomDB file from the my previous post example like following.

using System.Collections.Generic;

namespace CodeSimplified.Models
{
    public class CustomerDB
    {
        public IEnumerable<Customer> GetCustomers()
        {
            var databaseContext = new PetaPoco.Database("MyConnectionString");
            return databaseContext.Query<Customer>("exec mysp_GetCustomers");

        }
    }
}
That's It. Now It's time to run this in browser and Here is the output

Output

In future post I will explain How we can use PetaPoco with parameterised stored procedure. Hope you liked it.. Stay tuned for more.. Happy programming.

Shout it

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about ASP.NET 4.0