调用存储过程【Delphi版】
时间:2011-05-18 来源:edisonfeng
A)返回结果为参数
var
Cn:TAdoConnection;
Sp:TAdoStoredProc;
recordCount:string;
begin
Cn := TAdoConnection.Create(nil);
try
Cn.ConnectionString := [数据库连接字符串];
Cn.LoginPrompt := False;
Cn.KeepConnection := True;
Cn.ConnectionTimeout:=2;
try
Cn.Open;
Sp := TAdoStoredProc.Create(nil);
try
Sp.Connection := Cn;
Sp.Close;
Sp.ProcedureName :='getStudentAmount';
Sp.Parameters.Refresh;//参数的初始化要放在refresh之后
Sp.Parameters.ParamByName('@recordCount').Value:=0 ;//存储过程中的每一个参数都要
Sp.ExecProc; //进行初始化,包括“返回参数”
recordCount:= inttostr(Sp.Parameters.ParamByName('@recordCount').Value);
finally
Sp.Free;
end;
except
on E:EoleException do
begin
ShowMessage('数据库连接失败,请检查连接');
end;
end;
finally
Cn.Free;
end;
end;
B)返回结果为数据集
var
Cn:TAdoConnection;
Sp:TAdoStoredProc;
nameFieldString:string;
begin
Cn := TAdoConnection.Create(nil);
try
Cn.ConnectionString := [数据库连接字符串];
Cn.LoginPrompt := False;
Cn.KeepConnection := True;
Cn.ConnectionTimeout:=2;
try
Cn.Open;
Sp := TAdoStoredProc.Create(nil);
try
Sp.Connection := Cn;
Sp.Close;
Sp.ProcedureName :='queryAllStudentInfo';
Sp.Active:=True;//当返回结果是数据集时,一定要激活
Sp.Parameters.Refresh;
Sp.open;//返回的是参数时只能用ExecProc,返回的是数据集时用ExecProc与Open都可以
while not Sp.Eof do
begin
nameFieldString:=Sp.FieldByName('SName').AsString;
nameFieldString:=Trim(nameFieldString);
Sp.Next;
end;
finally
Sp.Free;
end;
except
on E:EoleException do
begin
ShowMessage('数据库连接失败,请检查连接');
end;
end;
finally
Cn.Free;
end;
end;
相关阅读 更多 +
排行榜 更多 +