我有一個存儲過程返回值(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;
}
當您在proc結束時選擇數字時,爲什麼不使用'ExecuteScalar'而不是'ExecuteNonQuery'?我想你可能會混淆輸出參數和結果集。 – RobH