Sub SQLServerDBAutomation()
Dim cnct As New ADODB.Connection
Dim recordset As New ADODB.recordset
Dim col As Integer
Cells.Clear
'Open the connection
cnct.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=Facility ServicessSQL;Data Source=.\sqlexpress;Use Procedure for Prepare=1;Auto Translate=True;Packet _ Size=4096;Workstation ID=RAVI-PC;Use Encryption for Data=False;Tag with column collation when possible=False;"
'Create RecordSet
recordset.Open "select * from Employee_Master", cnct, adOpenKeyset
'Write the field names
For col = 0 To recordset.Fields.Count - 1
Range("A1").Offset(0, col).Value = recordset.Fields(col).Name
Next
'Write the recordset
If recordset.RecordCount > 0 Then
Range("a1").Offset(1, 0).CopyFromRecordset recordset, recordset.RecordCount, recordset.Fields.Count
End If
Set recordset = Nothing
Set cnct = Nothing
End Sub
No comments:
Post a Comment