VBA执行存储过程

Sub test()
Dim strcon As String
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
strcon = "Driver={SQL Server};Server=**********;UID=**;PWD=*********;DataBase=***"
cn.Open strcon

strsql = "exec 存储过程名 '" & Sheet2.Cells(2, 2).Value & " ','" & Sheet2.Cells(3, 2).Value & " '"   '参数可选
rs.Open strsql, cn, adOpenDynamic, adLockBatchOptimistic
 r = 6
 Sheet2.Range(Cells(6, 1), Cells(50000, 20)).ClearContents
    For i = 0 To rs.Fields.Count - 1
       Sheet2.Cells(5, i + 1).Value = rs.Fields(i).Name
    Next i
    
While Not rs.EOF
    For i = 0 To rs.Fields.Count - 1
       Sheet2.Cells(r, i + 1).Rows.Value = rs.Fields(i).Value
    Next i
    r = r + 1
    rs.MoveNext       
Wend
End Sub

posted @ 2012-03-29 20:13  perock  阅读(6712)  评论(0编辑  收藏  举报