English 中文(简体)
• 如何在C#中检测数据检索,而不执行阅读
原标题:How to detect EOF on DataReader in C# without executing Read()
  • 时间:2011-10-21 02:28:46
  •  标签:
  • c#
  • ado.net

I m familiar with using .Read() to detect the EOF

        using (IDataReader reader = SqlHelper.ExecuteReader(_connectionString, "dbo.GetOrders"))
        {
            AssertOrder(reader);

            while (reader.Read())
            {
                yield return FillRecord<Order>(reader, StringComparer.OrdinalIgnoreCase);
            }
            reader.Close();
        }

由于一些令人厌恶的局势,我不得不加入,《观察》实际上推动了读者的发展。 因此,现在,在休息期间,由于我们两度前进,这实际上导致这一功能ski升。

我希望有一个IDataReader。 EOF,但只有t。 任何想法?

最佳回答

我认为,我很可能在评论中表明我的意见,但因为你要求“任何想法”。

很明显,这是第二次工作,但你想到:

(你们当然要执行可变的IDis(因为我认为IDataReader?)等。 可以证明,这几类人继承了IDataReader的遗产,并将这几类人作为学校。 或者得到真正的冷却,实施一种透明的 proxy,劫持阅读方法。

public class DataReaderWithEOF
{
     public bool EOF { public get; private set; }
     private IDataReader reader;

     public DataReaderWithEOF(IDataReader reader)
     {
          this.reader = reader;
     }

     public bool Read()
     {
           bool result = reader.Read();
           this.EOF = !result;
           return result;
     }
}
问题回答
using (IDataReader reader = SqlHelper.ExecuteReader(_connectionString, "dbo.GetOrders"))
    {
         if(!reader.HasRows)
         {
              Response.Write("EOF"); // empty
         }
         while (reader.Read()) //read only registers
         {
               yield return FillRecord<Order>(reader, StringComparer.OrdinalIgnoreCase);
          }
          reader.Close();
       }
    }

every time u use a. Read () it next record. then do if(reader.Read()) u would already be advancing a record.

“Steve s”解决办法的一个替代办法是打电话给读者。 阅读(阅读)时,FillRecord的“接近”是假的。 然后可以检查读物。 页: 1





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签