我写了这样一个函数
function SQLExec(cnn, cmd, rst: OleVariant; const SQL: string; prms: TParameters_): Boolean;
var
i: Longint;
begin
Result := not (VarIsEmpty(cnn) or VarIsEmpty(cmd)) and (cnn.State = adStateOpen);
if Result then
begin
try
cmd.CommandText := SQL;
cmd.CommandType := adCmdText;
cmd.ActiveConnection := cnn; for i := Low(prms) to High(prms) do
case prms[i].Direction of
adParamInput, adParamInputOutput:
cmd.Parameters[prms[i].Name].Value := prms[i].Value;
end;
cmd.Prepared := True;
rst := cmd.Execute; except
Result := False;
end;
end;
end;用来执行带参数的sql语句,update语句执行没有问题,但是在执行select语句时,结果集rst的RecordCount 为 -1,对其进行操作时提示“对象关闭时,不允许操作”,请大侠帮忙看看问题出在哪里,不胜感激!!
function SQLExec(cnn, cmd, rst: OleVariant; const SQL: string; prms: TParameters_): Boolean;
var
i: Longint;
begin
Result := not (VarIsEmpty(cnn) or VarIsEmpty(cmd)) and (cnn.State = adStateOpen);
if Result then
begin
try
cmd.CommandText := SQL;
cmd.CommandType := adCmdText;
cmd.ActiveConnection := cnn; for i := Low(prms) to High(prms) do
case prms[i].Direction of
adParamInput, adParamInputOutput:
cmd.Parameters[prms[i].Name].Value := prms[i].Value;
end;
cmd.Prepared := True;
rst := cmd.Execute; except
Result := False;
end;
end;
end;用来执行带参数的sql语句,update语句执行没有问题,但是在执行select语句时,结果集rst的RecordCount 为 -1,对其进行操作时提示“对象关闭时,不允许操作”,请大侠帮忙看看问题出在哪里,不胜感激!!
解决方案 »
- 请教D7中多线程时的内存管理机制
- 如何在Delphi中编程获取内存的类型是什么(如DDR、DDR2)?
- 网卡信息问题
- 如何获得外部程序按钮信息?在线等,高手进来看看
- 关于NULL值的问题!
- 如何实现图形中添加有数据库定义的点
- C/S模式下,客户端程序连接远程应用服务器时,为何连接不上?
- 用DELPHI如何编写ISAPI过滤器(IIS)
- 我的程序运行时候,开始老是弹出Database Login的对话框,怎么才可以不要它出来???
- VB做的控件在Delphi中不能使用,是VB的弱点还是Delphi的缺陷?欢迎有此经历者来此讨论。
- delphi如何自定义一个函数库,把自己需要的函数添加到里面?
- 一个IPictureDisp的问题
rst := cmd.Execute;
1、sql语句的第一个单词的大写是SELECT ,就用open (用TAdoQuery.Open)
2、其他情况用execute
var
sql, ConStr : string;
prms: TParameters_;
cnn, rst, cmd: OleVariant;
begin
cnn := CreateConnection;
rst := CreateRecordset;
cmd := CreateCommand; ConStr := '略';
try
try
ConnectToDB(cnn,'','',ConStr); sql :='select * from User where Name = :Name';
SetLength(prms,1); prms[0].Name := ':Name';
prms[0].Direction := adParamInput;
prms[0].Value := 'ccc'; SQLExec(cnn, cmd, rst,sql,prms); showmessage(inttostr(rst.RecordCount));
except
//
end;
finally
FreeCommand(cmd);
FreeRecordset(rst);
FreeConnection(cnn);
end;
end;改成这样也不行啊,各位大侠再帮忙看看啊!!!!
function SQLExec(cnn, cmd, rst: OleVariant; const SQL: string; prms: TParameters_): Boolean;
var
i: Longint;
begin
Result := not (VarIsEmpty(cnn) or VarIsEmpty(cmd)) and (cnn.State = adStateOpen);
if Result then
begin
try
cmd.CommandText := SQL;
cmd.CommandType := adCmdText;
cmd.ActiveConnection := cnn; for i := Low(prms) to High(prms) do
case prms[i].Direction of
adParamInput, adParamInputOutput:
cmd.Parameters[prms[i].Name].Value := prms[i].Value;
end; rst.ActiveConnection := cnn;
rst := cmd.Execute;
except
Result := False;
end;
end;
end;
到ADO中也一样,同样是Query