Delphi 执行SQL脚本/执行SQL GO 脚本语句  
官方Delphi 学习QQ群: 682628230(三千人)\n
频道

Delphi 执行SQL脚本/执行SQL GO 脚本语句


Delphi 执行SQL脚本/执行SQL GO 脚本语句

注意:文件的编码格式,最好要统一,ANSI编码或UNICODE编码

方法1:


   

var

  s: string;

  sqltext: string;

  sqlfile: TextFile;

begin

  if OpenDialog1.Execute then

  begin

    AssignFile(sqlfile, OpenDialog1.FileName);

    FileMode := 0;

    Reset(sqlfile);

    try

      ADOConnection1.BeginTrans;

      while not eof(sqlfile) do

      begin

        Readln(sqlfile, s);

        sqltext := s;

        while (not eof(sqlfile)) and (uppercase(trim(s)) <> 'GO') do

        begin

          Readln(sqlfile, s);

          if (uppercase(trim(s)) <> 'GO') then

            sqltext := sqltext + ' ' + s;

        end;

        adoquery1.Close;

        adoquery1.SQL.Clear;

        adoquery1.SQL.Add(sqltext);

        adoquery1.ExecSQL;

      end;

      CloseFile(sqlfile);

      ADOConnection1.CommitTrans;

      application.MessageBox('SQL执行完成!', '提示', MB_OK + MB_ICONINFORMATION);

    except

      raise exception.Create('SQL执行失败!');

      ADOConnection1.RollbackTrans;

    end;

  end;

end;

   

 

方法2:(这里要注意:脚本语句中 go 一定要换行)


   

var

  I: Integer;

  S: string;

begin

 with TStringList.Create do

 try

    LoadFromFile('test.sql');

    S := '';

    for I := 0 to Count - 1 do begin

      if SameText(Trim(Strings[I]), 'GO') then begin  // SameText 不区分大小写  读取到GO 就执行一次代码

         with ADOQuery1 do begin

           Close;SQL.Clear;

           SQL.Text:=sSQL;

           ExecSQL;

         end;

        S := '';

      end else S := S + Strings[I] + #13#10;

    end;

    if S <> '' then

     begin

       with ADOQuery1 do begin

          Close;SQL.Clear;

          SQL.Text:=sSQL;

          ExecSQL;

       end;

     end;

 finally

     Free;

 end;

 end;  


https://www.cnblogs.com/guorongtao/p/13678241.html


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.045664072036743 seconds