ãdelphi+oracleæ¥è¡¨è§£å³æ¹æ¡(ä¸)ãdelphiä¸è°ç¨oracleçåå¨è¿ç¨(å带è¿å游æ ï¼ä¸è¿åå¼ä¸¤ç§)
å
³é®å: delphi ,oracleåå¨è¿ç¨ï¼æ¸¸æ ,è¿åæ°æ®é,æ¥è¡¨
注ï¼delphi 6+ oracle 8.1.6
ä¸.å建å
ä¸å
ä½
1.éï¼å»ºè¡¨aaclass为ä¸é¢ä½æµè¯ç¨
create table aaclass(CID VARCHAR2(50), CNAME VARCHAR2(50), pnumber NUMBER(10,0) );
INSERT INTO aaclass values('c1', 'cn1', 10 ) ;
INSERT INTO aaclass values('c2', 'cn2', 40 ) ;
INSERT INTO aaclass values('c1', 'cn3', 30 ) ;
commit;
2.建å
:
CREATE OR REPLACE PACKAGE PKG_JCCTEST1
AS
type rc_class is ref cursor;
--æ±p1,p2çåä¸å·®ï¼è¿åçå¤ä¸ªå¼éè¿æ¸¸æ è¿å
procedure GetSubAndSum2(p1 number,p2 number ,
ResultCursor out rc_class);
--æ¥è¯¢æ»¡è¶³æ¡ä»¶çæ°æ®éï¼è¿åæ°æ®ééè¿æ¸¸æ è¿å
procedure GetClass2(a in number,ResultCursor out rc_class ) ;
--å¾è¡¨ä¸æä¸æ¡è®°å½ï¼ä¸è¿åç»æéæ¶ï¼æ¬äººç¨AdoQueryè°ç¨(adodataset好象è¦æ±å¿
é¡»è¿åç»æé)
procedure InsertClass( p_cid varchar2 ,p_cname varchar2 ,
p_pnumber number) ;
end PKG_JCCTEST1;
3.建å
ä½
CREATE OR REPLACE PACKAGE BODY PKG_JCCTEST1
AS
procedure GetSubAndSum2(p1 number,p2 number ,
ResultCursor out rc_class)
IS
BEGIN
open ResultCursor for
select p1-p2 as "sum", p1+p2 as "sub" from dual;
END ;
procedure GetClass2(a in number,ResultCursor out rc_class )
is
begin
open ResultCursor for
select aaclass.* from aaclass where pnumber >a;
end ;
procedure InsertClass( p_cid varchar2 ,p_cname varchar2 ,
p_pnumber number)
is
begin
insert into aaclass values(p_cid,p_cname,p_pnumber) ;
-- commit;
end ;
äº.å¨delphiä¸å©ç¨AdoDataSetè°ç¨ä¸è¿°ç¬¬ä¸ä¸ªåå¨è¿ç¨
1.å©ç¨AdoConnection1è¿æ¥æ°æ®åº(驱å¨ä¸º oracle Provider for OLE DB)ï¼
**并å¨è¿æ¥å符串ä¸å å
¥è¿ä¸è: PLSQLRSet=1; å¦ä¸æ示ï¼
Provider=OraOLEDB.Oracle.1;Password=KXD;Persist Security Info=True;User ID=KXD;Data Source=TEST3;PLSQLRSet=1
2.å¨çªä½ä¸å AdoDataSet1 ææè¿æ¥ä¸ºä¸è¿°AdoConnection1,ä¸é¢å¯ä»¥æ¾ä¸ä¸ªæé®ï¼åå»æé®å°±è½è°ç¨ç¬¬ä¸æ¥ä¸å建çå
è¿ç¨ï¼å¹¶è¿åæ°æ®éã代ç å¦ä¸æ示ï¼
procedure TForm1.Button1Click(Sender: TObject);
var
AResult , BResult : integer;
begin
ADODataSet1.Close ;
ADODataSet1.CommandType := cmdText ;
ADODataSet1.Parameters.Clear ;
//***å©ç¨callæ¹æ³è°ç¨oracleè¿ç¨æ¶ï¼åæ°å¿
é¡»ç±?æ¥ä¼ ï¼ å³ä½¿ä½ è¦ä¼ çåæ°ä¸ºå¸¸ç
//è¾åºæ¸¸æ çåæ°ä¸éè¦æå®!!!!!!ï¼æ¬æ¥æ¤å½æ°å¸¦ä¸ä¸ªåæ°ï¼æ们è¿éåªéè¦ä¼ 两个åæ°.
ADODataSet1.CommandText := '{call PKG_JCCTEST1.GetSubAndSum2(?,?)}' ;
//***C 顺åºæå
³ï¼createparamå¿
é¡»æ¾å¨commandtextèµå¼è¯å¥ä¹å.
// å建第ä¸ä¸ªåæ°ï¼å¯¹åºcallä¸ç第ä¸ä¸ª?,ftinteger为类å,10为é¿åº¦ï¼45ä¸ºä¼ å
¥çå®åå¼
ADODataSet1.Parameters.CreateParameter('p1',ftinteger,pdinput,10,45);
//å建第äºä¸ªåæ°ï¼æ ¹æ®createparameterçé¡ºåº èªå¨ä¸callä¸ç第äºä¸ªåæ°å¯¹åº
ADODataSet1.Parameters.CreateParameter('p2',ftinteger,pdinput,10,4);
//ä¸é¢è°ç¨ADODataSet1 çopenæ¹æ³ï¼è¿åæ°æ®é(对åºå
è¿ç¨ç游æ )
ADODataSet1.Open ;
//æ ¹æ®åå¨è¿ç¨ï¼æ°æ®éåªæä¸æ¡è®°å½ï¼æ以ä¸éè¦ç¨while do æ¥éåæ°æ®éï¼ç´æ¥åæ°æ®äº
//æ¤å¤çå段åæ ¹æ®å
è¿ç¨ä¸çè¿å游æ 对åºçå段åæ¥å
//å®ä¹çåå¨è¿ç¨è¿å游æ å¦ï¼ open ResultCursor for
// select p1-p2 as "sum", p1+p2 as "sub" from dual;
//æ对åºçå段å¼ååºæ¥å³å¯
AResult := ADODataSet1.Fields.FieldByName('sub').Value ;
BResult := ADODataSet1.Fields.FieldByName('sum').Value ;
//æ¾ç¤ºç»æ
showmessage(inttostr(AResult)) ;
showmessage(inttostr(BResult)) ;
end;
ä¸.å¨delphiä¸å©ç¨AdoDataSetè°ç¨ä¸è¿°ç¬¬äºä¸ªåå¨è¿ç¨
è¿æ¯å©ç¨ä¸è¿°çAdoDataSet1æ¥è°ç¨ç¬¬äºä¸ªåå¨è¿ç¨ï¼æ éä»»ä½æ¹å¨,å 第äºä¸ªæé®ï¼åå»æ¶ä»£ç å¦ä¸ï¼
procedure TForm1.Button2Click(Sender: TObject);
begin
ADODataSet1.Close ;
ADODataSet1.CommandType := cmdText ;
ADODataSet1.Parameters.Clear ;
//***å©ç¨callæ¹æ³è°ç¨oracleè¿ç¨æ¶ï¼åæ°å¿
é¡»ç±?æ¥ä¼ ï¼ å³ä½¿ä½ è¦ä¼ çåæ°ä¸ºå¸¸ç
//è¾åºæ¸¸æ çåæ°ä¸éè¦æå®!!!!!!ï¼æ¬æ¥æ¤å½æ°å¸¦ä¸¤ä¸ªåæ°ï¼æ们è¿éåªéè¦ä¼ ä¸ä¸ªåæ°.
ADODataSet1.CommandText := '{call PKG_JCCTEST1.GetClass2(?)}' ;
//***C 顺åºæå
³ï¼createparamå¿
é¡»æ¾å¨commandtextèµå¼è¯å¥ä¹å.
// å建第ä¸ä¸ªåæ°ï¼å¯¹åºcallä¸ç第ä¸ä¸ª?,ftinteger为类å,10为é¿åº¦ï¼20ä¸ºä¼ å
¥çå®åå¼
ADODataSet1.Parameters.CreateParameter('p1',ftinteger,pdinput,10,20);
//ä¸é¢è°ç¨ADODataSet1 çopenæ¹æ³ï¼è¿åæ°æ®é(对åºå
è¿ç¨ç游æ )
ADODataSet1.Open ;
while not ADODataSet1.Eof do
begin
showmessage('CID : '+string(ADODataSet1.FieldByName('CID').Value) +
'--CNAME :' + string(ADODataSet1.FieldByName('CNAME').Value) +
'--PNUMBER :' + string(ADODataSet1.FieldByName('PNUMBER').Value)
) ;
ADODataSet1.Next ;
end ;
end;
å å©ç¨adoqueryè°ç¨ç¬¬ä¸ä¸ªè¿ç¨ï¼ä¸è¿åæ°æ®éç
procedure TForm1.Button3Click(Sender: TObject);
begin
AdoQuery1.Close ;
AdoQuery1.Parameters.Clear ;
AdoQuery1.SQL.Clear ;
AdoQuery1.SQL.Add('{call PKG_JCCTEST1.GetSubAndSum2(?,?)}') ;
AdoQuery1.Parameters.CreateParameter('P1',ftstring,pdinput, 50,'c11') ;
AdoQuery1.Parameters.CreateParameter('P2',ftstring,pdinput, 50,'cn11') ;
AdoQuery1.Parameters.CreateParameter('P3',ftinteger,pdinput, 50,25) ;
AdoQuery1.ExecSQL ;
end;
äº å©ç¨adoqueryè°ç¨ç¬¬ä¸ä¸ªè¿ç¨ï¼è¿åæ°æ®éç.
procedure TForm1.Button4Click(Sender: TObject);
begin
AdoQuery1.Close ;
AdoQuery1.Parameters.Clear ;
AdoQuery1.SQL.Clear ;
AdoQuery1.SQL.Add('{call PKG_JCCTEST1.GetSubAndSum2(?,?)}') ;
AdoQuery1.Parameters.CreateParameter('P1',ftinteger,pdinput, 50,25) ;
AdoQuery1.Parameters.CreateParameter('P2',ftinteger,pdinput, 50,22) ;
AdoQuery1.Open ;
Showmessage(string( AdoQuery1.FieldByName('sub').Value)+'-'+
string( AdoQuery1.FieldByName('sum').Value));
end;
å
.å
³äºä¸å±ä½ç³»çæ¤ç±»é®é¢
两å±ç解å³äºï¼ä¸å±ç±»ä¼¼.
ä¸é´å±ç¨tadodataset ætadoquery (+tdatasetprovider),ä¸é´å±çadoconnectionçè¿æ¥å符串å ä¸plsqlRset=1;
客æ·ç«¯ç¨clientdataset ,大åå°å¼ï¼ä¸¾ä¾å¦ä¸:
begin
//è°ç¨ç¸åºçè¿ç¨
ClientDataSet1.Close ;
ClientDataSet1.Params.Clear ;
ClientDataSet1.CommandText := '{call PackageName.ProcedureName(?,?)}' ;
ClientDataSet1.Params.CreateParam(ftInteger , 'ParamName1', ptInput) ;
ClientDataSet1.Open ;
end ;
æ¬äººæ°´å¹³æéï¼å¦æä¸å½ä¸é误ä¹å¤è¯·ææ£!
--------------------------------------------------
é®ï¼ä½ 好ï¼
çå°ä½ çå
³äºDELPHIè°ç¨ORACLEåå¨è¿ç¨è¿åç»æéçæç« ï¼å¾å触åã
æå·²ç»å®ç°äºå¨DELPHIéè°ç¨ORACLEåå¨è¿ç¨ï¼éè¿ä¸ä¸ªæ¸¸æ åæ°ï¼è¿åä¸ä¸ªç»æéçæ
åµãç°å¨æ个é®é¢ï¼å¨åå¨è¿ç¨éï¼å¯ä»¥å®ç°å¤ä¸ªå¼±æ¸¸æ åæ°è¾åºå¤ä¸ªç»æéï¼ä½æ¯ï¼å¨DELPHIéï¼æä¸ç¥éï¼æä¹åå¾è¿ç¬¬äºä¸ªæå
¶åçéè¿æ¸¸æ è¿åçç»æéãéè¿ADOåªè½åå¾ç¬¬ä¸ä¸ªç»æéã
å¦æä½ ç¥é解å³æ¹æ¡ï¼è¯·EMAILèç³»æã
ä¸èææ¿ã
çï¼
ç¨nextrecordsetæ¹æ³å°±å¯ä»¥äºï¼ä»£ç å¦ä¸ï¼
var
vTemp1:integer;
begin
....
ADODataSet1.Open;
ADODataSet2.Recordset:=ADODataSet1.NextRecordset(vTemp1);
showmessage(ADODataSet2.FieldValues['CID']);
end;
é®ï¼æä¹è¿æ¥å±åç½å
çoracleçæ°æ®å¢ï¼
è¿æ¥å符串åºè¯¥æä¹åå¢ï¼
Provider=OraOLEDB.Oracle.1;Password=KXD;Persist Security Info=True;User ID=KXD;Data Source=TEST3;PLSQLRSet=1
æç¨çadoqueryæ§ä»¶ã
çï¼ä»¥ä¸ä¸ä¸ªåæ°æ ¹æ®ä½ çæ
åµä¿®æ¹å°±å¯ä»¥äº
Data Source=TNSå称
User ID=ç¨æ·å
Password=å¯ç
æ¬ææ¥èªCSDNå客
温馨提示:内容为网友见解,仅供参考