Skip to content
Advertisement

How to Count from sql as a value in Npsql

I am using Npgsql to access postgresql database

and I am running a query:

int a = connection.Execute(@"SELECT count(*) FROM account AS a WHERE a.account_name = 'food'");

The query will return value 4, but my a gets -1.what is the problem here and how can I solve it?

Advertisement

Answer

It looks like you are using Dapper. If so, you want QueryFirst not Execute, as Execute just returns the number of rows

int a = connection.QueryFirst<int>(@"
SELECT count(*)
FROM account AS a
WHERE a.account_name = 'food'
");
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement