SubSonic and Stored Procedures

限于喜欢 提交于 2019-12-04 17:11:50

If the results of the stored procedure has the same schema as one of your tables, you can build a collection using this code (SubSonic 2.1):

ProductCollection coll = new ProductCollection();
coll.LoadAndCloseReader(SPs.GetProducts(1).GetReader());

ExecuteTypedList<> is your best friend in this case:

IList<Product> list=SPs.GetProducts().ExecuteTypedList<Product>();

If my stored procedure returns all the fields from one of the tables for which I have a SubSonic object then I do a LoadAndCloseReader on the result of the stored procedure. If my stored procedure returns data that does not match a SubSonic object then I just work with it as a dataset.

Perhaps return a datareader and then iterate it to populate some custom objects. Alternatively the quick and dirty way (since you're not using domain driven design) create a view in the DB with the same structure as the stored proc, then load the result into your ViewObjectCollection similar to John's code.

You can do data readers, but that's so 1999. Returning objects is a breeze with SubSonic, and easier to use than a data reader. You can retrieve objects like so:

Dim Charts As Generic.List(Of MusicDB.Billboard) = _
    New SubSonic.Select(MusicDB.DB.Repository.Provider, New String() _
    {"Prefix", "Artist", "Track", "ArtistNarrowToken", "TrackNarrowToken", "ArtistId", "TrackId", "TrackYear"}). _
    From(MetadataTagger.MusicDB.Tables.Billboard). _
    Where(MusicDB.Billboard.Columns.ArtistNarrowToken).IsLessThan(10). _
    Or(MusicDB.Billboard.Columns.TrackId).IsNull(). _
    OrderAsc(New String() {"TrackYear"}).ExecuteTypedList(Of MetadataTagger.MusicDB.Billboard)()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!