Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Both approaches will require you to connect to your database and map the contents into your model. I'm assuming Approach 1 is 'when the web app starts connect and copy all the database contents into memory and access if from there' and Approach 2 is 'when I need to display some data, connect the the database and copy the specific contents to my model'.</p> <p>If this is the case, then Approach 2 is recommended (and you've stated you have done this so all is good). </p> <p>Approach 1 may work ok-ish for smaller sized databases but:</p> <ul> <li>You loose all the [acid][1]-y goodness that your database provides</li> <li>Your stuck with global collection variables - not the most loved concept in web apps</li> <li>You have an entire database unnecessarily in memory. Your slow point in web apps is usually the network, a few milliseconds to load data when needed is nothing compared with the time it takes for your html to reach the browser</li> </ul> <p>If you were to try approach one (not recommended, do not do, a kitten is harmed each time this code is run) , then the easiest way would be to have something like this in your global.asax.cs file:</p> <pre><code>public class MvcApplication : System.Web.HttpApplication { public static List&lt;MyTable1&gt; globalTable1; public static List&lt;MyTable2&gt; globalTable2; protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); var DatabaseMagic = new DatabaseAccessClass("a:\path\to\database.mdb"); globalTable1 = DatabaseMagic.getDataForTableOne(); //However you do your loading and mapping globalTable2 = DatabaseMagic.getDataForTableTwo(); //ditto } </code></pre> <p>Then in your controllers:</p> <pre><code> public ActionResult Index() { return View(MvcApplication.globalTable1); } </code></pre> <p>And your view:</p> <pre><code>@model List&lt;MvcApplication1.MvcApplication.MyTable1&gt; @{ ViewBag.Title = "Index"; } &lt;h2&gt;Blah&lt;/h2&gt; &lt;ul&gt; @foreach (var i in Model) { &lt;li&gt;@i.idField - @i.contentField &lt;/li&gt; } &lt;/ul&gt; </code></pre> <p>(Did I mention don't do this?)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload