Reply
Check for existence
Old 03-01-2006, 03:16 AM Check for existence
King Spam Talker

Posts: 1,004
Location: Manchester, UK
Hi,

I have a database of users and I want to perform a check before I add a new user to the db, to ensure that the username does not exist already.

Currently I'm using a stored procedure to return the number of rows that match the new username: if the number > 0 then I issue the error message "Achtung. User exists already etc".

It works but is this the best way?
gringo is offline
Reply With Quote
View Public Profile Visit gringo's homepage!
 
When You Register, These Ads Go Away!
     
Old 03-01-2006, 05:00 AM Re: Check for existence
Minaki's Avatar
Cheerleader

Posts: 1,626
Location: Guildford, UK
If you're using something like
SELECT Count(Username) FROM Users WHERE Username = 'NewUsersname'
then you should be alright. There's no major issue with doing it that way.

(Hint: Since you're returning a single value from a single row, you can use ExecuteScalar to improve efficiency)

Another way to do it would be to make sure all that field in the database is indexed, and then catch the errror that you get when you try to insert a duplicate username.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"
Inoxia Pyrotechnics Supplies | Surrey Angels Cheerleading Squad
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 03-01-2006, 07:07 AM Re: Check for existence
King Spam Talker

Posts: 1,004
Location: Manchester, UK
Thanks Minaki!

I didn't know whether there was some kind of 'best practice' to follow for this kind of thing.

I think I'll carry on retrieving the record count but use ExecuteScalar like you suggested.
gringo is offline
Reply With Quote
View Public Profile Visit gringo's homepage!
 
Old 03-02-2006, 12:54 PM Re: Check for existence
sdcdesign.co.uk's Avatar
Extreme Talker

Posts: 199
Location: High Wycombe, Buckinghamshire, London
The way i do it is via a datareader.

My SQL statement tries to select the user with the username they want to register, if the datareader reads, the username exists -> error message.

If not, i call another function to add the user.
__________________
[ Insert witty, yet highly intelligent signature here ]
sdcdesign.co.uk is offline
Reply With Quote
View Public Profile Visit sdcdesign.co.uk's homepage!
 
Old 03-03-2006, 02:56 AM Re: Check for existence
King Spam Talker

Posts: 1,004
Location: Manchester, UK
Yeah, that's similar to the way I was doing it. Nice to know I was on the right track!

Thanks sc.
gringo is offline
Reply With Quote
View Public Profile Visit gringo's homepage!
 
Reply     « Reply to Check for existence
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12673 seconds with 13 queries