一直没有写原创的习惯,每次都是从别的贴子拷贝过来。实在是不好意思。最近想把自己做的一个项目中所解决的问题和大家共享一下。希望对遇见这种情况的朋友有所帮助。
程序中要用到VB调用ORACLE的存储过程,存储过程返回的是多记录集。要是在C#中这种情况很好解决。但是到了VB中。情况就不一样了。在CSDN上也查了。都没有人说出完整的解决的解决方案。经过几天的摸索。终于搞定。下面是存储过程和相应的VB调用代码。
存储过程如下:大家只需关心返回的记录集
存储过程
1CREATE OR REPLACE PACKAGE BODY REPORT AS
2 PROCEDURE p_Report_Yysc_Zyq(strDate in date,v_result1 out t_cursor,v_result2 out t_cursor)
3 is
4 nCount int;
5 d_date date;
6 begin
7
8 open v_result1 for SELECT SSDW,sum(nvl(ZJS,0)) ZJS,sum(nvl(KJS,0)) KJS,sum(nvl(RCY,0)) RCY,sum(nvl(RC_Y,0)) RC_Y,sum(nvl(YLBH,0)) YLBH,sum(nvl(Y_LBH,0)) Y_LBH,sum(nvl(YCYL,0)) YCYL,sum(nvl(NCYL,0)) NCYL,round((1- (sum(nvl(RC_Y,0))/0.823/sum(nvl(RCY,0)))),3)*100 HS FROM REPORT_YYSC_ZYQ where TRUNC(rq)=TRUNC(d_date) and HZLX='1' group by SSDW;
9 open v_result2 for select * from REPORT_YYSC_ZYQ where trunc(RQ)=trunc(strDate) and HZLX='1';
10 end p_Report_Yysc_Zyq;
VB调用的代码如下:用下面的两种方式都可以调用:
1CREATE OR REPLACE PACKAGE BODY REPORT AS
2 PROCEDURE p_Report_Yysc_Zyq(strDate in date,v_result1 out t_cursor,v_result2 out t_cursor)
3 is
4 nCount int;
5 d_date date;
6 begin
7
8 open v_result1 for SELECT SSDW,sum(nvl(ZJS,0)) ZJS,sum(nvl(KJS,0)) KJS,sum(nvl(RCY,0)) RCY,sum(nvl(RC_Y,0)) RC_Y,sum(nvl(YLBH,0)) YLBH,sum(nvl(Y_LBH,0)) Y_LBH,sum(nvl(YCYL,0)) YCYL,sum(nvl(NCYL,0)) NCYL,round((1- (sum(nvl(RC_Y,0))/0.823/sum(nvl(RCY,0)))),3)*100 HS FROM REPORT_YYSC_ZYQ where TRUNC(rq)=TRUNC(d_date) and HZLX='1' group by SSDW;
9 open v_result2 for select * from REPORT_YYSC_ZYQ where trunc(RQ)=trunc(strDate) and HZLX='1';
10 end p_Report_Yysc_Zyq;
VB调用存储过程1
2Public Function ExecProcByODBC()Function ExecProcByODBC(ByVal ProcName As String, ByVal sDate As Date) As Variant
3Dim mycmd As ADODB.Command
4Set mycmd = New ADODB.Command
5Dim rs As New ADODB.Recordset
8mycmd.CommandText = "{call " + ProcName + "(?,{resultset 1, v_result1,v_result2})}"
9
10Set param = mycmd.CreateParameter("sDate", adDate, adParamInput, 16, sDate)
11mycmd.Parameters.Append param
14mycmd.CommandType = adCmdText
15
16rs.CursorType = adOpenStatic
17rs.LockType = adLockReadOnly
18
19Set mycmd.ActiveConnection = MyCnt
20Set rs.Source = mycmd
21rs.Open
22Set ExecProcByODBC = rs
23Set rs = Nothing
24Set mycmd = Nothing
25End Function
2Public Function ExecProcByODBC()Function ExecProcByODBC(ByVal ProcName As String, ByVal sDate As Date) As Variant
3Dim mycmd As ADODB.Command
4Set mycmd = New ADODB.Command
5Dim rs As New ADODB.Recordset
8mycmd.CommandText = "{call " + ProcName + "(?,{resultset 1, v_result1,v_result2})}"
9
10Set param = mycmd.CreateParameter("sDate", adDate, adParamInput, 16, sDate)
11mycmd.Parameters.Append param
14mycmd.CommandType = adCmdText
15
16rs.CursorType = adOpenStatic
17rs.LockType = adLockReadOnly
18
19Set mycmd.ActiveConnection = MyCnt
20Set rs.Source = mycmd
21rs.Open
22Set ExecProcByODBC = rs
23Set rs = Nothing
24Set mycmd = Nothing
25End Function
VB调用存储过程2
2Public Function ExecProc()Function ExecProc(ByVal ProcName As String, ByVal sDate As Date) As Variant
3Dim mycmd As ADODB.Command
4Set mycmd = New ADODB.Command
5Dim rs As New ADODB.Recordset
6Dim rs1 As New ADODB.Recordset
7Set mycmd.ActiveConnection = MyCnt
8mycmd.CommandType = adCmdStoredProc
9mycmd.CommandText = ProcName
10mycmd.Parameters.Append mycmd.CreateParameter("sDate", adDate, adParamInput, 16, sDate)
13Set rs = mycmd.Execute
25Set ExecProc = rs
26Set rs = Nothing
27Set mycmd = Nothing
28End Function
最后还要强调一点的时候,在写连接字符串的时候。务必如下。否则在用VB的NEXTRECORDSET会调用不成功。
2Public Function ExecProc()Function ExecProc(ByVal ProcName As String, ByVal sDate As Date) As Variant
3Dim mycmd As ADODB.Command
4Set mycmd = New ADODB.Command
5Dim rs As New ADODB.Recordset
6Dim rs1 As New ADODB.Recordset
7Set mycmd.ActiveConnection = MyCnt
8mycmd.CommandType = adCmdStoredProc
9mycmd.CommandText = ProcName
10mycmd.Parameters.Append mycmd.CreateParameter("sDate", adDate, adParamInput, 16, sDate)
13Set rs = mycmd.Execute
25Set ExecProc = rs
26Set rs = Nothing
27Set mycmd = Nothing
28End Function
strCnt = " Provider=OraOLEDB.Oracle.1;Password=1;Persist Security Info=True;User ID=1;Data Source=ORA92; PLSQLRSet=1"