I am in search of an good approach to import data from ms access and bind it to any Model of an MVC pattern
Here is the approach which we are thinking to following
Approach 1 :
Approach 2 :
Which approach is better and how I can Implement it?
UPDATE: I have implemented Approach 2 and its works fine , does anyone know how to implement Approach 1
Access can only import very specifically-formatted html files.
Let us look at a simple example of data export from Access. Open your database where you want to export the data from. In the Navigation Pane, select the object that you want to export the data from. You can export the data from table, query, form, and report objects etc.
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'.
If this is the case, then Approach 2 is recommended (and you've stated you have done this so all is good).
Approach 1 may work ok-ish for smaller sized databases but:
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:
public class MvcApplication : System.Web.HttpApplication {
public static List<MyTable1> globalTable1;
public static List<MyTable2> 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
}
Then in your controllers:
public ActionResult Index()
{
return View(MvcApplication.globalTable1);
}
And your view:
@model List<MvcApplication1.MvcApplication.MyTable1>
@{
ViewBag.Title = "Index";
}
<h2>Blah</h2>
<ul>
@foreach (var i in Model) {
<li>@i.idField - @i.contentField </li>
}
</ul>
(Did I mention don't do this?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With