2014-07-25 50 views
-2

我有一個存儲過程返回值(letterNo)沒有提供任何參數,它的工作原理,但同樣的過程在asp.net代碼中給出錯誤的值,即它只返回0但在SP中它返回期望值,如1,2等。存儲過程返回值在sql服務器,但不是在asp.net

爲什麼?

SP:

ALTER PROCEDURE [dbo].[SelectLetterNoFromComposedLetter] 

@LetterNo bigint output 

AS 
BEGIN 

    Declare @Date varchar(5) 
    Set @Date =(Select convert(Varchar(5),getdate(),110)) 

    Declare @MaxComposeLetterID bigint 
    Set @MaxComposeLetterID = (Select MAX(ComposedLetterID) from ComposedLetter) 

    Set @LetterNo= (Select Substring(ComposedLetter.LetterNo, 15,20) from ComposedLetter 
    where ComposedLetterID= @MaxComposeLetterID) 

    If (@Date !='01-01') --Check if it's first day of new year or not 
    Begin 
    Select @LetterNo + 1 as LetterNo -- if not then it returns existing letterno + 1; incrementing old value 
    End 
    Else If(@Date= '01-01') -- if current date if 1st January then firstly it checks whether any other entry has been made or not, if made then existing value + 1 
    Begin 
     If(@LetterNo > 0) --if made then existing value + 1 
     Begin 
      Select @LetterNo + 1 
     End 
     Else 
     Begin 
     Set @LetterNo = 1 -- else if no new record has been inserted on 1st January then return intial value i.e. 1 
     Select @LetterNo 
     End 
    End 

END 

的.cs: //封閉式Page_Load事件

if (!IsPostBack) 
     { 
      ManageComposedLetter mngCompLetters = new ManageComposedLetter(); 
      Int64 Letter_No = mngCompLetters.SelectLetterNoFromComposedLetter(); 
      txtLetterNo.Text = "PPO-CC/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/"+Letter_No; 

} 

業務層裏面的代碼:

public Int64 SelectLetterNoFromComposedLetter() 
     { 
      SqlCommand cmd = new SqlCommand("SelectLetterNoFromComposedLetter", DataBaseConnection.OpenConnection()); 
      cmd.CommandType = CommandType.StoredProcedure; 
      SqlParameter pLetterNo = new SqlParameter("@LetterNo", SqlDbType.BigInt); 
      cmd.Parameters.Add(pLetterNo); 
      pLetterNo.Direction = ParameterDirection.Output; 
      cmd.ExecuteNonQuery(); 
      Int64 Result = Convert.ToInt64(pLetterNo.Value); 
      return Result; 
     } 
+0

當您在proc結束時選擇數字時,爲什麼不使用'ExecuteScalar'而不是'ExecuteNonQuery'?我想你可能會混淆輸出參數和結果集。 – RobH

回答

-1

嘗試:

pLetterNo.Direction = ParameterDirection.ReturnValue; 
Int64 Result = Convert.ToInt64(cmd.Parameters["@LetterNo"].Value); 

編輯

ALTER PROCEDURE [dbo].[SelectLetterNoFromComposedLetter] 
AS 
BEGIN 

Declare @Date varchar(5) 
Set @Date =(Select convert(Varchar(5),getdate(),110)) 

Declare @MaxComposeLetterID bigint 
Set @MaxComposeLetterID = (Select MAX(ComposedLetterID) from ComposedLetter) 

Declare @LetterNo bigint 
Set @LetterNo= (Select Substring(ComposedLetter.LetterNo, 15,20) from ComposedLetter 
where ComposedLetterID= @MaxComposeLetterID) 

If (@Date !='01-01') --Check if it's first day of new year or not 
Begin 
Select @LetterNo = @LetterNo + 1 
reutrn @LetterNo -- if not then it returns existing letterno + 1; incrementing old value 
End 
Else If(@Date= '01-01') -- if current date if 1st January then firstly it checks whether any other entry has been made or not, if made then existing value + 1 
Begin 
    If(@LetterNo > 0) --if made then existing value + 1 
    Begin 
     Select @LetterNo = @LetterNo + 1 
     return @LetterNo 
    End 
    Else 
    Begin 
    Select @LetterNo = 1 -- else if no new record has been inserted on 1st January then return intial value i.e. 1 
    return @LetterNo 
    End 
End 
END 

其中:

public Int64 SelectLetterNoFromComposedLetter() 
     { 
      SqlCommand cmd = new SqlCommand("SelectLetterNoFromComposedLetter", DataBaseConnection.OpenConnection()); 
      cmd.CommandType = CommandType.StoredProcedure; 
      SqlParameter pLetterNo = new SqlParameter("@LetterNo", SqlDbType.BigInt); 
      cmd.Parameters.Add(pLetterNo); 
      pLetterNo.Direction = ParameterDirection.ReturnValue; 
      cmd.ExecuteNonQuery(); 
      Int64 Result = Convert.ToInt64(cmd.Parameters["@LetterNo"].Value); 
      return Result; 
     } 
+0

不工作,拋出錯誤, 過程或函數'SelectLetterNoFromComposedLetter'期望參數'@LetterNo',它沒有提供。 – user3518032

+0

從我可以看到你的存儲過程中,你給它一個參數'@ LetterNo',並且在'select @ LetterNo'行下面返回'@ LetterNo'行添加'return @ LetterNo' – user1

+0

你可能還需要添加'@ LetterNo'參數兩次到你的C#代碼..例如'SqlParameter pLetterNo = new SqlParameter(「@ LetterNo」,SqlDbType.BigInt);''和'SqlParameter returnValue = new SqlParameter(「@ LetterNo」,SqlDbType.BigInt);' – user1

-1

你可以嘗試設置變量聲明的,並在程序結束時選擇它。我認爲問題是存在的。我與EF有類似的問題。

0

存儲過程返回多個結果。

Select @LetterNo + 1 as LetterNo -- if not then it returns existing letterno + 1; incrementing old value 
... 
Select @LetterNo + 1 
... 
Select @LetterNo 

您應該改變他們

Select @LetterNo += 1 as LetterNo -- if not then it returns existing letterno + 1; incrementing old value 
... 
Select @LetterNo += 1 
... 
Select @LetterNo = @LetterNo 

,或者使用SET運營商

SET @LetterNo = @LetterNo + 1 
... 
SET @LetterNo = @LetterNo + 1 
... 
SET @LetterNo = @LetterNo 

最後分配新建分配FY是沒有意義的(@LetterNo = @LetterNo),但它是取自你的代碼。