简体   繁体   English

在没有ORM的情况下使用ASP.NET MVC

[英]Using ASP.NET MVC without an ORM

In my ASP MVC application I'm using standard SQL (rather that Linq to SQL or other ORM) to query my database. 在我的ASP MVC应用程序中,我使用标准SQL(而不是Linq to SQL或其他ORM)来查询我的数据库。

I would like to pass the database results to my view and iterate over the results in my view. 我想将数据库结果传递给我的视图,并在我的视图中迭代结果。 But I'm not sure how to do this. 但我不知道该怎么做。 Every example I've seen passes some string or uses L2S. 我见过的每个例子都传递了一些字符串或使用了L2S。 I would like to pass something like nested Hashtables, but the only thing I can think of is to pass an SqlDataReader object to the view, but this sounds like a really bad idea. 我想传递嵌套的Hashtables之类的东西,但我唯一能想到的是将SqlDataReader对象传递给视图,但这听起来真的很糟糕。

How would I go about displaying my database results from a standard SQL query to my view? 我如何将标准SQL查询的数据库结果显示到我的视图中? I would really like use Linq or other ORM, but requirements dictate we don't (don't ask me why, I don't understand). 我真的很想使用Linq或其他ORM,但要求我们不要求(不要问我为什么,我不明白)。 I'm doing this in VB. 我在VB中这样做。 I'll try by best to convert any C# examples provided. 我会尽量转换所提供的任何C#示例。

You could create simple classes for the data you want to transfer and then populate a List of objects in your controller from a data reader manually, and then pass this to your View - eg (C# but this should be easy to convert) 您可以为要传输的数据创建简单类,然后手动从数据读取器填充控制器中的对象列表,然后将其传递给您的视图 - 例如(C#但这应该很容易转换)

// open your connection / datareader etc.

List<Customer> customers = new List<Customer>();

while(dataReader.Read())
{
 Customer c = new Customer();
 c.Id = dataReader.GetInt32(0);
 c.Name = dataReader.GetString(1);
 // etc (you might want to use string indexers instead of ints for the get methods)

 customers.Add(c);
}

// close and dispose your datareader / connection etc as usual

return View("List", customers);

MVC is about separation of concerns. MVC是关于关注点的分离。 Passing SqlDataReaders, DataTables, or whatever class that resides in the System.Data namespace to a view is not a good idea. 将SqlDataReaders,DataTables或驻留在System.Data命名空间中的任何类传递给视图并不是一个好主意。 You need to define a model which might talk to the database, and a controller which will pass this model to the view. 您需要定义可能与数据库通信的模型,以及将此模型传递给视图的控制器。 If your company policy says don't use an ORM then maybe classic WebForms are better suited to your scenario than the MVC pattern. 如果您的公司政策说不使用ORM,那么经典WebForms可能比MVC模式更适合您的场景。

I agree with Rashack. 我同意拉沙克的观点。 This article explains it in some detail. 本文将详细解释它。 link text 链接文字

In a nutshell, here's how to do it using DataTable and DataReader: 简而言之,以下是使用DataTable和DataReader的方法:

private DataTable GetData()
{
    DataTable dt = new DataTable();

    using (SqlConnection connection
             = new SqlConnection("ConnectionString"))
    using (SqlCommand command = new SqlCommand())
    {
        command.Connection = connection;
        command.CommandText = "SELECT * FROM Customers";

        connection.Open();
        using (SqlDataReader reader =
            command.ExecuteReader
                (CommandBehavior.CloseConnection))
        {
            dt.Load(reader);
        }
    }

    return dt;
}

Then, you can read that DataTable into an entity object that you pass around. 然后,您可以将DataTable读入您传递的实体对象中。

I think you'll find this can yield much better performance than using Linq or an ORM. 我想你会发现这可以比使用Linq或ORM产生更好的性能。

尝试使用DataTables - DataTable可以从IDataReader加载数据...(我认为该方法称为Load)

You could create your own Data Transfer Object classes and populate instances of them using ADO.Net code. 您可以创建自己的数据传输对象类,并使用ADO.Net代码填充它们的实例。 These DTO classes would be simple POCO -style classes that just contained property get/set accessors, no methods. 这些DTO类将是简单的POCO样式类,它们只包含属性get / set访问器,没有方法。 Using POCO objects is arguably preferable to DataSets/DataTables as they are lightweight (no superfluous state) and are more intuitive to work with from an object-oriented perspective. 使用POCO对象可能比DataSets / DataTables更可取,因为它们是轻量级的(没有多余的状态),并且从面向对象的角度来看更直观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM