{"id":456,"date":"2003-07-23T11:58:28","date_gmt":"2003-07-23T15:58:28","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=456"},"modified":"2003-07-23T11:58:28","modified_gmt":"2003-07-23T15:58:28","slug":"retrieving-recordcount-property-in-net","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/07\/23\/retrieving-recordcount-property-in-net\/","title":{"rendered":"Retrieving &#8216;recordcount&#8217; property in .NET"},"content":{"rendered":"<p>Spent a bit this morning wading through .NET documentation trying to find out how one retrieves the number of records returned from a query.  Turns out it&#8217;s pretty simple.  First you get your query rolling:<\/p>\n<p>\/\/ get the connection string from settings<br \/>\nString cs = System.Configuration.ConfigurationSettings.AppSettings[&#8220;connectionString&#8221;].ToString();<br \/>\n\/\/ get a sql connection using the connection string<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDataSqlClientSqlConnectionClassTopic.asp?frame=true\">SqlConnection<\/a> sqlcon = new SqlConnection(cs);<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDataSqlClientSqlDataAdapterClassTopic.asp?frame=true\">SqlDataAdapter<\/a> adapter= new SqlDataAdapter(&#8220;select * from yourtable&#8221;, sqlcon);<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDataDataSetClassTopic.asp?frame=true\">DataSet<\/a> dataset = new DataSet();<br \/>\nadapter.Fill(dataset,&#8221;mytable&#8221;); <\/p>\n<p>At this point you&#8217;ve run the query and you have a DataSet which you can then loop over like any other collection, ie:<\/p>\n<p>foreach (<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDataDataRowClassTopic.asp?frame=true\">DataRow<\/a> d in dataset.Tables[&#8220;mytable&#8221;].Rows)<\/p>\n<p>or you can retrieve the number of rows:<\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfSystemDataDataRowCollectionClassTopic.asp?frame=true\">DataRowCollection<\/a> drc = dataset.Tables[&#8220;mytable&#8221;].Rows;<br \/>\nConsole.WriteLine(&#8220;there are &#8221; + drc.Count + &#8221; rows in the query.&#8221;);<\/p>\n<p>or even quicker like this:<\/p>\n<p>Console.WriteLine(&#8220;there are &#8221; + dataset.Tables[&#8220;mytable&#8221;].Rows.Count + &#8221; rows in the query.&#8221;);<\/p>\n<p>Back to work&#8230;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spent a bit this morning wading through .NET documentation trying to find out how one retrieves the number of records returned from a query. Turns out it&#8217;s pretty simple. First you get your query rolling: \/\/ get the connection string from settings String cs = System.Configuration.ConfigurationSettings.AppSettings[&#8220;connectionString&#8221;].ToString(); \/\/ get a sql connection using the connection string &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/07\/23\/retrieving-recordcount-property-in-net\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Retrieving &#8216;recordcount&#8217; property in .NET<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/456"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=456"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/456\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}