为什么以下代号执行后并关闭窗口,其它窗口在CommandText中使用Insert语名会出错?
procedure TZhuanChangEdit.FormCreate(Sender: TObject);
 var
   bargainCDS: TClientDataSet;
begin
   bargainCDS:=TClientDataSet.Create(nil);
   with bargainCDS do
   begin
      RemoteServer:=DM_Client.CDSRemote.RemoteServer;
      ProviderName:=DM_Client.CDSRemote.ProviderName;
      CommandText:='select bargain from ylc_database.dbo.bargain'
                  +' where ok=0 group by bargain';
      Open;
      while not Eof do
      begin
         cboBargain.Items.Add(FieldByName('bargain').AsString);
         Next;
      end;
      Close;
   end;
   FreeAndNil(bargainCDS);
end;

解决方案 »

  1.   

    补充:ClientDataSet都是以下代码。
    RemoteServer:=DM_Client.CDSRemote.RemoteServer;
    ProviderName:=DM_Client.CDSRemote.ProviderName;
      

  2.   

    如果不执行TZhuanChangEdit.FormCreate中的代码就没有问题。
      

  3.   

    既然连的是同一个PROVIDER,就没必要重复创建CDS
      

  4.   

    如下代码问题。分不够,再加。unit Unit3;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, DBClient, MConnect, StdCtrls, Grids, DBGrids;type
      TForm1 = class(TForm)
        DCOMConnection1: TDCOMConnection;
        ClientDataSet1: TClientDataSet;
        DataSource1: TDataSource;
        Button1: TButton;
        Button2: TButton;
        ComboBox1: TComboBox;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
     var tmp: TClientDataSet;
    begin
       tmp:=TClientDataSet.Create(nil);
       tmp.RemoteServer:=Clientdataset1.RemoteServer;
       tmp.ProviderName:='remotedsp';
       tmp.CommandText:='insert agreement (date,code) values (''2003-4-19'',''t001'')';
       tmp.Execute;  //出现“灾难性故障”错误
       tmp.Free;
    end;procedure TForm1.Button2Click(Sender: TObject);
     var tmp: TClientDataSet;
    begin
       tmp:=TClientDataSet.Create(nil);
       tmp.RemoteServer:=ClientDataSet1.RemoteServer;
       tmp.ProviderName:='remotedsp';
       tmp.CommandText:='select sim_name from sg_sales.dbo.id08 order by sim_name';
       tmp.Open;
       combobox1.Items.Clear;
       while not tmp.Eof do
       begin
          combobox1.Items.Add(tmp['sim_name']);
          tmp.Next;
       end;
       tmp.Close;
       freeandnil(tmp);
    end;end.
      

  5.   

    以上代码先执行Button2再执行Button1