如何查找Sql Datareader有多行? [英] How to Find Sql Datareader has More then One Row?

查看:95
本文介绍了如何查找Sql Datareader有多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Winform

i想知道如何在查询成功时找到Sqldatareader返回多少行。

这里我的代码:

i am Using Winform
i want to know how to find that "How many Rows are Return by Sqldatareader" when Query Succeeded.
Here my Code :

if (sqldreader.HasRows>1)
                 {
                txtid.ReadOnly=true;
                txtname.ReadOnly = true;
                mnupdate.Enabled = false;
                txtaddress.ReadOnly = true;
                txtcnic.ReadOnly = true;
                cmbcity.Enabled = false;
                txtmobile.ReadOnly = true;

                  }
                 else{
                     while (sqldreader.Read())
                           {
                    lbstatus.ForeColor = Color.Red;
                    lbstatus.Text = ""
                            }
                     }

推荐答案

在你的while循环中,你需要跟踪。只需要一个int计数器,你可以增加(sqldreader.Read())



由于DataReaders一次只能读取一条记录,所以无法获得完整的记录直到你读完所有内容。
In your while loop you'll need to keep track. Just have an int counter that you increment in while (sqldreader.Read())

Since DataReaders only read one record at a time you can't get the full record count until you have read through it all.


首先,HasRows是一个布尔字段,让你知道是/否有记录。



SQLdatareader没有用于获取记录数的属性或方法。这是因为正如MSDN文档所说,它是来自SQL Server数据库的仅向前行的流。这意味着,datareader不知道有多少记录,因为记录没有被读取或还有。你必须阅读它们才能找出它们的数量。这似乎是一种限制,但是数据集不能一次获取所有记录这一事实使得它快速有效。



如果你想要使用datareader并需要计数,您需要读取记录并随时计算。请参阅此链接以获取示例:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/dc198199-be24-473c-9973-746fe14d65ba/row-count-using -a-sqldatareader?forum = csharpgeneral [ ^ ]
First of all, HasRows is a boolean field which lets you know yes/no that there are records.

A SQLdatareader does not have a property or method to get the count of the number of records. That is because it is, as the MSDN documentation says, "forward-only stream of rows from a SQL Server database". This means, the datareader has no idea how many records there are because the records are not read or "there" yet. You have to read them to find out how many there are. This may seem like a limitation, but the fact that the dataread does not fetch all the records at once is what makes it fast and efficient.

If you do want to use the datareader and do need a count, you need to read the records and count them as you go. See this link for an example:
http://social.msdn.microsoft.com/Forums/vstudio/en-US/dc198199-be24-473c-9973-746fe14d65ba/row-count-using-a-sqldatareader?forum=csharpgeneral[^]


这篇关于如何查找Sql Datareader有多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆