Delphi 执行SQL脚本/执行SQL GO 脚本语句
Delphi 执行SQL脚本/执行SQL GO 脚本语句
注意:文件的编码格式,最好要统一,ANSI编码或UNICODE编码
方法1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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 一定要换行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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 ; |
创建时间:2020.09.16 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
分类:
Delphi 与 SQL
标签:
Delphi
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报