一个未见正式文档的ADO技术,欢迎大家讨论

ccat 2002-07-09 12:50:52
我们都知道,在ADO或其他任何数据库控件中,要向数据库发送SQL语句之前,一定要知道它会不会返回结果集。然后用execute或open分别处理。一个偶然的机会,我试验成了以下的代码,可以成功的用一个执行命令来处理所有的SQL语句,在这里,我只写了一个单结果集的例子,但有了这个方法,扩充成多结果集只是举手之劳。只是,我对这段代码的合理性一点把握也没有。无论从Object Pascal语言还是ADO接口的角度,我的知识都不足以解释它。所以,我把它放在这里,请大家一起看一下。如果它有什么潜在的错误,欢迎大家指正,如果它是对的,也欢迎大家和我一起分享。以下是一个ADO查询分析器的一段代码,全部代码和可执行文件在CSDN的软件中心。叫ADOQuery。
//在同一次执行中不加区分地处理有无结果集的SQL。
procedure TFormMain.QueryDataExecute(Sender: TObject);
var
EffRecs : Integer;
ADORecSet : _RecordSet;
begin
try
Screen.Cursor := crHourGlass;
DMADO.ADOCommand.CommandText := REText.Lines.Text;
ADORecSet := DMADO.ADOCommand.Execute(EffRecs, null);
//注意这里的判断
if ADORecSet.State = 0 then
begin
REInfo.Lines.Text := '本次操作影响了 ' + IntToStr(EffRecs) + ' 行。';
DMADO.ADODataSetMain.Close;
Pages.ActivePage := InfoPage;
end
else
begin
if EffRecs = -1 then EffRecs := 0;
REInfo.Lines.Text := '共返回了 ' + IntToStr(ADORecSet.RecordCount)+ ' 行。'
+ '本次操作另外影响了 ' + IntToStr(EffRecs) + ' 行。';
DMADO.ADODataSetMain.Recordset := ADORecSet._xClone;
ADORecSet.Close;
Pages.ActivePage := DataPage;
end;
finally
Screen.Cursor := crDefault;
end;
end;
...全文
97 26 打赏 收藏 转发到动态 举报
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
naughtyboy 2002-07-11
  • 打赏
  • 举报
回复
学习
Kuye2002yx 2002-07-11
  • 打赏
  • 举报
回复
收藏,翻译的事我想试试.
yctwfly 2002-07-11
  • 打赏
  • 举报
回复
试试看!
vigrous_chen 2002-07-10
  • 打赏
  • 举报
回复
看看先,在学习
ccat 2002-07-10
  • 打赏
  • 举报
回复
有没有哪位好人把上面诸高人提供的资料译成中文?相信对初学者会有帮助的。我们可以把它发到文档中心去,或整理成FAQ,如何?
ccat 2002-07-10
  • 打赏
  • 举报
回复
楼上诸兄:
当然不是不谦虚,我还要谢谢大家呢。从这里面,我学到了很多的东西啊。这些知识可能诸位现在觉得没什么,当时我一个人在黑暗中摸索的感觉可实在不好。我谢谢大家,明天结贴,来者有分,提供帮助者高分。
让我们互相交流,共同进步,谢谢大家。
格兰特杨 2002-07-10
  • 打赏
  • 举报
回复
这是ADO对象模型的比较高级的应用罢了,并没用得着接口,
你没有写类,构造和析构又是从何而来?

Recordset的State的文档,这也是在MSDN里找到的...。

adStateClosed 0 Indicates that the object is closed.
adStateOpen 1 Indicates that the object is open.
adStateConnecting 2 Indicates that the object is connecting.
adStateExecuting 4 Indicates that the object is executing a command.
adStateFetching 8 Indicates that the rows of the object are being retrieved.

我发表一点我的意见,应该不会算得不谦虚吧~
luoweicaisd 2002-07-09
  • 打赏
  • 举报
回复
未试过这样写法,试一下再说
Billy_Chen28 2002-07-09
  • 打赏
  • 举报
回复
思考中......
luoweicaisd 2002-07-09
  • 打赏
  • 举报
回复
未试过这样写法,试一下再说
ccat 2002-07-09
  • 打赏
  • 举报
回复
欢迎大家和我联系
lx1978@263.net
QQ:41934583
ccat 2002-07-09
  • 打赏
  • 举报
回复
楼上的兄弟,你的这段我也看到了,不过它写当时并没有给我太大的帮助,因为那时的我连接口如何用都不会。事实上,现在我也不明白,到底我这么使会不会出问题,这样就不用构造和析构?而且这些文档,并没有明确指出,State可以用于这种用途。事实上,我当时写这个程序主要是读到一本书,自称高级技巧,写查询分析器时,先open一回,有异常的话,就在except中再excute一回……
我当时那个有气啊,这算什么技巧啊,简直不是用傻可以形容的。居然也拿出来骗稿费……因为我知道,sql server的查询分析器的确是只传了一回,所以我也想做一个这样的。当时我没有人可以指望,完全是自己从VCL的源码中摸出来了。所以一点底也没有。
不要笑,这方面的资料太少,我也找不到人问,没有人天生会编程啊。不懂当然是不好的,可不懂装懂就更不好了,对吧。
谢谢大家指教。
格兰特杨 2002-07-09
  • 打赏
  • 举报
回复
怎么没有正式文档?无非是通过ADOCommand生成Recordset的原生对象。
_RecordSet是Borland Delphi中RecordSet的原型。
通过ADOCommand.Execute取得返回行数,通过Recordset的State取得结果
集的状态。

这是Borland的帮助。
property Recordset: _Recordset;

Description

Recordset is the interface through which the records of an ADO dataset are accessed. When you open an ADO dataset, the value of Recordset is automatically set to the interface that provides access to the records. This value should not be used until after an OnRecordSetCreate event occurs.

Use Recordset to get direct access to the ADO recordset object the dataset component represents. This direct access reference allows an application to use properties and methods of the underlying recordset object. Accessing the underlying recordset object is especially useful for utilizing properties and methods of the recordset object not surfaced in ADO dataset components.

Ordinarily, an application would seldom need to access the underlying recordset object directly. One situation that would use such access is directing the recordset produced by the execution of a TADOCommand component. In these situations, assign the recordset returned by the TADOCommand.Execute method directly to the Recordset property.

ADODataSet1.Recordset := ADOCommand1.Execute;

这是ADO的文档。
State Property
Indicates for all applicable objects whether the state of the object is open or closed.

Indicates for all applicable objects executing an asynchronous method, whether the current state of the object is connecting, executing, or retrieving.

Return Value
Returns a Long value that can be an ObjectStateEnum value. The default value is adStateClosed.

Remarks
You can use the State property to determine the current state of a given object at any time.

The object's State property can have a combination of values. For example, if a statement is executing, this property will have a combined value of adStateOpen and adStateExecuting.

The State property is read-only.

Execute Method (ADO Command)
Executes the query, SQL statement, or stored procedure specified in the CommandText or CommandStream property.

Syntax
For a Recordset-returning Command:

Set recordset = command.Execute( RecordsAffected, Parameters, Options )
For a non–recordset-returning Command:

command.Execute RecordsAffected, Parameters, Options
Return Value
Returns a Recordset object reference, a stream, or Nothing.

Parameters
RecordsAffected
Optional. A Long variable to which the provider returns the number of records that the operation affected. The RecordsAffected parameter applies only for action queries or stored procedures. RecordsAffected does not return the number of records returned by a result-returning query or stored procedure. To obtain this information, use the RecordCount property. The Execute method will not return the correct information when used with adAsyncExecute, simply because when a command is executed asynchronously, the number of records affected may not yet be known at the time the method returns.
Parameters
Optional. A Variant array of parameter values used in conjunction with the input string or stream specified in CommandText or CommandStream. (Output parameters will not return correct values when passed in this argument.)
Options
Optional. A Long value that indicates how the provider should evaluate the CommandText or the CommandStream property of the Command object. Can a bitmask of one or more CommandTypeEnum or ExecuteOptionEnum values.
Note Use the ExecuteOptionEnum value adExecuteNoRecords to improve performance by minimizing internal processing.
If adExecuteStream was specified, the options adAsyncFetch and adAsynchFetchNonBlocking are ignored.
Do not use the CommandTypeEnum values of adCmdFile or adCmdTableDirect with Execute. These values can only be used as options with the Open and Requery methods of a Recordset.
zyt 2002-07-09
  • 打赏
  • 举报
回复
很正确的用法,没有什么问题!
zyt 2002-07-09
  • 打赏
  • 举报
回复
很正确的用法,没有什么问题
yangxd 2002-07-09
  • 打赏
  • 举报
回复
没有什么啊,你现在用它就是返回了一个标准的ado的_RecordSet对象,其实总体上说,delphi只是包装了一个ado的部分东东给大家用,要想用的深入,就还得用ado自己的对象,方法,事件,很多ado的东东,你可以自己创建,然后使用,不必要拘泥于delphi包装出来的形式,还有异步构型等很多ado的东东,用起来后,ado的效率才得到了最大的优化
Yang_ 2002-07-09
  • 打赏
  • 举报
回复
??
robinhunter 2002-07-09
  • 打赏
  • 举报
回复
是吗?试一下
robinhunter 2002-07-09
  • 打赏
  • 举报
回复
是吗?试一下先.
日总是我哥 2002-07-09
  • 打赏
  • 举报
回复

mark~~
加载更多回复(6)

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧