{"id":152,"date":"2005-02-16T11:34:00","date_gmt":"2005-02-16T19:34:00","guid":{"rendered":"http:\/\/spacefold.com\/colin\/morethanfour\/?p=152"},"modified":"2020-08-16T11:35:36","modified_gmt":"2020-08-16T19:35:36","slug":"vfp-oledbdatareader-and-returning-count","status":"publish","type":"post","link":"https:\/\/spacefold.com\/colin\/morethanfour\/2005\/02\/16\/vfp-oledbdatareader-and-returning-count\/","title":{"rendered":"VFP OleDbDataReader and returning COUNT(*)"},"content":{"rendered":"\n<p>I found out something interesting about the VFP OleDb driver yesterday. I&#8217;m using a DataReader to return records from a FoxPro table, and one thing about DataReaders in .NET 1.1 is that you can&#8217;t get a record count. (There&#8217;s a .RowsAffected property but that&#8217;s different, as I understand it.)<\/p>\n\n\n\n<p>The usual thing to do is to perform a pre-select to get the record count, and use ExecuteScalar() to return the single count value:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">StringBuilder sb = new StringBuilder();\nsb.Append(\"SELECT COUNT(*) FROM customers WHERE name = '\");\nsb.Append(  cCustomer ) ;\nsb.Append(  \"'\") ;\n\noCmd = new System.Data.OleDb.OleDbCommand( sb.ToString(), oVfpConn ) ;\n\nint resultCount = (int) oCmd.ExecuteScalar();  \n<\/pre>\n\n\n\n<p>This code works fine if you&#8217;re using a SqlClient.SqlDataReader against SQL Server, but here, we get an exception: &#8220;specified cast is not valid&#8221;.<\/p>\n\n\n\n<p>It turns out that, for the VFP OleDb source, oCmd.ExecuteScalar() returns a System.Decimal object instead of an int. After some experiments, I found this seemed to work best:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\t\nint resultCount = System.Decimal.ToInt32( (Decimal) oCmd.ExecuteScalar() ); \n\nif ( resultCount == 0 )\n{\n\t...\n<\/pre>\n\n\n\n<p>This is fun!<\/p>\n\n\n\n<p>Update: David Stevenson&nbsp;<a href=\"http:\/\/talkingfox.blogspot.com\/2006\/07\/getting-integer-count-from-vfp-ole-db.html\">has a better solution<\/a>: Use CAST to force the results to an Integer:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\t\nStringBuilder sb = new StringBuilder();\nsb.Append(\"SELECT CAST(COUNT(*) AS Integer) FROM customers WHERE name = '\");\nsb.Append(  cCustomer ) ;\nsb.Append(  \"'\") ;\n\noCmd = new System.Data.OleDb.OleDbCommand( sb.ToString(), oVfpConn ) ;\n\nint resultCount = (int) oCmd.ExecuteScalar();  \n<\/pre>\n\n\n\n<p>Much cleaner.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I found out something interesting about the VFP OleDb driver yesterday. I&#8217;m using a DataReader to return records from a FoxPro table, and one thing about DataReaders in .NET 1.1 is that you can&#8217;t get a record count. (There&#8217;s a .RowsAffected property but that&#8217;s different, as I understand it.) The usual thing to do is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-foxpro","post-preview"],"_links":{"self":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":0,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/spacefold.com\/colin\/morethanfour\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}