存储过程运行正常,但数据读取器得到错误的结果.有时



我有一个旅行预订系统的存储过程,它接收TripID并识别旅行是否是国内的(例如,所有旅行段的出发地和目的地国家是否相同(。当我从 SSMS 运行该过程时,它正确地返回 1 表示国内,0 返回国际。但是,当我尝试通过 DataReader 访问应用程序中的数据时,对于国内旅行,它不恰当地返回 0。

话虽如此,我认为问题并不完全出在 DataReader 上,因为当我将存储过程更改为立即返回 1 时,DataReader 将正确检测到此值。

任何人都可以建议更改我的代码以修复此行为吗?

下面是精简的存储过程:

SET NOCOUNT ON;
--  EXEC CheckIsDomestic 6343
Declare @HomeOffice INT = (SELECT TOP 1 o.DestinationID
FROM  TR_Trips t
JOIN TR_Travelers ta ON t.TravelerID = ta.TravelerID
JOIN TR_OfficeLocations o ON ta.OfficeID = o.Office_Loc_Id
WHERE t.TripID = @TripID)
SELECT l.Destination_ID AS DestinationID
INTO #TempDest
FROM TR_Trips t JOIN TR_Legs l ON t.TripID = l.TripID
WHERE t.TripID = @TripID                                                    
--Check whether there is a destination in the list that is different than the home country                                                      
DECLARE @CountRows int = (SELECT COUNT(*) 
FROM #TempDest t
WHERE DestinationID <> @HomeOffice )                                                        
IF @CountRows > 0
BEGIN 
SELECT 0
RETURN --tried with and without RETURN; no change
END
ELSE 
BEGIN SELECT 1
RETURN
END

以下是我的应用程序的适用部分:

public bool IsDomestic(int TripID)
{
bool ReturnValue = true;
NewStoredProcedureCommand("CheckIsDomestic");
AddParameter("@TripID", TripID, System.Data.SqlDbType.Bit);
ReturnValue = Execute_ReturnValueBool();
return ReturnValue;
}
public Boolean Execute_ReturnValueBool()
{
if (sqlCommand == null)
NewCommand();
if (sqlCommand.Connection.State != ConnectionState.Open)
sqlCommand.Connection.Open();
bool ReturnValue = false;
SqlDataReader DR = sqlCommand.ExecuteReader();
if (DR.HasRows)
{
DR.Read();
System.Diagnostics.Debug.WriteLine(DR[0]);
ReturnValue = Convert.ToBoolean(DR[0]);
}
DR.Close();
sqlCommand.Connection.Close();
return ReturnValue;
}

为什么在应用程序代码中对 TripID 参数使用 BIT 类型?尝试将其设置为 INT。

最新更新