SQL 2008: SPROC throw error so I can catch in ASP.NET
I've got this simple SPROC:
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT stat开发者_StackOverflowements.
SET NOCOUNT ON;
IF EXISTS(SELECT UserName FROM Party WHERE UserName = @UserName)
BEGIN
--This means it exists, return it to ASP and tell us
SELECT 'This record already exists!'
END
ELSE
BEGIN
--This means the record isn't in there already, let's go ahead and add it
SELECT 'Record Added'
-- Insert statements for procedure here
INSERT INTO Party
(EmailAddress, UserName, LoginPin)
VALUES (@EmailAddress, @UserName, @LoginPin)
END
END
How do I throw an exception from the SPROC so that my .NET C# app can catch the error using a TRY CATCH block?
You would use RAISERROR
in your stored procedure.
See this Microsoft article on the topic.
Also, see the MSDN reference on RAISERROR
精彩评论