首先看我的Oracle數據包
create or replace package Pkg_GetQuery is
-- Author : ZM
-- Created : 2007-12-12 19:38:22
-- Purpose :
-- Public type declarations
type Cur_Ref is ref cursor;
Procedure Proc_GetQueryStudinfo(strkey varchar2,cur_Result out Cur_Ref);
end Pkg_GetQuery;
其次包的主體實現:
create or replace package body Pkg_GetQuery is
-- Private type declarations
procedure Proc_GetQueryStudinfo(strkey varchar2,cur_Result out cur_Ref)
is
strsql varchar2(1000);
begin
strsql:=''select * from StudInfo Where StudName Like ''''''||strkey||''%'''''';
Open Cur_Result For StrSql;/**//*比較重要的一句*/
end;
end Pkg_GetQuery;
再次,用VB調用存儲過程開始:
conn.ConnectionString = strconn
conn.CursorLocation = adUseClIEnt
conn.Open
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "STUD20050704006.Pkg_GetQuery.Proc_GetQueryStudInfo"
Set para1 = cmd.CreateParameter("StrKey", adVarChar, adParamInput, 1000, "張")
para1.Value = Text1.Text
cmd.Parameters.Append para1
'' Set para2 = cmd.CreateParameter("Cur_Result", adUserDefined, adParamOutput)
'' cmd.Parameters.Append para2
Set rs = cmd.Execute()
大家看到了,我第二個參數的類型,我寫的是adUserDefined,後測試不行.它並不像C#那樣,可以
寫cmd.Parameters.Add("rst",OracleType.Cursor); 後查了很多資料,才解決.
完整代碼:
Dim strconn As String
Dim conn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim para As New ADODB.Parameter
Dim rs As New ADODB.Recordset
Dim para1 As New ADODB.Parameter
Dim para2 As New ADODB.Parameter
Private Sub Command1_Click()
strconn = "Provider=MSDAORA.1;PassWord=*****;User ID=Linux_wolfelite;Data Source=0.0.0.0/jkxoracl;Persist Security Info=True"
conn.ConnectionString = strconn
conn.CursorLocation = adUseClIEnt
conn.Open
Set cmd.ActiveConnection = conn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "STUD20050704006.Pkg_GetQuery.Proc_GetQueryStudInfo"
'' Set para = cmd.CreateParameter("score", adNumeric, adParamInput)
'' para.Precision = 4
'' para.NumericScale = 1
Set para1 = cmd.CreateParameter("StrKey", adVarChar, adParamInput, 1000, "張")
para1.Value = Text1.Text
cmd.Parameters.Append para1
'' Set para2 = cmd.CreateParameter("Cur_Result", adUserDefined, adParamOutput)
'' cmd.Parameters.Append para2
Set rs = cmd.Execute()
If rs.RecordCount > 0 Then
MsgBox rs.RecordCount
End If
Set DataGrid1.DataSource = rs
DataGrid1.Refresh
If Not rs.BOF Then
rs.MoveLast
Text2.Text = rs(0).Value
End If
'' Set rst = cmd.Execute(Null, Null, adCmdStoredProc)
'' Set rs = Nothing
'' Set conn = Nothing
End Sub
Private Sub Command2_Click()
rs.MovePrevious
Text2.Text = rs(0).Value
End Sub
說明:
1,text1是輸入參數值,
2,text2沒有多大意義,我是為了測試是否可以movelast
3,感謝這個帖子,http://www.80diy.com/home/20060323/15/4634977.Html,
4,給出C#調用代碼,用C#代碼調用是比較簡單的.
private DataTable ExecuteProcToDataSet(string Proc_Name,Hashtable HT,string
Ref_Cur_Name) ...{
•DataTable MyTable = new DataTable();
• try ...{
• OraConn = new OracleConnection(StrConnOracle);
• OraComm = new OracleCommand();
• OraComm.Connection = OraConn;
•
OraComm.CommandType = CommandType.StoredProcedure;
• OraComm.CommandText = Proc_Name;
• IDictionaryEnumerator Enumerator= HT.GetEnumerator();
•
while (Enumerator.MoveNext()) ...{
•
object
Value = Enumerator.Value;
• OracleParameter OraParam=new
OracleParameter(Enumerator.Key.ToString(), Value);
• OraComm.Parameters.Add(OraParam);
•
OracleParameter OraParamCur = new
OracleParameter(Ref_Cur_Name, OracleType.Cursor);
• OraParamCur.Direction = ParameterDirection.Output;
• OraComm.Parameters.Add(OraParamCur);
• DataSet MyDataSet = new DataSet();
• OraAdapter = new OracleDataAdapter(OraComm);
• OraAdapter.Fill(MyDataSet, "MyTable");
• MyTable = MyDataSet.Tables["MyTable"];
• }
• catch (Exception ex)
• ...{
• MessageBox.Show(ex.Message);
• }
• return MyTable;
• }