vba中调用sql存储过程,返回数据集?求解!
Private Sub commandbutton1_Click()
Dim i As Integer, sht As Worksheet 'i为整数变量;sht 为excel工作表对象变量,指向某一工作表
Dim cn As New adodb.Connection '定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
Dim rs As New adodb.Recordset '定义记录集对象,保存数据表
Dim strCn As String, strSQL As String '字符串变量
Set sht = ThisWorkbook.Worksheets("sheet1")
strCn = "Provider=sqloledb;Server=ipsundb;Database=ufdata_100_2011" + ";Uid=sa;Pwd=W8c2w8a0;"
''strSQL = " select ccode,ccode_name,cexch_name,sbb,sbb1,smd,smc,sld,slc from GL_P_FSEYEB"
cn.Open strCn '与数据库建立连接,如果成功,返回连接对象cn
Set g_Cmd = New Command
g_Cmd.ActiveConnection = strCn ' 连接到数据库
g_Cmd.CommandType = adCmdStoredProc ' 表示cmd的类型为存储过程
g_Cmd.CommandText = "GL_P_FSEYEB" ' 调用存储过程
g_Cmd.Parameters(1) = "1"
g_Cmd.Parameters(2) = "5"
g_Cmd.Parameters(3) = 0
g_Cmd.Parameters(4) = Null
g_Cmd.Parameters(5) = "我"
g_Cmd.Parameters(6) = 1
g_Cmd.Parameters(7) = 3
g_Cmd.Parameters(8) = 0
g_Cmd.Parameters(9) = Null
g_Cmd.Parameters(10) = Null
g_Cmd.Parameters(11) = Null
g_Cmd.Parameters(12) = Null
g_Cmd.Parameters(13) = "case when cclass ='资产' then 1 else case when cclass ='负债' then 2 else case when cclass ='权益' then 3 else case when cclass ='成本' then 4 else 5 end end end end as lx"
g_Cmd.Parameters(14) = "YEB26753"
g_Cmd.Parameters(15) = Null
Set g_Rs = g_Cmd.Execute ' 执行存储过程 把数据库查询结果返回给数据集
rs.Open g_Cmd.Execute, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中。此行运行时错误,提示:超时已过期。
i = 5
Do While Not rs.EOF '当数据指针未移到记录集末尾时,循环下列操作
sht.Cells(i, 1) = rs(1)
sht.Cells(i, 2) = rs(2)
sht.Cells(i, 3) = rs(3)
sht.Cells(i, 4) = rs(4)
sht.Cells(i, 5) = rs(5)
sht.Cells(i, 6) = rs(6)
sht.Cells(i, 7) = rs(7)
sht.Cells(i, 8) = rs(8)
sht.Cells(i, 9) = rs(9)
rs.MoveNext '移向下一条记录
i = i + 1
Loop
rs.Close
cn.Close '关闭数据库链接,释放资源
MsgBox "查询完毕!"
End Sub
[解决办法]
二楼给删除了?