Forum Discussion

ray_mosley's avatar
ray_mosley
Frequent Contributor
13 years ago

How to execute a SQL insert from TC 9

The code segment below works(the entry IS inserted in the table).


function qaCreatePrenatalHxFolder()

{


    var sql_statement = "select * from dbo.TRK_TrackCategory where TrackCategory_Code = 'Prenatal hx'";

    var Qry;// Create a query

    Qry = ADO.CreateADOQuery();

    Qry.ConnectionString = "Driver={SQL Server};Server=" + dbServer + ";Database=" + dbName + "; Trusted_Connection=True;";

    Qry.SQL = sql_statement;

    Qry.Open();     // Execute the query

    var tmpCount = Qry.RecordCount;

    Qry.Close();    // Closes the query

    //does the Prenatal folder already exist?

    if (tmpCount == 0)

    {

       // No, create it

       Qry = ADO.CreateADOQuery();

       Qry.ConnectionString = "Driver={SQL Server};Server=" + dbServer + ";Database=" + dbName + "; Trusted_Connection=True;";

       var sql_statement = aqFile.ReadWholeTextFile("Q:\\Insert_Prenatal_HX.sql", aqFile.ctANSI);

       Qry.SQL = sql_statement;

       Qry.Open();     // Execute the query

       var tmpCount = Qry.RecordCount;

       Qry.Close();    // Closes the query

    }

}

//END OF FUNCTION qaCreatePrenatalHxFolder


It works, but TestComplete complains and terminates - as shown in the attached file - because the "Stop on error" attribute is set. I tried to find ways to reset Stop on error programmatically, but it is read-only apparently settable only from the project properties GUI of TestComplete.



How do I get my script to execute and continue?



Thanks.

















4 Replies

  • RHoward's avatar
    RHoward
    New Contributor
    Try using a different ADO object instead of the query object - ADO Command is what I use for inserts.  Assuming the file you open has an INSERT INTO statement in it, try the following.



    var sql_statement = aqFile.ReadWholeTextFile("Q:\\Insert_Prenatal_HX.sql", aqFile.ctANSI);

    var Qry = ADO.CreateADOCommand();

    Qry.ConnectionString = "Driver={SQL Server};Server=" + dbServer + ";Database=" + dbName + "; Trusted_Connection=True;";

    Qry.CommandText = sql_statement

    Qry.CommandType = cmdText;

    Qry.Execute();

    Qry.Close();



    I've never tried to use RecordCount with an ACO Command object, but since this is an insert you aren't going to get a result set anyway.
  • ray_mosley's avatar
    ray_mosley
    Frequent Contributor
    Ryan, thank you for your answer and your time. 



    I will try the different object, and I don't need the RecordCount for this insert.
  • ray_mosley's avatar
    ray_mosley
    Frequent Contributor
    Your response worked perfectly, except that Qry.Close() is not a recognized method.  Thanks.