最近在学使用Delphi编写ASP组件,遇到了一个问题,我使用的是原生ADO。在做数据储存的时候出了问题,提示“操作必须使用一个可更新的查询”的错误。小弟不思其解。贴出源代码让大家帮帮忙,看是哪里出了问题,或者应该换成什么方式来储存进入数据库?unit Unit1;{$WARN SYMBOL_PLATFORM OFF}interfaceuses
  ComObj, ActiveX, AspTlb, Project2_TLB, StdVcl,ADOint,SysUtils;type
  Tguestbook = class(TASPObject, Iguestbook)
  protected
    procedure OnEndPage; safecall;
    procedure OnStartPage(const AScriptingContext: IUnknown); safecall;
    procedure LinkDatabase; safecall;
    procedure SaveArticle; safecall;
  end;implementationuses ComServ;var conn:oleVariant;procedure Tguestbook.OnEndPage;
begin
  inherited OnEndPage;
end;procedure Tguestbook.OnStartPage(const AScriptingContext: IUnknown);
begin
  inherited OnStartPage(AScriptingContext);
end;procedure Tguestbook.LinkDatabase;
var DB_Path:string;
begin
   DB_Path := ExtractFilePath (ParamStr(0))+'database.mdb';
   DB_Path := 'Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=;Data Source='+DB_Path+';Persist Security Info=False';
   conn := CreateOleObject('ADODB.Connection');
   conn.open(DB_Path);
end;procedure Tguestbook.SaveArticle;
var fatherid:integer;
    subject,body:String;
    addposttime:TDatetime;
    sql:String;
    rs:oleVariant;
begin
    rs := CreateOleObject('ADODB.Recordset');
    fatherid := 1;
    subject := request.Form.Item ['subject'];
    body := request.Form.Item ['body'];
    addposttime:=now;    //就是这段出了错,一但执行了conn.execute(sql),就说“操作必须使用一个可更新的查询”    sql := 'Insert into [guestbook] (fatherid,subject,body,addposttime) values ('+IntToStr(fatherid)+','''+subject+''','''+body+''','''+DatetimeToStr(addposttime)+''')';
    conn.execute(sql);    //难道得换成下面这样的方法吗?为什么SQL语句会提示这样的错误呢?    //sql := 'Select top 1 * From [guestbook] order by id desc';
    //rs.open (sql,conn,1,3,1);
    //rs.addnow;
    //rs.fields['fatherid'].value := 1;
    //rs.update;    rs.close;
end;initialization
  TAutoObjectFactory.Create(ComServer, Tguestbook, Class_guestbook,
    ciMultiInstance, tmApartment);
end.

解决方案 »

  1.   

    应该与游标有关系,设置“rs.open (sql,conn,1,3,1);”中的相关参数。系统默认的方式是为了提高效率。
      

  2.   

    谢谢,我想问一下。为什么用sql := 'Insert into [guestbook] (fatherid,subject,body,addposttime) values ('+IntToStr(fatherid)+','''+subject+''','''+body+''','''+DatetimeToStr(addposttime)+''')';
        conn.execute(sql);不能执行呢?而一定要addnow,update这样落后的方法呢?
      

  3.   

    我是使用TADOCommand组件动态创建管理的
    是可以的,记得那个CoInit什么的,就OK
      

  4.   

    我在ASP中的调用如下:<% 
       Dim DelphiASPObj
       Set DelphiASPObj = Server.CreateObject("Project2.guestbook") 
       DelphiASPObj.LinkDatabase()
       DelphiASPObj.SaveArticle()
       Set DelphiASPObj = Nothing
    %>
      

  5.   

    为什么使用DELPHI内置的ADO对象呢?
    另外,要执行更新语句最好使用"ADODB.Command"对象
    "ADODB.RecordSet"主要是用来返回结果集的,它所执行的应该是一条查询语句
      

  6.   

    因为原生的ADO相对的会快很多,执行语句时我就是用了conn.execute(sql);所以程序反出来给我一个错误提示:“操作必须使用一个可更新的查询”