database constraints, quick ways???

  • Thread starter Thread starter William Ryan
  • Start date Start date
W

William Ryan

I'm assuming you have your key already set on the server.


Check this link and about half way down the code, you'll
see how to do it.
http://samples.gotdotnet.com/quickstart/howto/doc/adoplus%
5CUpdateDataFromDB.aspx
You can do it after the fact if you want, which gives you
much more flexibility depending on what you want to
accomplish. http://msdn.microsoft.com/library/default.asp?
url=/library/en-
us/cpguide/html/cpcondefiningprimarykeyfortable.asp

The cool thing about ADO.NET is that can create keys and
relationships between fields that don't have them defined
in the db, and even on fields that don't exist in the
actual db.

If you have any problems let me know.

Good Luck,

Bill

W.G. Ryan
www.knowdotnet.com
 
Hi,

Thanks for your attention.

Let me completely redescribe my problem:

I've a USER table which contains USERNAME column. I try to keep usernames
unique. When I'm adding I new user, I frist call my Stored Procedure who
checks if that username already exists in the DB and if no I add that user
by calling another Stored Procedure. As you see I'he called SPs two times.

In order not to do this, I can set the column unique at SQL Server 2000 and
check the errors at the program side.

Or in another way, in one SP I can frist check the username and return back
an error if it already exists, and add the row if no.


I'm dealing with a ASP.NET application and I need quick user adding
operation. I don'y want to make 2 queries in the DB.

Ozgur.
 
I'd do your last proposal as it's the most efficient:
Call the sproc to add the user (DB has UserName defined as unique) on insert
it will fail if it's a duplicate otherwise the insert will succeed.

Then you can return an affirmative or an error on your asp page. If it was a
dup, the user will have to make another round trip to you with a new
username anyway so your down to one database operation per request.

Tony
 
Thanks again.

Do you have any SQL code which catches an error? I couldn't find any.

Ozgur.
 
There are SQLException classes and OleDbException classes. If I understand
what you want to do, you can just try catch the insert operation and if you
get an exception Primary Key...then you know it exists.
 
Back
Top