Simplify your approach. Although it is perfectly fine to use ADO and program all this, it really isn't necessary for the simple data dump you are trying to do. Use the built-in QTP feature called "Database Output Value". This takes care of creating the database connection string and pulling the data, you just specify the fields you want to grab or enter the simple SQL statement. QTP will then execute the SQL and bring back the data which allows you to then map it to the columns in the data table. Once done and you close the window, the data will be in the data table and then you can save the file by doing the Export feature in the popup menu within the data table. Use the built- in features, it eliminates all the coding and running into syntax and/ or logic issues. Use coding where appropriate but from what you described you should be able to handle this real easy with the features I mentioned. :) --- Shawn LoPorto Learn QTP Online Today! Visit: http://www.asi-test.com/ASI/products/
On Mar 21, 12:36 pm, Shalabh Dixit <[email protected]> wrote: > Guys... > > I need to export the data from database table to a excel sheet and to > datatable as well. > > Request you to please provide your valuable inputs... > > Below is the code I tried but didn't worked... :( > > Set objDB = CreateObject("ADODB.Connection") > objDB.ConnectionString = "Provider=SQLOLEDB.1;Password=1234;Persist > Security Info=True;User ID=sa;Initial Catalog=AdventureWorks;Data > Source=SHALABHDIXIT-PC" > objDB.Open > > If objDB.state=1 Then > msgbox("Connection Is Establsihed") > else > msgbox("Connection is not opened") > exittest > End If > > sql_query = "select DepartmentID, Name from HumanResources.Department" > > Set objResults = objDB.Execute(sql_query ) > > Set s=datatable.GetSheet ("Global") > Set deptID=s.addparameter("DepartmentID", " ") > Set deptName=s.addparameter("Name", " ") > > Do Until objResults.EOF > DataTable.SetNextRow > deptID.value = objResults("DepartmentID") > deptName.value = objResults("Name") > msgbox deptID & " " & " "& deptName > objResults.MoveNext > Loop > > DataTable.ExportSheet "E:\DATA\Test.xls" > msgbox "The file is saved in E:\DATA\Test.xls" > > objResults.Close > objDB.Close -- You received this message because you are subscribed to the Google "QTP - HP Quick Test Professional - Automated Software Testing" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/MercuryQTP?hl=en
