调用sql存储过程,返回数据集?求解!!!!!!感谢高手回答!

user 2011-05-17 04:08:56
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=love9;"
''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


...全文
180 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
vbman2003 2011-05-18
  • 打赏
  • 举报
回复
还有直接:
g_Cmd.ActiveConnection = cn ' 连接到数据库

vbman2003 2011-05-18
  • 打赏
  • 举报
回复
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"
--------------------------------------
具我所知,参数是对象,所以这个参数不会作为一个语句来处理....
clear_zero 2011-05-17
  • 打赏
  • 举报
回复

Do While Not g_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
这些rs也要改成g_rs

你断点debug就会明白多一点
user 2011-05-17
  • 打赏
  • 举报
回复
????????????跪求答案!!!!!!!!!!!!!!!!!
user 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 sjjufo 的回复:]
引用 2 楼 clear_zero 的回复:
不要写这
rs.Open g_Cmd.Execute, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中。此行运行时错误,提示:超时已过期。

你的g_Rs 已经拿到返回值了

如果不写那句的话,会有这个错误:
Operation is not allowed when the object is closed.
[/Quote]

然后这一句会报错:Do While Not rs.EOF '当数据指针未移到记录集末尾时,循环下列操作
user 2011-05-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 clear_zero 的回复:]
不要写这
rs.Open g_Cmd.Execute, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中。此行运行时错误,提示:超时已过期。

你的g_Rs 已经拿到返回值了
[/Quote]
如果不写那句的话,会有这个错误:
Operation is not allowed when the object is closed.
clear_zero 2011-05-17
  • 打赏
  • 举报
回复
不要写这
rs.Open g_Cmd.Execute, cn '执行strSQL所含的SQL命令,结果保存在rs记录集对象中。此行运行时错误,提示:超时已过期。

你的g_Rs 已经拿到返回值了
user 2011-05-17
  • 打赏
  • 举报
回复
????????

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧