Forum Discussion

ilija_panov's avatar
ilija_panov
Contributor
13 years ago

How to test in script if an ADO RecordSet object has returned records or not ?

Hi Everyone,



I have the following procedure which should check if records in a table have been inserted for a given date with a simple query (

select 1 as inserted from dbo.ExchangeRates where ValidDate = '2012-09-05'  ):




function ratesInserted(date)

{

    var Cmd, RecSet, Prm;

    Cmd = ADO.CreateCommand();

    Cmd.ActiveConnection = connectCWN();

    // Specify command type and text

    Cmd.CommandType = adCmdText;

    Cmd.CommandText = "select 1 as inserted from dbo.ExchangeRates where (ValidDate = ?)";

    // Create accountnum parameter   

    Prm = Cmd.CreateParameter("@ValidDate",adVarChar, adParamInput); 

    // Specify the parameter value

    Prm.Size = 30;

    Prm.Value = date;

    Cmd.Parameters.Append(Prm);

    // Execute the command

    RecSet = Cmd.Execute();

      if (!RecSet.Fields(0).Value.Exists)

      {

        Log.Message("false"); 

        return "false";

 

      }

      else  

      {

        Log.Message("true");   

        return "true";

       

      }

    Cmd.ActiveConnection.Close();

}




The problems is that the RecSet always alsways has a property RecSet.Exists = true whether it returns an empty  data set or not which is what I need to check in the if condition. 



If using !RecSet.Fields(0).Value.Exists or !RecSet.Fields(0).Value == null it fails when the RecSet is empty because there is no BOF EOF



Third attempt was to use the RecSet.BOF property but this is alwayds treue as well . which is kind of weird and in contradiction to the above



I would really appreciate if you can help me out which property or method of RecSet to use in order to check if it returns a data set .



Best Regards,

  Ilija


















1 Reply

  • Seems to have solved the isuse with using the EOF property:




    //ok

    function ratesInserted(date)

    {

        var Cmd, RecSet, Prm;

        Cmd = ADO.CreateCommand();

        Cmd.ActiveConnection = connectCWN();

        // Specify command type and text

        Cmd.CommandType = adCmdText;

        Cmd.CommandText = "select 1 as inserted from dbo.ExchangeRates where (ValidDate = ?)";

        // Create accountnum parameter   

        Prm = Cmd.CreateParameter("@ValidDate",adVarChar, adParamInput); 

        // Specify the parameter value

        Prm.Size = 30;

        Prm.Value = date;

        Cmd.Parameters.Append(Prm);

        // Execute the command

        RecSet = Cmd.Execute();

          if (RecSet.EOF == false)

          {

            Log.Message("true"); 

            return "true";

     

          }

          else 

          {

            Log.Message("false");   

            return "false";

           

          }

        Cmd.ActiveConnection.Close();

    }




    Just in case it is encountered by someone else ...



    Best Regards,

       Ilija