从方法返回的DataReader

编程入门 行业动态 更新时间:2024-10-27 16:37:44
本文介绍了从方法返回的DataReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下方法

public static SqlDataReader MenuDataReader(string url) { using (SqlConnection con = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand("spR_GetChildMenus", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@PageUrl", url); cmd.Parameters.AddWithValue("@MenuId", ParameterDirection.Output); cmd.Parameters.AddWithValue("@ParentId", ParameterDirection.Output); cmd.Parameters.AddWithValue("@TitleText", ParameterDirection.Output); cmd.Parameters.AddWithValue("@ExternalUrl", ParameterDirection.Output); cmd.Parameters.AddWithValue("@FullUrl", ParameterDirection.Output); cmd.Parameters.AddWithValue("@ChildCount", ParameterDirection.Output); con.Open(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { //return reader; while (reader.Read()) { return reader; } } } } return null; }

这IM调用像这样

which im calling like this

SqlDataReader reader = MenuDataReader(url); if (reader.HasRows) { while (reader.Read()) { }}

然而即时得到错误信息

however im getting the error message

无效试图调用HasRows当读者是封闭的。

Invalid attempt to call HasRows when reader is closed.

谁能帮我出

感谢

推荐答案

你真的需要的读者,还是你只是需要一些方法来遍历里面的行?我建议一个的迭代器块的。您可以将行遍历源法里面,收益各行依次调用者。

Do you really need the reader, or do you just need some way to iterate over the rows inside it? I suggest an iterator block. You can iterate over your rows inside the source method, and yield each row in turn to the caller.

有与此技术一拧:因为你的收益的相同的每次迭代,有些情况下这可能会导致一个问题,所以你最好也要求对象委托某处复制行的内容。我也喜欢到可以用于任何的查询,并使用相同的代表技术来处理参数的数据,像这样的抽象给通用方法:

There is a twist with this technique: because you're yielding the same object with each iteration, there are cases where this can cause a problem, and so you're best off also asking for a delegate to copy the contents of the row somewhere. I also like to abstract this to a generic method that can be used for any query, and use the same delegate technique to handle parameter data, like so:

private IEnumerable<T> GetRows(string sql, Action<SqlParameterCollection> addParameters, Func<IDataRecord, T> copyRow) { using (var cn = new SqlConnection("Connection string here")) using (var cmd = new SqlCommand(sql, cn) { cmd.CommandType = CommandType.StoredProcedure; addParameters(cmd.Parameters); cn.Open(); using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { yield return copyRow(rdr); } rdr.Close(); } } } public IEnumerable<MenuItem> GetChildMenus(string url) { return GetRows("spR_GetChildMenus", p => { //these lines are copied from your question, but they're almost certainly wrong p.AddWithValue("@PageUrl", url); p.AddWithValue("@MenuId", ParameterDirection.Output); p.AddWithValue("@ParentId", ParameterDirection.Output); p.AddWithValue("@TitleText", ParameterDirection.Output); p.AddWithValue("@ExternalUrl", ParameterDirection.Output); p.AddWithValue("@FullUrl", ParameterDirection.Output); p.AddWithValue("@ChildCount", ParameterDirection.Output); }, r => { return new MenuItem( ... ); } }

更多推荐

从方法返回的DataReader

本文发布于:2023-05-31 10:02:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389511.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   DataReader

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!