Thursday, March 31, 2011

Retrive the column data from database in vbscript


Function retrive_Column_Data()
    Dim fieldname1 As String
    Dim cntAs Integer
    Dim fieldsArr()
   
    cnt= 0
    Set rs = CreateObject("adodb.recordset")
  
    sqlquery = "enter here Select query"
    Set rs = CreateObject("adodb.recordset")
    rs.Open sqlquery, con
    rs.movefirst
   
    While Not rs.EOF
       
        cnt= cnt+ 1       
        rs.movenext
       
    Wend
   
    rs.movefirst
    ReDim testdatadetailsarr(cnt- 1)
   
    While Not rs.EOF
       
        For I = 0 To cnt- 1
            fieldname1 = rs("dbcolumnName_1")
            fieldname2=  rs("dbcolumnName_2")
            fieldsArr(I) = fieldname1 & "," & fieldname2
            rs.movenext
           
        Next
       
    Wend
   
    retrive_Column_Data= fieldsArr
    Set rs = Nothing
    Erase fieldsArr

End Function

Function to find row count from database in vbscript

Function rowCount()
   
    Dim cnt As Integer
   
    cnt = 0
   
    sqlquery = "enter here Select query"
    Set rs = CreateObject("adodb.recordset")
    rs.Open sqlquery, con
    If rs.BOF = False And rs.EOF = False Then
   
        rs.movefirst
   
        While Not rs.EOF
       
            cnt = cnt + 1
            rs.movenext
           
        Wend
       
    rowCount = cnt
    End If
    Set rs = Nothing
   
End Function

Adodb connection in vbscript

Function OpenDBConnect()
   
    Set con = CreateObject("adodb.connection")
    con .ConnectionString = "Enter here Connection string"
    con .Open

End Function