Hi !!! I have a table with two columns. The first column is called 'Login', the second column is called 'Password'. I wish with help of some query to extract how many rеcords I have in this columns. I use 'SELECT COUNT(*) FROM TableName WHERE Login = @login AND Password = @password'. However this query always returns the same result no matter is the parameters exist or not. ??? Sorry for my bad English.

Recommended Answers

All 7 Replies

'select count(login) from mytable'

I don't wish to count how many rows i have in column 'Login'. I wish to find how many rows meet of my requirements "SLECT COUNT(*) FROM TableName WHERE Login = @login AND Password = @password"

so in this case you will always get zero, or at maximum one output - because you want to count how many users has "exactly this" name, and "exactly this" password.

I dont get it really what are you trying to count here. If All the code is good, there SHOULD have been only one person with name and password - if not, your code does not do a comparison when new user want to register (if the username already exists, the new users has to pick another one). Otherwise, having login with multiple users with the same userName is pointless.
What do you think?

I would use following query to check if there is a user with same username exists. Check can be performed with 'if' statement and reader.Read() function.

SELECT * FROM table WHERE username = @username

MitjaBonca is right.

Yes I would like to count exactly how many users meet of "exactly this" name, and "exactly this" password at the same time. And yes I would like to get exactly one output (if has any records that match of this requirements) and zero (if don't).

If so, then take jugosoft`s example, a bit modiffied. As simple as that. like:

'SELECT COUNT(*) FROM TableName WHERE Login = @login AND Password = @password'

This is your example, and its a good one, even there is no need to use a Password in the query.
, so you can only do:

'SELECT COUNT(*) FROM TableName WHERE Login = @login'
 //and Login is your UserName column in the db

You quited in your 1st post:
However this query always returns the same result no matter is the parameters exist or not.

If the userName passed to the parameter exist in the db, it will return 1, if the userName does NOT exist, it will (it has to) return 0.
If there is anything else then this, there is something wrong with your db creation.

I hope this helps explaining your issue.
Mitja

OK how can I extract the returned result???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.