注册 登录
编程论坛 Delphi论坛

在Delphi写SQL语句,从一个表向另一个表复制数据,出错。怎么办?

BCLT_xj9605 发布于 2020-09-28 14:31, 2359 次点击
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, Grids, DBGrids, ADODB, StdCtrls;

type
  TForm1 = class(TForm)
    ADOTable1: TADOTable;
    ADOTable2: TADOTable;
    ADOTable3: TADOTable;
    ADOQuery1: TADOQuery;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    DBGrid3: TDBGrid;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
connectstr:string;
begin
  connectstr:=GetCurrentDir+'\SCCS.MDB;';
  ADOTable1.ConnectionString:=
    'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+connectstr+'Persist Security Info=False';
  ADOTable2.ConnectionString:=
    'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+connectstr+'Persist Security Info=False';

  //connectstr:=GetCurrentDir+'\DCB.MDB;';
  connectstr:=GetCurrentDir+'\DCB1.MDB;';
  ADOTable3.ConnectionString:=
    'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+connectstr+'Persist Security Info=False';
  ADOQuery1.ConnectionString:= 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+connectstr+'Persist Security Info=False';

  ADOTable1.TableName:='sk';
  ADOTable2.TableName:='cy';
  ADOTable3.TableName:='DC';
  ADOTable1.Active:=true;
  ADOTable2.Active:=true;
  ADOTable3.Active:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
querystr:string;
begin
  {将sk数据导入到DC表中}
  ADOTable1.Active:=false;
  ADOTable2.Active:=false;
  ADOTable3.Active:=false;
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  querystr:='Delete from DC';
  ADOQuery1.SQL.Add(querystr);
  ADOQuery1.ExecSQL;

  //ADOTable1.Active:=true;
  //ADOTable2.Active:=false;
  //ADOTable3.Active:=false;
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  //querystr:='insert into dc (测点顺序号,射孔井段顶深,射孔井段底深,层位,生产层位)';
  //querystr:='insert into dc(CDSXH,SKJD1,SKJD2,CW,SCCW)';
  //querystr:=querystr+'select * from sk';
  querystr:='insert into dc(CDSXH,SKJD1,SKJD2,CW,SCCW) select * from sk';
  ADOQuery1.SQL.Add(querystr);
  ADOQuery1.ExecSQL;

  showmessage('导入完毕。');
end;

end.
2 回复
#2
supermay2021-10-20 09:33
你这个是跨库导入数据呀,你看看access中如何跨库导入吧,这个不是D的问题。
#3
Tnipp2021-10-29 09:52
你是ADOQuery3连的dc库,但是insert语句用的ADOQuery1
1