通过编程导入Ms访问数据

时间:2022-07-29 11:20:44

I am in search of an good approach to import data from ms access and bind it to any Model of an MVC pattern

我正在寻找一种从ms access导入数据并将其绑定到任何MVC模式模型的好方法


Here is the approach which we are thinking to following

这是我们正在考虑遵循的方法

Approach 1 :

方法1:

  • Open Ms Access file
  • 打开女士访问文件
  • Open database
  • 开放数据库
  • Open all tables
  • 打开所有的表
  • Import data of all tables and bind them to an model
  • 导入所有表的数据并将它们绑定到一个模型
  • Close all tables
  • 关闭所有表
  • Close database
  • 关闭数据库
  • Close file
  • 关闭文件

Approach 2 :

方法2:

  • Connect Ms Access Database in Asp.Net MVC
  • 在Asp中连接Ms Access数据库。Net MVC
  • Open the database
  • 打开数据库
  • pass an query
  • 通过查询
  • fetch data and bind it to model
  • 获取数据并将其绑定到模型
  • close database
  • 关闭数据库

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

更新:我已经实现了方法2,它工作得很好,有人知道如何实现方法1吗

5 个解决方案

#1


1  

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'.

这两种方法都需要您连接到数据库并将内容映射到模型中。我假设方法1是“当web应用程序开始连接并将所有数据库内容复制到内存中并从那里访问”,方法2是“当我需要显示一些数据时,连接数据库并将特定内容复制到我的模型中”。

If this is the case, then Approach 2 is recommended (and you've stated you have done this so all is good).

如果是这种情况,那么建议使用方法2(您已经声明您已经这样做了,所以一切都很好)。

Approach 1 may work ok-ish for smaller sized databases but:

方法1可能适用于较小的数据库,但是:

  • You loose all the [acid][1]-y goodness that your database provides
  • 您失去了数据库提供的所有[acid][1]-y的好处
  • Your stuck with global collection variables - not the most loved concept in web apps
  • 你的全球收集变量——不是网络应用中最受欢迎的概念。
  • 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
  • 内存中有一个不必要的数据库。web应用程序的慢点通常是网络,在需要时几毫秒加载数据与html到达浏览器所需的时间相比是微不足道的

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:

如果你尝试接近一种方法(不推荐,不要做,每次运行这段代码时,小猫都会受到伤害),那么最简单的方法就是在你的global.asax中使用类似的方法。cs文件:

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?)

(我说过不要这样做吗?)

#2


1  

Use Entity Framework.Create ViewModel to map.

使用实体框架。创建地图视图模型。

#3


0  

What you should do is build your model according to your table. So the model class should have properties which correspond to your table fields. Then when you require the Model you would query againist the DB and populate the model's properties accordingly.

您应该做的是根据您的表构建模型。因此,模型类应该具有与表字段相对应的属性。然后,当您需要模型时,您将查询反向数据库并相应地填充模型的属性。

#4


0  

I did not understood Approach 1.

我没有理解方法1。

Is it a requirement to use Access? I have seen there are lot of problems with file based database (such as Access) so better import all the data to SQL Server or some other database from Access and then use option 2.

是否需要使用Access?我已经看到,基于文件的数据库(例如Access)存在很多问题,所以最好将所有数据导入到SQL Server或其他数据库中,然后使用选项2。

As your database already created you can use Entity Framework database first approach to bind it.

由于您的数据库已经创建,您可以使用实体框架数据库优先方法来绑定它。

#5


0  

You need to add using System.Data.OleDb; in Header file And add these Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\myAccessFile.mdb; Persist Security Info=False; line in connection string use connection string fetch
update ms-access database using OleDbCommand , OleDbConnection

需要使用System.Data.OleDb添加;在头文件中并添加这些Provider=Microsoft.ACE.OLEDB.12.0;数据源=|DataDirectory|\myAccessFile.mdb;坚持安全信息= False;连接字符串中的行使用连接字符串fetch update ms-access数据库使用OleDbCommand, OleDbConnection

and ms-access query just like sql query

以及ms-access查询,就像sql查询一样

#1


1  

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'.

这两种方法都需要您连接到数据库并将内容映射到模型中。我假设方法1是“当web应用程序开始连接并将所有数据库内容复制到内存中并从那里访问”,方法2是“当我需要显示一些数据时,连接数据库并将特定内容复制到我的模型中”。

If this is the case, then Approach 2 is recommended (and you've stated you have done this so all is good).

如果是这种情况,那么建议使用方法2(您已经声明您已经这样做了,所以一切都很好)。

Approach 1 may work ok-ish for smaller sized databases but:

方法1可能适用于较小的数据库,但是:

  • You loose all the [acid][1]-y goodness that your database provides
  • 您失去了数据库提供的所有[acid][1]-y的好处
  • Your stuck with global collection variables - not the most loved concept in web apps
  • 你的全球收集变量——不是网络应用中最受欢迎的概念。
  • 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
  • 内存中有一个不必要的数据库。web应用程序的慢点通常是网络,在需要时几毫秒加载数据与html到达浏览器所需的时间相比是微不足道的

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:

如果你尝试接近一种方法(不推荐,不要做,每次运行这段代码时,小猫都会受到伤害),那么最简单的方法就是在你的global.asax中使用类似的方法。cs文件:

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?)

(我说过不要这样做吗?)

#2


1  

Use Entity Framework.Create ViewModel to map.

使用实体框架。创建地图视图模型。

#3


0  

What you should do is build your model according to your table. So the model class should have properties which correspond to your table fields. Then when you require the Model you would query againist the DB and populate the model's properties accordingly.

您应该做的是根据您的表构建模型。因此,模型类应该具有与表字段相对应的属性。然后,当您需要模型时,您将查询反向数据库并相应地填充模型的属性。

#4


0  

I did not understood Approach 1.

我没有理解方法1。

Is it a requirement to use Access? I have seen there are lot of problems with file based database (such as Access) so better import all the data to SQL Server or some other database from Access and then use option 2.

是否需要使用Access?我已经看到,基于文件的数据库(例如Access)存在很多问题,所以最好将所有数据导入到SQL Server或其他数据库中,然后使用选项2。

As your database already created you can use Entity Framework database first approach to bind it.

由于您的数据库已经创建,您可以使用实体框架数据库优先方法来绑定它。

#5


0  

You need to add using System.Data.OleDb; in Header file And add these Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\myAccessFile.mdb; Persist Security Info=False; line in connection string use connection string fetch
update ms-access database using OleDbCommand , OleDbConnection

需要使用System.Data.OleDb添加;在头文件中并添加这些Provider=Microsoft.ACE.OLEDB.12.0;数据源=|DataDirectory|\myAccessFile.mdb;坚持安全信息= False;连接字符串中的行使用连接字符串fetch update ms-access数据库使用OleDbCommand, OleDbConnection

and ms-access query just like sql query

以及ms-access查询,就像sql查询一样