来自 proc 的 asp.net 多个结果集:是否有必要将结果映射到类?如果是这样,怎么办?

发布于 2024-10-04 09:52:30 字数 1639 浏览 0 评论 0原文

请考虑以下场景。

您有一个存储过程返回多个结果集 出于报告目的而运行查询。

该过程还使用表值参数(SQL 2008)。 http://www.sommarskog.se/arrays-in-sql-2008 .html#LINQ_EF

这些结果集是只读的,这意味着您不必 担心更新、删除或插入。

这些结果集不映射到数据库中的任何表; 这些是报告结果,而不是数据库表的镜像。

现在,我的背景是asp classic,而我一直在努力阅读 在所有 .NET 数据访问策略上,似乎: (1) MS正在推Entity Framework (2) MS有过多的数据访问/架构策略 (3)整个话题是一个持续争论的话题 (4) 项目的情况有助于决定正确的数据访问策略, 并且 ORM 似乎没有针对这种数据访问场景进行优化 (5) 没有用于映射多个结果集存储过程的开箱即用的解决方案

此外,我正在寻找对 html 输出的完全控制,因此如果使用后面的代码, 仅中继器和文字可用于数据绑定。

我发现 Web 窗体编程模型无法提供完全分离 代码和内容,并且可以像 asp classic 一样“意大利面条”。 (MVC 方法看起来更像是 asp classic,但这个项目已经是 WebForms 了。)

我认为通过数据访问和内联脚本 <% %> 可以很容易地实现这一点。在同一页上。 这是会产生的代码类型的示例:

<%
' data access (skipping a bunch of code)....
Dim myDataSet As New DataSet()
myCommand.Fill(myDataSet)
'...etc.    

Teachers = myDataSet.Tables(0).Rows
Students = myDataSet.Tables(1).Rows
'... etc.%>

然后

<%  If Teachers.Count > 0 Then%>
<table><%  For Each _Teachers In Teachers%>
    <tr>
        <td><%= _Teachers(0)%></td>
        <td><%= _Teachers(1)%></td>
    </tr><% Next %>    
</table>
<%  Else%>Hey, there's no records.<% End If %> 

(当我尝试在“Protected Sub Page_Load ...”下的代码中分离数据访问时, 我在 .aspx 页面中使用的代码背后的变量不断给出错误: “[变量] 未声明。由于其保护级别,它可能无法访问。”)

如果过程中的结果集不是强类型的, 这是世界末日吗,糟糕的编程实践,可维护性的噩梦, 最糟糕的选择等等?

如果存储过程的结果集应该是强类型的, 那么最有效的方法是什么? (我不是 查找有关此主题的简单教程。)

提前感谢您的任何帮助。

Please consider the following scenario.

You have a stored proc returning multilple result sets for
queries run for reporting purposes.

The proc also use table valued parameters (SQL 2008).
http://www.sommarskog.se/arrays-in-sql-2008.html#LINQ_EF

These result sets are read-only, meaning you don't have to
worry about updates, deletes, or inserts.

These result sets do not map to any table in the Database;
these are reporting results, not mirrors of database tables.

Now, my background is asp classic, and while I have been trying to read up
on all the .NET data access strategies, it seems:
(1) MS is pushing Entity Framework
(2) MS has a plethora of data access/architecture strategies
(3) the entire topic is a continual subject of debate
(4) the circumstances of the project help dictate the proper data access strategy,
and ORM does not seem optimized for this data access scenario
(5) there is no out-of-the-box solution for mapping multiple result set stored procs

Also, I'm looking for full control over the html output, so if using code behind,
only Repeaters and Literals would be used for databinding.

I find the Web Forms programming model fails to deliver complete separation
of code and content, and can be just as "spaghetti" as asp classic. (The MVC
approach seems much more like asp classic, but this project is already WebForms.)

I think this would be fairly easy to implement by having data access and inline scripting <% %> on the same page.
Here's an example of the kind of code that would result:

<%
' data access (skipping a bunch of code)....
Dim myDataSet As New DataSet()
myCommand.Fill(myDataSet)
'...etc.    

Teachers = myDataSet.Tables(0).Rows
Students = myDataSet.Tables(1).Rows
'... etc.%>

then

<%  If Teachers.Count > 0 Then%>
<table><%  For Each _Teachers In Teachers%>
    <tr>
        <td><%= _Teachers(0)%></td>
        <td><%= _Teachers(1)%></td>
    </tr><% Next %>    
</table>
<%  Else%>Hey, there's no records.<% End If %> 

(When I try to separate the data access in code behind under the "Protected Sub Page_Load...",
the variables in the code behind that I use in the .aspx page keep giving the error:
"[variable] is not declared. It may be inaccessible due to its protection level.")

If the result sets from the proc are not strongly typed,
is it the end of the world, awful programming practice, a maintainability nightmare,
the worst possible choice, etc.?

If the result sets from the stored proc ought to be strongly typed,
then what is the most effective way of going about this? (I'm not
finding straightforward tutorials on this topic.)

Thanks in advance for any help.

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

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

发布评论

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

评论(1

独孤求败 2024-10-11 09:52:30

听起来您好像在问是否应该继续对数据使用 ADO.NET 容器或自定义域类。

如果您觉得需要类的强类型、输入的某些验证或在显示之前应用于数据的其他逻辑,那么请考虑编写一个方法来将 DataTable 转换为 YourClass 的集合。否则,如果这是为了显示,那么坚持使用 DataTable 并没有什么问题。如果您要为其他组件提供接口,或者需要类提供的功能,请选择类的强类型。

public IEnumerable<Teacher> ConvertToTeachers(DataTable dt)
{
    foreach (var row in dt.Rows.AsEnumerable())
    {
        //create a teacher from this row. modify row indexers as required.
        yield return new Teacher{ TeacherName = row["Name"].Value,
                                  Location = row["Location"].Value };
    }   
}

在表示层,考虑利用 ASP.NET Webforms 为您提供的服务器控件。它们提供数据绑定功能,消除所有循环。

  • 确保您的网格视图是按照您的喜好设计的。包含所需数据的适当列:常规绑定字段和超链接。

  • 将您的数据绑定到网格。

 gridViewTeachers.DataSource = myDataSet.Tables(0).Rows
 gridViewTeachers.DataBind

It sounds like you're asking whether you should continue to use ADO.NET containers or custom domain classes for your data.

If you feel you need the strong typing of a class, and some validation of input, or other logic applied to your data before display, then consider writing a method to convert a DataTable into a collection of YourClass. Otherwise, if this is for display, then there's nothing wrong with sticking with the DataTable. If you're providing an interface for other components, or need the features that a class would give you, go for the strong typing of a class.

public IEnumerable<Teacher> ConvertToTeachers(DataTable dt)
{
    foreach (var row in dt.Rows.AsEnumerable())
    {
        //create a teacher from this row. modify row indexers as required.
        yield return new Teacher{ TeacherName = row["Name"].Value,
                                  Location = row["Location"].Value };
    }   
}

On the presentation tier, consider leveraging the server controls that ASP.NET webforms provides you. They provide data-binding capabilities that remove all that looping.

  • Ensure your gridview is designed as you like. Proper columns containing the data you want: regular bound fields and hyperlinks.

  • bind your data to the grid.

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