Почему процедура SQL возвращает 0 строк при вызове из приложения? - PullRequest
1 голос
/ 21 октября 2019

У меня есть эта процедура

ALTER PROCEDURE [dbo].[ReadEquipments] null, 'Vessel'

    @Param varchar(30),    -- EquipmentNo
    @EqptType varchar(30)
AS
BEGIN

        IF((@Param is not null OR @Param != '') AND (@EqptType is not null OR @EqptType != ''))
        Begin
              select Functionallocation as 'EquipmentNo', WinFileno as 'Functionallocation', EqptType , 
              EquipmentDescription, TrainNo, PlantNo 'PlanGroup' , PlantNo 'WorkCenter' ,Area,  * from EngineeringData 
              where EqptType = @EqptType and Functionallocation = @Param
        End
        Else IF(@Param is not null OR @Param != '')
        Begin
              select Functionallocation as 'EquipmentNo', WinFileno as 'Functionallocation', EqptType , 
              EquipmentDescription, TrainNo, PlantNo 'PlanGroup' , PlantNo 'WorkCenter' ,Area,  * from EngineeringData 
              where Functionallocation = @Param
        End
        Else IF(@EqptType is not null OR @EqptType != '')
        Begin
              select Functionallocation as 'EquipmentNo', WinFileno as 'Functionallocation', EqptType , 
              EquipmentDescription, TrainNo, PlantNo 'PlanGroup' , PlantNo 'WorkCenter' ,Area,  * from EngineeringData 
              where EqptType = @EqptType
        End
        Else IF((@EqptType is null OR @EqptType = '') AND (@Param IS NOT NULL OR @Param != ''))
        Begin
              select Functionallocation as 'EquipmentNo', WinFileno as 'Functionallocation', EqptType , 
              EquipmentDescription, TrainNo, PlantNo 'PlanGroup' , PlantNo 'WorkCenter' ,Area,  * from EngineeringData 
              where FunctionalLocation = @Param
        End
        Else IF((@Param  = '' OR @Param is null) AND (@EqptType IS NOT NULL OR @EqptType != ''))
        Begin
              select Functionallocation as 'EquipmentNo', WinFileno as 'Functionallocation', EqptType , 
              EquipmentDescription, TrainNo, PlantNo 'PlanGroup' , PlantNo 'WorkCenter' ,Area,  * from EngineeringData 
              where EqptType = @EqptType
        End

END

Если я отправлю ему параметры, то есть ноль, 'Судно'. Возвращает 21 ряд. Это нормально, но когда я звоню из applcaition.

public DT_EquipmentResponseEquipment[] Equipments(DT_EquipmentRequest req)
    {
        try
        {
        if (string.IsNullOrEmpty(req.EquipmentNumberFrom))
        {
            req.EquipmentNumberFrom = null;
        }

        if (string.IsNullOrEmpty(req.ObjectType[0]))
        {
            req.ObjectType[0] = null;
        }

        SI_GetDetails_Out_SyncService c = new SI_GetDetails_Out_SyncService();


        string strConnection = System.Configuration.ConfigurationManager.ConnectionStrings["SAPConnection"].ConnectionString;

        SqlConnection con = new SqlConnection(strConnection);

        SqlCommand sqlCmd = new SqlCommand();
        sqlCmd.Connection = con;
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.CommandText = "ReadEquipments";

        //string[] arrObjTypes = MT_EquipmentRequest.ObjectType[9];
        sqlCmd.Parameters.Add("@Param", SqlDbType.VarChar, 30).Value = null; //req.EquipmentNumberFrom;
        sqlCmd.Parameters.Add("@EqptType", SqlDbType.VarChar, 30).Value = "Vessel"; //req.ObjectType[0];

        SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

        DataTable dtRecord = new DataTable();
        sqlDataAdap.Fill(dtRecord);

        List<DT_EquipmentResponseEquipment> listResponse = new List<DT_EquipmentResponseEquipment>();

        foreach (DataRow dr in dtRecord.Rows)
        {
            DT_EquipmentResponseEquipment response = new DT_EquipmentResponseEquipment();

            response.EQUNR = Convert.ToString(dr["EquipmentNo"]);
            response.EQTYP = Convert.ToString(dr["EqptType"]);
            response.EQKTX = Convert.ToString(dr["EquipmentDescription"]);
            response.SWERK = Convert.ToString(dr["TrainNo"]);
            response.INGRP = Convert.ToString(dr["PlanGroup"]);
            response.LGWID = Convert.ToString(dr["WorkCenter"]);
            response.HEQUI = Convert.ToString(dr["Area"]);

            listResponse.Add(response);
        }



        return c.Equipment(req);
    }
    catch (Exception ex)
    {

        throw;
    }
}

Для этого он возвращает 0 строк, даже если я передаю одинаковые параметры.

sqlCmd.Parameters.Add("@Param", SqlDbType.VarChar, 30).Value = null; //req.EquipmentNumberFrom;
            sqlCmd.Parameters.Add("@EqptType", SqlDbType.VarChar, 30).Value = "Vessel"; //req.ObjectType[0];

ПОЧЕМУ? даже те же параметры и тот же тип, но все еще не являются результатом их использования приложения. Из необработанных цен возвращается нормально. Я перепробовал все.

1 Ответ

2 голосов
/ 21 октября 2019

Не устанавливать нуль в качестве значения параметра, используйте DBNull.Value

   sqlCmd.Parameters.Add("@Param", SqlDbType.VarChar, 30).Value = DBNull.Value; //req.EquipmentNumberFrom;
...