Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

.NET Forum


You are currently viewing our .NET Forum as a guest. Please register to participate.
Login



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

Posts: 1,186
Location: Manchester, UK
Trades: 0
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!
 
 
Register now for full access!
Old 03-01-2006, 06:00 AM Re: Check for existence
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
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!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 03-01-2006, 08:07 AM Re: Check for existence
King Spam Talker

Posts: 1,186
Location: Manchester, UK
Trades: 0
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, 01:54 PM Re: Check for existence
sdcdesign.co.uk's Avatar
Extreme Talker

Posts: 198
Location: High Wycombe, Buckinghamshire, London
Trades: 0
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, 03:56 AM Re: Check for existence
King Spam Talker

Posts: 1,186
Location: Manchester, UK
Trades: 0
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 Search this Thread
Search this Thread:

Advanced Search

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

BB 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.47928 seconds with 12 queries