Calling stored procedures with parameters in PetaPoco

Tech Xie picture Tech Xie · Aug 5, 2011 · Viewed 16.2k times · Source

I want to be able to call a stored proc with named parameters in PetaPoco.

In order to call a stored proc that does a search/fetch:

Can I do something like this:

return db.Fetch<Customer>("EXEC SP_FindCust",
new SqlParameter("@first_name", fName),
new SqlParameter("@last_name", lName),
new SqlParameter("@dob", dob));

Also, how can I call a stored proc that does an insert?

return db.Execute("EXEC InsertCust @CustID = 1, @CustName = AAA")

Thanks, Nac

Answer

Tech Xie picture Tech Xie · Aug 5, 2011

Update:

I tried the following for fetch and insert and it worked perfectly:

var s = PetaPoco.Sql.Builder.Append("EXEC SP_FindCust @@last_name = @0", lname);
s.Append(", @@first_name = @0", fName);
s.Append(", @@last_name = @0", lName);
s.Append(", @@dob = @0", dob);
return db.Query<Cust>(s);

This can be improved further to pass SQL parameters.