HasRows Property (SqlDataReader)
Gets a value indicating whether the
SqlDataReader contains one or more rows.
public override bool HasRows {get;}
'Declaration
Public Overrides ReadOnly Property HasRows As Boolean
Property Value
true if the
SqlDataReader contains one or more rows; otherwise,
false.
This sample shows how to optimize reading from a data source using
HasRows property.
public void FetchResults(SqlConnection myConnection)
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Test.Dept");
cmd.FetchSize = 100;
cmd.Connection = myConnection;
myConnection.Open();
try
{
SqlDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
for (int i=0;i<reader.FieldCount;i++)
{
Console.Write("\t"+reader[i]);
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("No rows detected.");
}
}
finally
{
myConnection.Close();
}
}
Public Sub FetchResults(ByVal myConnection As SqlConnection)
Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Test.Dept")
cmd.FetchSize = 100
cmd.Connection = myConnection
myConnection.Open()
Try
Dim reader As SqlDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While reader.Read()
Dim i
For i = 0 To reader.FieldCount - 1
Console.Write(String.Concat(" ", reader(i)))
Next i
Console.WriteLine()
End While
Else
Console.WriteLine("No rows detected.")
End If
Finally
myConnection.Close()
End Try
End Sub
Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2