如何在不使用实体框架的情况下使用asp.net MVC5从SQL Server显示表?

时间:2022-07-22 09:46:39

I'm kind of new ASP.NET MVC 5, but I already have working code in a controller to access database and can perform queries and stored procedure. But after searching Google for answers, I'm still lost on how to be able to retrieve data from my database and display it in a table format without using Entity Framework due to reasons from manager.

我是一个新的ASP。NET MVC 5,但是我已经在一个控制器中有了工作代码来访问数据库,并且可以执行查询和存储过程。但是在搜索了谷歌找到答案后,由于manager的原因,我仍然不知道如何从数据库中检索数据并以表格式显示数据。

In simple words, I just want to do something as simple as select * from table and be able to display the data in a table format.

简单地说,我只想做一些简单的事情,如从表中选择*,并能够以表格式显示数据。

Here's what I have tried:

以下是我尝试过的:

I tried to create a model class via ADO.NET and got the following 2 classes:

我试图通过ADO创建一个模型类。NET获得了以下两个类:

namespace AsyncFileProcessor.Models
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class AsyncFileProcessingStatus : DbContext
    {
        public AsyncFileProcessingStatus()
            : base("name=AsyncFileProcessingStatus")
        {
        }

        public virtual DbSet<AsyncFileProcessingQueue> AsyncFileProcessingQueues { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }
}

namespace AsyncFileProcessor.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("AsyncFileProcessingQueue")]
    public partial class AsyncFileProcessingQueue
    {
        public int ID { get; set; }

        [Required]
        [StringLength(256)]
        public string Filename { get; set; }

        [StringLength(256)]
        public string Path { get; set; }

        public int Status { get; set; }

        public DateTime CreatedDate { get; set; }

        public int CreatedById { get; set; }

        public DateTime UpdatedDate { get; set; }

        public int UpdatedById { get; set; }
    }
}

Then I manually created a controller:

然后我手工创建了一个控制器:

namespace AsyncFileProcessor.Controllers
{
    public class FileProcessStatusController : Controller
    {
        // GET: FileProcessStatus
        public ActionResult Index()
        {
            return View();
        }
    }
} 

For view, I just want the table to be displayed in Index.cshtml

对于视图,我只是希望将表显示在Index.cshtml中。

@{
    ViewBag.Title = "DynamicFileUploader";
}
              //Stuff here....

                <div class="col-lg-6">
                    <h1 style="text-align:center;">Placeholder</h1>
                    // The table would go inside this section
                </div>

I tried @model IEnumerable<AsyncFile.....> in Index.cshtml, but it doesn't recognize anything related to my model.

我试着@ model IEnumerable < AsyncFile .....在指数>。cshtml,但它不认识任何与我的模型相关的东西。

Tried a lot of solutions online, but my lack of knowledge about MVC5 fails me.

我在网上尝试了很多解决方案,但是我对MVC5的了解不够。

If someone can help me figure out how I can accomplish this and maybe explain the whole MVC5 workflow in simple terms, I would appreciate it.

如果有人能帮我解决这个问题,或者用简单的术语解释整个MVC5工作流,我会很感激。

I can do this easily in Django :(

在Django,我可以很容易地做到这一点。

2 个解决方案

#1


3  

You can use Dapper ORM developed by StackExchange.

您可以使用StackExchange开发的Dapper ORM。

It basically maps an object-oriented domain model to a traditional relational database. Otherwise, you will have to use ADO.Net.

它基本上是将面向对象的域模型映射到传统的关系数据库。否则,您将不得不使用ADO.Net。

For example,

例如,

public async Task<IList<AsyncFileProcessingQueue>> GetAsyncFileProcessingQueue()
{
   IList<AsyncFileProcessingQueue> result;
   using (IDbConnection conn = new SqlConnection(DataConnectionString))
   {
      conn.Open();

      var entities = await conn.QueryAsync<AsyncFileProcessingQueue>
          ("SELECT * FROM YOUR_TABLE");

      result = entities.ToList();
   }
   return result;
}

Dapper is lightweight and very fast. I use it, if I do not have control over Database or Database is not normalized.

Dapper是轻量级的而且非常快。我使用它,如果我对数据库没有控制,或者数据库没有规范化。

#2


0  

If you want to write less SQL, but still have Dapper-like performance, you can use Tortuga Chain.

如果您希望编写更少的SQL,但仍然具有类似dap的性能,那么可以使用Tortuga链。

using Tortuga.Chain;

static SqlServerDataSource s_Data = new SqlServerDataSource(DataConnectionString);

public async Task<IList<AsyncFileProcessingQueue>> GetAsyncFileProcessingQueue()
{
   return s_Data.From("YOUR_TABLE").ToCollection<AsyncFileProcessingQueue>().ExecuteAsync();
}

https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper

https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper

#1


3  

You can use Dapper ORM developed by StackExchange.

您可以使用StackExchange开发的Dapper ORM。

It basically maps an object-oriented domain model to a traditional relational database. Otherwise, you will have to use ADO.Net.

它基本上是将面向对象的域模型映射到传统的关系数据库。否则,您将不得不使用ADO.Net。

For example,

例如,

public async Task<IList<AsyncFileProcessingQueue>> GetAsyncFileProcessingQueue()
{
   IList<AsyncFileProcessingQueue> result;
   using (IDbConnection conn = new SqlConnection(DataConnectionString))
   {
      conn.Open();

      var entities = await conn.QueryAsync<AsyncFileProcessingQueue>
          ("SELECT * FROM YOUR_TABLE");

      result = entities.ToList();
   }
   return result;
}

Dapper is lightweight and very fast. I use it, if I do not have control over Database or Database is not normalized.

Dapper是轻量级的而且非常快。我使用它,如果我对数据库没有控制,或者数据库没有规范化。

#2


0  

If you want to write less SQL, but still have Dapper-like performance, you can use Tortuga Chain.

如果您希望编写更少的SQL,但仍然具有类似dap的性能,那么可以使用Tortuga链。

using Tortuga.Chain;

static SqlServerDataSource s_Data = new SqlServerDataSource(DataConnectionString);

public async Task<IList<AsyncFileProcessingQueue>> GetAsyncFileProcessingQueue()
{
   return s_Data.From("YOUR_TABLE").ToCollection<AsyncFileProcessingQueue>().ExecuteAsync();
}

https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper

https://github.com/docevaad/Chain/wiki/A-Chain-comparison-to-Dapper