How do you connect to database using vbscript ?

Questions by ypraveen_77   answers by ypraveen_77

Showing Answers 1 - 27 of 27 Answers

amarreddy79

  • Feb 16th, 2007
 

here is the sample script of using data base connection.........SQL="SELECT * FROM ORDERS"connection_string="QT_Flight32" isConnected = db_connect ( curConnection ,connection_string )If isConnected = 0 then ' execute the basic SQL statement set myrs=db_execute_query( curConnection , SQL ) ' report the query and the connection string Reporter.ReportEvent micInfo ,"Executed query and created recordset ","Connection_string is ==> " & connection_string & " SQL query is ===> " & SQL ' show the number of rows in the table using a record set msgBox " Quantity of rows in queried DB ( db_get_rows_count )==> " & db_get_rows_count( myrs ) ' show the number of rows in the table using a new SQL statement msgBox " Quantity of rows in queried DB (db_get_rows_count_SQL ) ==> " & db_get_rows_count_SQL( curConnection , "SELECT COUNT(*) FROM ORDERS" ) ' change a value of a field in an existing row rc = db_set_field_value (curConnection, "ORDERS" , "Agents_Name" , "test", "Agents_Name", "AGENT_TESTER") ' examples of how to retrieve values from the table msgBox "val row 0 col 0: " & db_get_field_value( myrs , 0 , 0 ) msgBox "val row 0 col 1: " & db_get_field_value( myrs , 0 , 1 ) msgBox "val row 1 col Name: " & db_get_field_value( myrs , 1 , "Agents_Name" ) msgBox "val SQL row 1 col Name: " & db_get_field_value_SQL( curConnection , "ORDERS" , 1 , "Agents_Name" ) db_disconnect curConnectionEnd If

  Was this answer useful?  Yes

Using ADODB connection we can connect to DBset DBobj=createobject("ADODB.connection")DBobj.open("DSN=raghu")set rc=createobject("ADODB.recordset")set rc=DBobj.execute("Select * from orders")while rc.eof<>truevalue1=rc.fields(0)value2=rc.fields(1)wendif you don't have dsn created then you can use connection stringDBobj.open("drive=sql server;server=system20;database=raghu;uid=admin;pwd=hyderabad007")if you have any doubts feel free to contact me atraghu_13_sa@yahoo.com or 9866379523

  Was this answer useful?  Yes

Dani Vainstein

  • Feb 17th, 2007
 

using MS-ADODB object

  Was this answer useful?  Yes

ravisQTP

  • Feb 20th, 2007
 

Dim objConnection,objRecordset,strSearchCriteria
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adUseClient = 3

Set objConnection = CreateObject("ADODB.Connection")
Set objRecordset = CreateObject("ADODB.Recordset")

objConnection.Open "DSN=<DSN Name>;UID=<Database userID>;PWD=<Database password>;SERVER=<Server that u r going to connect>;"

MsgBox "Connected"

objRecordset.Open "SELECT * FROM USER where username = 'Ravi'" , objConnection, adOpenStatic, adLockOptimistic

If objRecordset.EOF Then
    Wscript.Echo "Record cannot be found."
Else
    Wscript.Echo "Record found."

 Wscript.Echo "User ID is "&objRecordset("USEREID")
 Wscript.Echo "User Name is " &objRecordset("USERENAME")

End If
objRecordset.Close
objConnection.Close

  Was this answer useful?  Yes

leiklc

  • Oct 3rd, 2007
 

It seems you use Winrunner function (db_get_rows_count, db_set_field_value) to get row count and field value. Does QTP have similar function to get row count, field value? Could you please give an example?

Thanks

  Was this answer useful?  Yes

Set FSO=CreateObject("Scripting.FileSystemObject")
Set FSO = Createobject("ADODB.connection")
Set B =Createobject("ADODB.RecordSet")
  FSO.connectionstring="DSN=QT_Flight32;"  "this is connecting to Flight 4a Database   FSO.Open
  FSO.commandTimeOut=7000
Set B =FSO.execute("select * from orders")
msgbox B.getstring

  Was this answer useful?  Yes

Hi,
 

'Sample code for Connecting to SQL SERVER Data Base & Fetch the Data from Data Base                         

using SQL Server Authentication

 

Str_Connect = "Provider=SQLOLEDB;Data Source=Server;Initial Catalog=sureshdb"

 

Set CnnSQL=CreateObject("ADODB.Connection")

CnnSQL.Open Str_Connect, "sa", "abcd"

 

If CnnSQL.State=1 Then

Msgbox "Connected"

else

Msgbox "Not Connected"

End If

 

Set Rs=CreateObject("ADODB.Recordset")

Set Rs=CnnSQL.Execute("Select * from emp")

Do while not Rs.EOF

Msgbox (Rs(0) & space(3) & Rs(1) & space(3) & Rs(2) & space(3) & Rs(3))

Rs.MoveNext

Loop

 

Note:  Line No: 1   as it is u copy and try  (spaces sensitive)

Data Source is  your System Name or Server Name

            Initial Catalog is the DataBase Name (which is existed in SQL Server DataBase)           

            Line No: 3    "sa"    (UserName)

                              “abcd”   (Password)

Suresh

  Was this answer useful?  Yes

ashok

  • Nov 24th, 2016
 

We installed driver in my system instead of DSN (cause we dont have permissions to create DSN and tell me how to create DSN), please send me the code and procedure step by step

  Was this answer useful?  Yes

Hima Sagar

  • Jul 25th, 2017
 

using the below automation object
Set objDB=CreateObject(adodb.connection)

  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