Friday, 9 March 2012

To connect a database and get the records from database


Set cnn = CreateObject("ADODB.Connection")
   cnn.ConnectionString = "driver={SQL Server};server=<enterserver>;" & _
      "uid=<enterlogon>;pwd=<enterpassword>;database=<enterdatabase>"
   cnn.Open
   If cnn.State = adStateOpen Then
      cnnState = "open"
   Else
      cnnState = "closed"
   End If
set rs=createobject("adodb.recordset")
mySql = "Select * from Tab_name"
rs.open sql,cnn
' To print all the column names
for i=0 to rs.fields.count-1
print rs.fields(i).name
Next
'to print all the values for each column
for i=0 to rs.fields.count-1
Print rs.Fields(i).value
rs.moveNext
Next
'close the connection
cnn.Close
cnnState = "closed"

No comments:

Post a comment