Forum Discussion

MikeSchmid's avatar
MikeSchmid
Occasional Contributor
11 years ago

Exporting DBTables to excel

Since TestComplete does not provide a way to export the contents of DBTables I wrote my own script to export it to excel. I have one problem however. There is currently no way to retrieve the column names from DBTables. I can get the column index by passing in the column name if I already know it, but I do not want that, I want to pass in the column index and be returned the column name associated with it. Obviously it knows what to do with the column names since it can pass back an index. It needs another method to pass back the column name.

2 Replies

  • jose_pita's avatar
    jose_pita
    Super Contributor

    "Tbl.Fields.Item(i).Name" gives you the name of the column.



    function sqlQuerySelect(server, query, user)


    {


      var ConnectionString = "DRIVER=SQL Server;SERVER="+ server +";UID="+user+";PWD="+user+";


     


      // Creates a new connection


      var ConnDB = ADO.CreateConnection();


      ConnDB.ConnectionString = ConnectionString;


      ConnDB.Open();


      var resultArray = new Array();


      // Opens a recordset


      var Tbl = ConnDB.Execute(query);  


     


      // Scans all records returned by the query


      Tbl.MoveFirst();


     


      var arrIndex = 0;


      while (! Tbl.EOF)


      {


        for (i = 0; i < Tbl.Fields.Count; i++)


        {


          Log.Message(Tbl.Fields.Item(i).Name +" -> "+Tbl.Fields.Item(i).Value);


          resultArray[arrIndex] = Tbl.Fields.Item(i).Value;


        }


     


        Tbl.MoveNext();


        arrIndex++;


      }


     


      // Closes the recordset and the connection


      Tbl.Close();


      


      // Closes the recordset and the connection


      ConnDB.Close();


      return resultArray;


    }

  • MikeSchmid's avatar
    MikeSchmid
    Occasional Contributor
    I see how that would work for a sql database table. But how does that work with a TestComplete DBTables store?