Skip to content

Instantly share code, notes, and snippets.

@iJungleboy
Created May 12, 2014 19:41
Show Gist options
  • Save iJungleboy/8b8dd9d225aa8b5daa0c to your computer and use it in GitHub Desktop.
Save iJungleboy/8b8dd9d225aa8b5daa0c to your computer and use it in GitHub Desktop.
Blog Demo using PetaPoco in Razor with DNN to access SQL data
@functions
{
// for PetaPoco you must first create a class containing the fields you want
private class fileRecord
{
public int FileId {get;set;}
public string FileName { get; set; }
public int Size { get; set; }
public int FolderId { get; set; }
}
}
@{
var sqlCommand = "Select Top 10 * from Files Where PortalId = @0"; // PetaPoco requires numbered parameters like @0 instead of @PortalId
var db = new PetaPoco.Database(Content.ConnectionName);
var files = db.Query<fileRecord>(sqlCommand, Dnn.Portal.PortalId);
}
<div class="sc-element">
@Content.Toolbar
<h1>Simple Demo with PetaPoco Data access</h1>
<p>This demo uses PetaPoco as a mini-ORM to get the data. More info on <a href="http://www.toptensoftware.com/petapoco/" target="_blank">PetaPoco here</a>.</p>
<h2>Pros</h2>
<ol>
<li>Typed data</li>
<li>Entity-Framework-like feeling without needing pre-compile</li>
<li>Less code than the other direct data methods (SQL & DataTable)</li>
<li>Short, brief syntax</li>
<li>Would already support paging and other features (read the PetaPoco docs)</li>
</ol>
<h2>Cons</h2>
<ol>
<li>Requires you to write classes for each type you need</li>
<li>Lots of boilerplate / plumbing code for typed classes</li>
<li>Numbered Parameters @@0 instead of @@PortalId</li>
<li>Default mode with Query is forward-only like using a SQLReader</li>
<li>Can't use 2sxc pipelines</li>
<li>Can't use 2sxc-built-in serializers and other features</li>
</ol>
<h2>The top 10 files found in this portal as returned by PetaPoco</h2>
<ol>
@foreach (var file in files)
{
<li>@file.FileName</li>
}
</ol>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment