簡體   English   中英

檢索存儲過程的輸出參數值時出錯

[英]Error in retrieving the value of the output parameter of the stored procedure

我有一個定義如下的存儲過程,以檢查重復的電子郵件是否存在:

 USE [SQL2008_850994_onebizness]
 GO
 /****** Object:  StoredProcedure [dbo].[EmailExists]    Script Date: 04/07/2013 15:14:22 ******/
 SET ANSI_NULLS ON
 GO
 SET QUOTED_IDENTIFIER ON
 GO

 ALTER PROCEDURE [dbo].[EmailExists]
 @EmailID Nvarchar(50),
 @Success int output, 
 @msg varchar(50) output  

 AS
 BEGIN
 IF EXISTS(SELECT 1 FROM dbo.Membership WHERE EmailId = @emailID) 
     OR EXISTS(SELECT 1 FROM dbo.Allocation where ResourceEmail=@emailID)
 begin
 set @Success=6
 set @msg='Duplicate Email found. Please try again.'      
 --Insert the records in the database
 end
 END

我在C#代碼中檢索成功的值,如下所示:

 int returnVal = int.Parse(myCOmmand.Parameters["@Success"].ToString());

我收到錯誤消息“輸入值的格式不正確”。

有人可以讓我知道導致錯誤的原因嗎?

您是否設置了如下參數

myCommand.Parameters.Add(new SqlParameter("@Success", SqlDbType.Int));
myCommand.Parameters["@Success"].Direction = ParameterDirection.Output;

然后檢索為

int returnVal = (int)myCommand.Parameters["@Success"].Value;

在編寫int returnVal之前,需要添加以下代碼

myCOmmand.Parameters.Add("@Success", SqlDbType.Int).Direction = ParameterDirection.Output;

接着:

int returnVal = Convert.ToInt32(myCOmmand.Parameters["@Success"].Value);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM