我近阶段要编写对数据库的数据导出成文件,然后再把导出的文件导入到数据库中,下面是我写的有关的测试例子。导出文件很顺利,没有问题;但在导入的时候出现了问题,就是在导入完一个文件后,接着导入第二个文件、第三个文件时,出现一个异常。并且第一个导入的文件,在数据库中没有导入记录。我不知道这是什么问题?请高手帮忙!!
下面是我的例子代码:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text:='select * from test';
  ADOQuery1.Open;
  ADOQuery1.SaveToFile('c:\1234.txt');
  ADOQuery1.Close;
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text:='select * from test1';
  ADOQuery1.Open;
  ADOQuery1.SaveToFile('c:\12345.txt');
  ADOQuery1.Close;
  ADOQuery1.Close;
  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Text:='select * from test2';
  ADOQuery1.Open;
  ADOQuery1.SaveToFile('c:\123456.txt');
  ADOQuery1.Close;
  showmessage('导出数据成功!');
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  if not  ADOConnection1.Connected then
    ADOConnection1.Open;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  ADOQuery2.Close;
  ADOQuery2.SQL.Clear;
  ADOQuery2.SQL.Text:='select * from test';
  ADOQuery2.Open;
  ADOQuery2.LoadFromFile('c:\1234.txt');
  ADOQuery2.Close;
  ADOQuery2.Close;
  ADOQuery2.SQL.Clear;
  ADOQuery2.SQL.Text:='select * from test1';
  ADOQuery2.Open;
  ADOQuery2.LoadFromFile('c:\12345.txt');
  ADOQuery2.Close;
  ADOQuery2.Close;
  ADOQuery2.SQL.Clear;
  ADOQuery2.SQL.Text:='select * from test2';
  ADOQuery2.Open;
  ADOQuery2.LoadFromFile('c:\123456.txt');
  ADOQuery2.Close;
  showmessage('导入数据成功!');
end;
以上代码中数据库为SQL Server数据库,test、test1、test2三个表结构完全相同,你们可以自己试一下。快点帮我解决一下吧!!

解决方案 »

  1.   

    procedure TForm1.Button2Click(Sender: TObject);
    begin
      ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.LoadFromFile('c:\1234.txt');
      ADOQuery2.Open;
      ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.LoadFromFile('c:\12345.txt');
      ADOQuery2.Open;
      ADOQuery2.Close;
      ADOQuery2.SQL.Clear;
      ADOQuery2.LoadFromFile('c:\123456.txt');
      ADOQuery2.Open;
      ADOQuery2.Close;
      showmessage('导入数据成功!');
    end;
    测试通过
      

  2.   

    转贴一个例子
    (QRY为ADOQUERY,ADODB为ADOCONNECTION)
      qry.close;
      qry.sql.Clear;
      qry.sql.add('insert into appupdate values(:rq,:bb,:chd,:nr)');
      qry.Parameters.ParamByName('rq').value:=f_time;
      qry.Parameters.ParamByName('chd').value:=f_size;
      qry.Parameters.ParamByName('bb').value:=cxbb.Value;
      qry.Parameters.ParamByName('nr').LoadFromFile(f_dir.Text,ftBlob);
      adodb.BeginTrans;
      try
         qry.ExecSQL;
         adodb.CommitTrans;
         application.MessageBox('数据保存成功!','提示信息',48);
      except
         on e:exception do
         begin
            adodb.RollbackTrans;
            application.MessageBox(pchar('数据提交失败!'+#13+#13+e.message),'错误信息',16);
         end;
      end;
      adodb.close;
      

  3.   

    来个最笨的办法,
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      adoquery1.active:=False;
      adoquery1.sql.clear;
      adoquery1.LoadFromFile('c:\1234.txt');
      adoquery2.active:=False;
      adoquery2.sql.clear;
      adoquery2.sql.text='select * from text';
      mpost(adoquery1,adoquery2);
      adoquery2.UpdateBatch;
    end;function TDataFrm.mPost(var S, D: TAdoQuery): Boolean;
    var
      i ,j : Integer;
    begin
      S.Active := True ; D.Active := True ;
      for i := 0 to S.RecordCount - 1 do
      begin
        D.Append ;
        for j := 0 to S.Fields.Count - 1 do
        begin
           D.Fields[j].Value := S.Fields[j].Value ;
        end;
        D.Post ;
        S.Next ;
      end;
    end;