GetScalar return type

Posts   
 
    
kris_s
User
Posts: 5
Joined: 28-Jun-2006
# Posted on: 28-Jun-2006 14:31:53   

I'm using SQL Server 2000 and have a table with primary key of type bigint. When I'm using CountRow aggregate function, the returned type from function GetScalar is Int32. What to do to have it return Int64?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Jun-2006 14:49:26   

I think the GetScalar method returns an object that you may cast it to whatever is really returned from the ADO.NET. GetScalar() does not transform what ADO.NET returns to any other type.

kris_s
User
Posts: 5
Joined: 28-Jun-2006
# Posted on: 28-Jun-2006 15:41:33   

GetScalar returns an Object. I can cast it to int, but cast to long gives an InvalidCastException.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Jun-2006 15:57:26   

Then int is what ADO.NET returns when it count the rows of your table. Which might make sense. Rows count is not related to the PK dataType.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 28-Jun-2006 16:27:32   

you could do: long value = Convert.ToInt64(returnedObject);

wink

Frans Bouma | Lead developer LLBLGen Pro
kris_s
User
Posts: 5
Joined: 28-Jun-2006
# Posted on: 28-Jun-2006 16:35:26   

Yes, i know, but if the returned object has range of Int32 than there is no sense convering it. This doesn't resolve my problem.

When the number of rows exceeds maximal value of Int32 I won't be able to correctly count them?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 28-Jun-2006 17:30:01   

kris_s wrote:

Yes, i know, but if the returned object has range of Int32 than there is no sense convering it. This doesn't resolve my problem.

When the number of rows exceeds maximal value of Int32 I won't be able to correctly count them?

As Walaa said: the GetScalar method returns the value returned from the ADO.NET command's ExecuteScalar method. This means that if you execute it against SqlServer and SqlServer returns an int32, you will get an int32. If it returns a bit, you'll get a boolean. So if you in this case get an int32, SqlServer has returned an int32 on the query.

Frans Bouma | Lead developer LLBLGen Pro
kris_s
User
Posts: 5
Joined: 28-Jun-2006
# Posted on: 28-Jun-2006 17:59:51   

Okej, thanks. I will figure out something sunglasses

kris_s
User
Posts: 5
Joined: 28-Jun-2006
# Posted on: 29-Jun-2006 12:55:08   

Okej, what I found is that COUNT returns int. There is a function COUNT_BIG which works exactly like COUNT but returns bigint (I know it exists on sql server, don't know on what other db egines). So, I'll just write a simple stored procedure and return bigint from it.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 29-Jun-2006 13:52:46   

If you expect more than 2 billion rows in your table, you should, otherwise it's IMHO not that necessary wink

Frans Bouma | Lead developer LLBLGen Pro