Silktest: Reading data from Excel sheet

First of all you need to know that Silktest treats Excel sheet also as a database. Therefore the foremost thing you will need to do is to create a DSN. This can be done by going to :
Start -> Control Panel -> Administrative Tools -> Data Sources(ODBC)

Once here click on ADD under User DSN Tab.
Select Microsoft Excel Driver
Follow the links and select the excel sheet you would like to set as Datasource.
Click on finish tab.

Now come to silktest and open a new script. Declare a function to connect to excel. The example is explained below in detail:

[-] public Array of String connectExcel(String backupSpace,int remainMonths)

// the DSN is the name of the DSN you created earlier. DBQ is the path of the excel
// sheet you have. Be careful of the slashes. Keep it forward so windows doesnt get
//confused.

[ ] STRING gsDSNConnect = DSN=backup_vh15;DBQ=C:/Documents and Settings/kdarshan/My Documents/kilsest/WISE/backupScenarios.xls;UID=;PWD=
[ ] HDATABASE hdbc
[ ] HSQL hstmt
[ ] String beginRow =
[ ] String endRow =
[-] if(backupSpace==2 GB && remainMonths==11)
[ ] beginRow = B9
[ ] endRow = B13
// call the inbuilt database connect function with the datasource

[ ] hdbc = DB_Connect(gsDSNConnect)
[ ] string resultStr=
[ ] int cnt = 1
//Over here upgradespacepricing is the name of the excel sheet. normally it can be
// Sheet1 for a new excel sheet. you can change it acording to you.

[ ] hstmt = DB_ExecuteSQL(hdbc,Select * from [UpgradeSpacePricing$+beginRow+:+endRow+])
[-] while(DB_FetchNext(hstmt,resultStr))
[ ] //keycodes[cnt] = resultStr
[ ] cnt = cnt+1
[ ] //print(The array count in excel+Str(cnt))
[ ] ARRAY[cnt-1] of STRING keycodes
[ ] //print(The array size in excel function +Str(ArraySize(keycodes)))
[ ] int kcnt = 1
[ ] hstmt = DB_ExecuteSQL(hdbc,Select * from [UpgradeSpacePricing$+beginRow+:+endRow+])
[-] while(DB_FetchNext(hstmt,resultStr))
[ ] keycodes[kcnt] = resultStr
[ ] kcnt = kcnt+1
[ ] DB_FinishSql(hstmt)
[ ] DB_Disconnect(hdbc)
[ ] return keycodes

Finally close the connection and return.

You also can use the below function to connect to a DB

[-] public String connectDBLicense(String checkStr)
[ ] String licenseId=
[ ] HDATABASE hdbc
[ ] HSQL hstmnt
[ ] // Connect to the ecommerce_vh11_darshan
[ ] hdbc = DB_Connect (dsn=ecommerce_vh11_darshan)
[-] //Retrieve the invoice# if present
[ ] hstmnt = DB_ExecuteSql (hdbc, select license_id from license where keycode=+checkStr+)
[-] while(DB_FetchNext(hstmnt,licenseId))
[ ] //error = true
[ ] DB_FinishSql(hstmnt)
[ ] DB_Disconnect(hdbc)
[ ] return licenseId

Leave a Reply

Your email address will not be published. Required fields are marked *