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

发布于 2024-07-18 06:37:59 字数 345 浏览 10 评论 0原文

在我的 ASP MVC 应用程序中,我使用标准 SQL(而不是 Linq to SQL 或其他 ORM)来查询数据库。

我想将数据库结果传递到我的视图并迭代我视图中的结果。 但我不知道该怎么做。 我见过的每个例子都会传递一些字符串或使用 L2S。 我想传递诸如嵌套 Hashtables 之类的东西,但我唯一能想到的就是将 SqlDataReader 对象传递给视图,但这听起来是一个非常糟糕的主意。

我如何将标准 SQL 查询的数据库结果显示到我的视图中? 我真的很想使用 Linq 或其他 ORM,但需求决定我们不这样做(不要问我为什么,我不明白)。 我在VB中做这个。 我将尽力转换提供的任何 C# 示例。

In my ASP MVC application I'm using standard SQL (rather that Linq to SQL or other ORM) to query my database.

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

How would I go about displaying my database results from a standard SQL query to my view? I would really like use Linq or other ORM, but requirements dictate we don't (don't ask me why, I don't understand). I'm doing this in VB. I'll try by best to convert any C# examples provided.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最近可好 2024-07-25 06:37:59

您可以为要传输的数据创建简单的类,然后从数据读取器手动填充控制器中的对象列表,然后将其传递给您的视图 - 例如(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);

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 - e.g. (C# but this should be easy to convert)

// 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);
初熏 2024-07-25 06:37:59

MVC 是关于关注点分离的。 将 SqlDataReaders、DataTables 或驻留在 System.Data 命名空间中的任何类传递给视图并不是一个好主意。 您需要定义一个可以与数据库通信的模型,以及一个将该模型传递给视图的控制器。 如果您的公司政策规定不要使用 ORM,那么经典 WebForms 可能比 MVC 模式更适合您的场景。

MVC is about separation of concerns. Passing SqlDataReaders, DataTables, or whatever class that resides in the System.Data namespace to a view is not a good idea. 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.

晌融 2024-07-25 06:37:59

我同意拉沙克的观点。 本文对此进行了一些详细解释。链接文本

简而言之,以下是如何使用 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;
}

然后,您可以将该 DataTable 读入您传递的实体对象中。

我想您会发现这比使用 Linq 或 ORM 能产生更好的性能。

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:

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.

I think you'll find this can yield much better performance than using Linq or an ORM.

妳是的陽光 2024-07-25 06:37:59

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

Try using DataTables - DataTable can load data from IDataReader... (I think the method's called Load)

她说她爱他 2024-07-25 06:37:59

您可以创建自己的数据传输对象类并使用ADO.Net代码填充它们的实例。 这些 DTO 类将是简单的 POCO 样式的类,仅包含属性 get/set 访问器,没有方法。 使用 POCO 对象可以说比 DataSets/DataTables 更好,因为它们是轻量级的(没有多余的状态),并且从面向对象的角度来看更直观。

You could create your own Data Transfer Object classes and populate instances of them using ADO.Net code. These DTO classes would be simple POCO-style classes that just contained property get/set accessors, no methods. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文