Last active
August 29, 2015 14:01
-
-
Save iJungleboy/0adbf20dd81a741580e6 to your computer and use it in GitHub Desktop.
Blog Demo showing how to use DataReader 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.SqlClient | |
@{ | |
var conString = ConfigurationManager.ConnectionStrings[Content.ConnectionName].ToString(); | |
var con = new SqlConnection(conString); | |
con.Open(); | |
var command = new SqlCommand("Select Top 10 * from Files Where PortalId = @PortalId", con); | |
command.Parameters.Add("@PortalId", Dnn.Portal.PortalId); | |
SqlDataReader myReader = command.ExecuteReader(); | |
} | |
<div class="sc-element"> | |
@Content.Toolbar | |
<h1>Simple Demo using DataReader access</h1> | |
<p>This demo accesses the data directly, uses a SQL parameter for the PortalId then shows the first 10 files it finds. More intro-material for direct database access in this <a href="http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C" target="_blank">article on codeplex</a>.</p> | |
<h2>Pro</h2> | |
<ol> | |
<li>Easy - copy paste etc.</li> | |
<li>Standard .net, no learning curve</li> | |
</ol> | |
<h2>Cons</h2> | |
<ol> | |
<li>Only forward looping through the reader</li> | |
<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</h2> | |
<ol> | |
@while (myReader.Read()) | |
{ | |
<li>@myReader["FileName"]</li> | |
} | |
</ol> | |
@{ | |
con.Close(); | |
} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment