excel中利用宏提取sql server中数据

 

Dim i As Integer, sht As Worksheet
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strcn As String, strsql As String
strcn = "provider=sqloledb;server=ERPA;database=Northwind;Uid=sa;pwd=sa;"
strsql = "select *from Region"
cn.Open strcn
rs.Open strsql, cn
i = 2
Set sht = ThisWorkbook.Worksheets("sheet1")
'Do While Not rs.EOF
'sht.Cells(i, 1) = rs("RegionID")
'sht.Cells(i, 2) = rs("RegionDescription")
'rs.movenext
'i = i + 1
'Loop
sht.Range("A1").CopyFromRecordset rs


rs.Close

你可能感兴趣的:(excel中利用宏提取sql server中数据)