Mega Code Archive

 
Categories / C# Tutorial / ADO Net
 

DataReader has records using HasRows property

using System; using System.Data; using System.Data.SqlClient;     class Program     {         static void Main(string[] args)         {             string sqlConnectString = "Data Source=(local);" +                 "Integrated security=SSPI;Initial Catalog=AdventureWorks;";             string sqlSelect = "SELECT * FROM Person.Contact";             string sqlSelectEmpty = "SELECT * FROM Person.Contact";             SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);             DataTable dt = new DataTable( );             da.Fill(dt);             Console.WriteLine("DataTable has records = {0}", dt.Rows.Count > 0);             using (SqlConnection connection = new SqlConnection(sqlConnectString))             {                 SqlCommand command = new SqlCommand(sqlSelect, connection);                 connection.Open( );                 SqlDataReader dr = command.ExecuteReader( );                 Console.WriteLine(dr.HasRows);                 Console.WriteLine(dr.Read( ));                 dr.Close( );             }             da = new SqlDataAdapter(sqlSelectEmpty, sqlConnectString);             dt = new DataTable( );             da.Fill(dt);         }     }