我想從存儲過程輸出,但在C#它給了我0.1爲什麼在c#級輸出參數返回0?
public short SelectOccupantTypesByOccupantTypeID(Int64 OccupantID)
{
SqlCommand SqlCom = new SqlCommand("SelectOccupantTypesByOccupantTypeID", DatabaseConnection.OpenConnection());
SqlCom.CommandType = CommandType.StoredProcedure;
SqlCom.Parameters.AddWithValue("@pk_Occupants_OccupantID", OccupantID);
SqlParameter sqlParamOccupantTypeID = new SqlParameter("@OccupantTypeID", SqlDbType.SmallInt);
sqlParamOccupantTypeID.Direction = ParameterDirection.Output;
SqlCom.Parameters.Add(sqlParamOccupantTypeID);
short OccupantTypeID = Convert.ToInt16(sqlParamOccupantTypeID.Value);
SqlCom.ExecuteNonQuery();
DatabaseConnection.CloseConnection();
return OccupantTypeID;
}
試圖讓它
protected void ddlOccupants_SelectedIndexChanged(object sender, EventArgs e)
{
Int64 OccupantID= Convert.ToInt64(ddlOccupants.SelectedValue);
Int16 OccupantTypeID= MngOccupants.SelectOccupantTypesByOccupantTypeID(OccupantID);
txtBoxMonthlyInstallment.Text = OccupantTypeID.ToString();
}
SP:
alter PROCEDURE [dbo].[SelectOccupantTypesByOccupantTypeID]
@pk_Occupants_OccupantID bigint,
@OccupantTypeID smallint output
AS
BEGIN
Set @OccupantTypeID= (Select Occupants.OccupantsOccupantTypeID
From Occupants
Where Occupants.pk_Occupants_OccupantID= @pk_Occupants_OccupantID)
return @OccupantTypeID
END
但SP返回正確的值在SQL級別但在C#級別返回0。爲什麼?
您需要調用'ExecuteNonQuery' _before_您讀取值... – petelids
仔細檢查您傳遞的參數並提取數據邏輯。如果DBMS說它返回零,它將返回零。確保使用配置文件工具。 – jean
@jean這不一定是正確的。如果整數的值沒有被db調用覆蓋,它將返回默認值,對於整數類型,默認值爲0,這意味着petelids評論是答案。 –