Sub SQLTest()
          Dim connection As New ADODB.connection
          Dim recordSet As ADODB.recordSet
          Dim searchStr As String
          Dim connStr As String
          Dim currSheet As Worksheet
              connStr = "Provider=SQLOLEDB;Server=127.0.0.1;Database=BMS;Trusted_Connection=yes;Uid=sa;Pwd="
              connection.Open connStr
              
          Set currSheet = Workbooks(1).Sheets(2)
              
          Dim sqlStr As String
                  sqlStr 
          = "SELECT * FROM M_嬈柋僾儔儞"
                  
          Set recordSet = New ADODB.recordSet
                  recordSet.Open sqlStr, connection
                  
                  
          Dim i As Integer
                  i 
          = 1
                  
          While Not recordSet.EOF
                  currSheet.Cells(i, 
          1).Value = recordSet.Fields(0).Value
                  currSheet.Cells(i, 
          2).Value = recordSet.Fields(1).Value
                  recordSet.MoveNext
                  i 
          = i + 1
                  Wend
          End Sub

          加上Trusted_Connection=yes

          posted on 2010-08-30 15:37 Ying-er 閱讀(501) 評論(0)  編輯  收藏 所屬分類: VBA
          主站蜘蛛池模板: 枝江市| 靖边县| 府谷县| 龙岩市| 遂溪县| 福州市| 万州区| 临颍县| 长白| 车致| 丰县| 始兴县| 南雄市| 夏津县| 南平市| 沅陵县| 商丘市| 新建县| 乌兰察布市| 军事| 类乌齐县| 屯昌县| 盱眙县| 阿巴嘎旗| 松原市| 台北市| 博罗县| 康平县| 马边| 石台县| 贺兰县| 尚志市| 共和县| 城口县| 邓州市| 民勤县| 崇礼县| 鞍山市| 揭东县| 个旧市| 巴里|