Created
May 12, 2014 19:35
-
-
Save iJungleboy/d35bf8f71c5236169185 to your computer and use it in GitHub Desktop.
Blog - Demo how to use a DataTable in Razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Configuration | |
@using System.Data | |
@using System.Data.SqlClient | |
@{ | |
var conString = ConfigurationManager.ConnectionStrings[Content.ConnectionName].ToString(); | |
var sqlCommand = "Select Top 10 * from Files Where PortalId = @PortalId"; | |
var adapter = new SqlDataAdapter(sqlCommand, conString); | |
adapter.SelectCommand.Parameters.Add("@PortalId", Dnn.Portal.PortalId); | |
var fileTable = new DataTable(); | |
adapter.Fill(fileTable); | |
// for the demo, apply some operation to the data | |
fileTable.DefaultView.Sort = "FileName DESC"; | |
} | |
<div class="sc-element"> | |
@Content.Toolbar | |
<h1>Simple Demo with DataTable access</h1> | |
<p>This demo accesses the data by filling it into a DataTable. </p> | |
<h2>Pro</h2> | |
<ol> | |
<li>Standard .net, no learning curve</li> | |
<li>Allows further data manipulation in memory</li> | |
<li>You can use the data a few times (reader is forward-only)</li> | |
<li>Connection handling open/close is done automatically by the Adapter</li> | |
</ol> | |
<h2>Cons</h2> | |
<ol> | |
<li>Code feels technical and maybe difficult</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 from DB</h2> | |
<ol> | |
@foreach (DataRow row in fileTable.Rows) | |
{ | |
<li>@row["FileName"]</li> | |
} | |
</ol> | |
<h2>The top 10 files found in this portal with reverse sorting</h2> | |
<ol> | |
@foreach (DataRow row in fileTable.DefaultView.ToTable().Rows) | |
{ | |
<li>@row["FileName"]</li> | |
} | |
</ol> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment