ADO+查询数据库

Sub 查询数据库()

Set rs = CreateObject("ADODB.RECORDSET")
   cnn.Open = "DSN=GSM;uid=ailxl;pwd=ailxl;Database=感知数据库;" 'TABLE=DBO.DATA;"
  Sql = "SELECT  *  FROM [Data] DATA  WHERE YEAR([Datetime Id(GSM_CELL)])=2018 " 
 
 Set rs = cnn.Execute(Sql) '

Sheets("SHEET2").Cells.ClearContents       '清理保存数据的区域
     For i = 0 To rs.Fields.Count - 1
       Worksheets("SHEET2").Cells(1, i + 1) = rs.Fields(i).Name
     Next
Sheets("SHEET2").Range("a2").CopyFromRecordset rs
 
rs.Close
Set rs= Nothing
end SUB()

上一篇:Excel VLOOKUP函数


下一篇:office 小技巧