OleDBCommand.ExecuteScalar() always returns 0

  • Thread starter Thread starter Patrick Questembert
  • Start date Start date
P

Patrick Questembert

I am running MySQL 4.1 Gamma on WinXP under VS 2003 / ADO.NET and trying to
use the OleDb ExecuteScalar() method (as opposed to the DataReader) and
consistently failing - getting 0 always.

Here is the code:

catCMD.CommandText = "SELECT count(*) from MyTable;"
long N = (long)catCMD.ExecuteScalar();

N is always 0, even though there are rows in the table.

As a side issue, I find it rather painful to guess the type of data items
returned by the OleDb objects ... initially I had:

int N = (int)catCMD.ExecuteScalar();

This caused an exception - as if C# can't handle a conversion from int64 to
int32 ...

Thanks!
Patrick
 
Patrick Questembert said:
I am running MySQL 4.1 Gamma on WinXP under VS 2003 / ADO.NET and trying to
use the OleDb ExecuteScalar() method (as opposed to the DataReader) and
consistently failing - getting 0 always.

Here is the code:

catCMD.CommandText = "SELECT count(*) from MyTable;"
long N = (long)catCMD.ExecuteScalar();

N is always 0, even though there are rows in the table.

Does it work with simple select?
As a side issue, I find it rather painful to guess the type of data items
returned by the OleDb objects ... initially I had:

int N = (int)catCMD.ExecuteScalar();

This caused an exception - as if C# can't handle a conversion from int64
to int32 ...

You might try using Convert.ToInt32 method.
 
Interesting that you should ask if it works with a simple SELECT, because I
actually also have a problem where any SUM() as the top-level SELECT always
returns an empty row (.FieldCount = 0). I haven't found a solution to this,
so now I wrap such selects into an outer select. I did that in this case as
well (even though it's a count(*) and not a sum()) as follows:

SELECT t1.mycount from(SELECT count(*) as mycount FROM MyTable) AS t1;
But it fails just the same.

Could it have anything to do with the MySQL OLE DB driver I am using (a
rather old one) or it this strictly an issue within ADO .NET and the MySQL
server?

Thanks,
Patrick
 
Back
Top