EXCEL表格链接SQLSEVER数据库

Sub 数据库连接()
    Set Cnn = CreateObject("ADODB.Connection")
   Set rs = CreateObject("ADODB.Recordset")
    Cnn.ConnectionString = "Provider=SQLOLEDB;Persist Security Info=True;User ID=sa;Password=123456;Initial Catalog=运营数据库; Data Source =10.10.1.156"
    Cnn.Open
    Sql = "select 行业,SUM(帐存辅助数量)as 数量 from 运营数据库.dbo.行业分析$ group by 行业  order by 数量 desc"
      Set rs = Cnn.Execute(Sql)
      For i = 0 To rs.Fields.Count - 1
      Cells(1, i + 1) = rs.Fields(i).Name
      Next
        Range("a2").CopyFromRecordset rs
End Sub

 

posted on 2018-07-18 10:12  上山打老虎下山采蘑菇  阅读(367)  评论(0编辑  收藏  举报

导航