Hi friends,I was asked a question in one interview that is How can we connect to database in QTP.I need the script. Can anybody help me.

Showing Answers 1 - 10 of 10 Answers

gvinothk

  • Dec 11th, 2006
 

Hi,

Create a function in a VBS file with the connection string for the respective database. Pass the servername, dbname, username and password as the parameter.

Include the vbs file in your script and call the function wherever it is necessary.

Thanks

Vinoth


 

  Was this answer useful?  Yes

prasanna

  • Dec 11th, 2006
 

Hi Vinod,

Thanks for your reply, but I don't know how to code the connection string. Could you please send me the code.

Prasanna Laxmi

  Was this answer useful?  Yes

megana

  • Dec 11th, 2006
 

In the excel sheet select one cell an dright click sheet import from database

1.select specifi sql query n click on next

2. click create button

3.select machine datasource

4.select data source name what ever you want

5.write sql statement

6. click finish

it is the proces of retrivig the data from the database by connecting through the odbc.

  Was this answer useful?  Yes

Ravi

  • Dec 12th, 2006
 

HI Here the code...enjoy

 Public DSN,uid,pwd,Conn
  DSN = "Testing"
  uid = "Scott"
  pwd = "Tiger"

'*****************************************************************

  '/* Connect Database */
  '*****************************************************************
    Function Connection()
        Set conn = CreateObject("ADODB.connection")
        conn.open  "DSN=" & DSN & ";uid=" & uid & ";pwd=" & pwd & ";"
      End Function
  '*****************************************************************

Ravi Konka
Bangalore

  Was this answer useful?  Yes

gvinothk

  • Dec 12th, 2006
 

Here is the code for the vbs file.

Public Function DBQuery(dbsrv, dbname, DBUID, DBPWD, Query)
On Error Resume Next
    Dim cn
    Dim rs
    set cn=Createobject("ADODB.Connection")
    set rs=Createobject("ADODB.RecordSet")
    cn.ConnectionString = "DRIVER={SQL Server};SERVER=" & dbsrv & ";UID=" & DBUID & ";PWD=" & DBPWD & ";APP=Microsoft Open Database Connectivity;WSID=IQS;DATABASE=" & dbname & ";"
    cn.Open
    rs.Open Query, cn, 1, 2
    CValue = rs(0)
    rs.Close
    cn.Close
    Set rs = Nothing
    Set cn = Nothing
    DBQuery = CValue
End Function

Regards

Vinoth

  Was this answer useful?  Yes

prasanna

  • Dec 13th, 2006
 

Hi Vinoth,

I cann't able to understand the program, mainly connection string and open query string. Could you please tell me about those or else could you please send me any document which tells about database connection through vbscript.

-Prasanna

  Was this answer useful?  Yes

sampat

  • Dec 14th, 2006
 

i want to learn scripting in qtp.from where can i start?plz guide me as i knew only operations of such tools.

  Was this answer useful?  Yes

raghu

  • Jan 3rd, 2007
 

i think using this code we can open databse connection for when we have DSN's

set Dbobj=createobject("ADODB.connection")
Dbobj.open("dsn=raghu")
set rc=createobject("ADODB.recordset")
set rc=Dbobj.execute("select * from emp")
while rc.eof<>true
data=rtrim(rc.fields(0))&" "&rc.fields(1)
msgbox data
rc.movenext
wend

  Was this answer useful?  Yes

amninder

  • Apr 23rd, 2008
 

this code works fine...
try it.

set con=createobject("ADODB.Connection")
con.connectionstring("DSN=QT_Flight32;DBQ=C:Program FilesMercury InteractiveQuickTest Professionalsamplesflightappflight32.mdb;Driver=C:WINDOWSsystem32odbcjt32.dll;DriverId=281;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;")
con.open()
set rcrecordset=con.execute("select Customer_Name from Orders")
rcrecordset.movefirst
var=rcrecordset.fields("Customer_Name")
msgbox var
rcrecordset.close
con.close
set rcrecordset=nothing
set con=nothing

'Connectivity is done through DSN(Data Source Name)..
'you can easily create this string by using database checkpoint or output value wizard.
'Insert-->checkpoint-->database checkpoing-->select specify SQL statement manually radio button and click next-->now click on create... Button and after that click on Machine Data Source Tab and select the required database source Name(DSN).
it will display the connection string ...

I hope this will solve ur problem.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions