日期:2011-01-25 23:11:00 来源:本站整理
<b>批量履行SQL语句的示例</b>[MSSQL防范]
本文“<b>批量履行SQL语句的示例</b>[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
在项目中,常常会碰到需求批量履行SQL语句的情形,下面将为您示例阐明批量履行SQL语句的办法,但愿对教您学习SQL语句有所帮忙.
当然,我们更想在程序中去履行这些语句,而不是再去翻开查询解析器.
当我们要在程序中批量履行SQL语句时,需求懂得AdoConnection,或AdoQuery(假定我们利用ADO组件)可以履行的语句有什么要求.
1、在查询解析器里,一条SQL语句,我们可以直接写成
Select * from aTable where ID=123
大概
Select * from aTable where ID=123;
这两条语句后果相同.差别的地方只是第二条多了一个“;”,用来表示一条语句完毕.
2、在查询解析器里,多条SQL语句的写法
insert into aTable(Field1,Field2) values( '值一','值二')
go
这里的 go 表示一个批处理
按照以上两点,我们可以看出,我们要在程序中批量履行SQL语句,需求判断一个完好的操作包含几条语句,这些语句是不是可以单独履行等.
下面是一个示例程序(程序中不触及到Select操作)
<
- unit BatchSQL;
- {
- 批量履行SQL脚本
- E-main: goldli@163.com
- }
- interface
- uses
- SysUtils,ADODB,Classes;
- type
- TOnException = procedure(const E: Exception) of object;
- TOnSQLExecute = procedure(const strSQL: string;const RowsAffected:Integer) of object;
- type
- {完好的SQL语句}
- TSQLString = class
- private
- FBuffer:string;
- FSQL:TStrings;
- FChanged:Boolean;
- function GetSQL:string;
- public
- constructor Create;
- destructor Destroy;override;
- procedure Append(const StrSQL:string);
- property SQL:string read GetSQL;
- end;
- TBatchSQL = class
- private
- FConnection:TADOConnection;
- FSQLList:TList;
- FOnException:TOnException;
- FOnSQLExecute:TOnSQLExecute;
- public
- constructor Create(const AConnection:TADOConnection);
- destructor Destroy;override;
- property Connection:TADOConnection write FConnection;
- procedure LoadFromFile(const FileName:string);
- procedure Execute;
- property OnException:TOnException write FOnException;
- property OnSQLExecute:TOnSQLExecute write FOnSQLExecute;
- end;
- implementation
- { TSQLString }
- procedure TSQLString.Append(const StrSQL: string);
- begin
- FSQL.Append(StrSQL);
- FChanged:=True;
- end;
- constructor TSQLString.Create;
- begin
- FSQL:=TStringList.Create;
- end;
- destructor TSQLString.Destroy;
- begin
- FSQL.Free;
- inherited;
- end;
- function TSQLString.GetSQL: string;
- begin
- if FChanged then
- begin
- FBuffer:=FSQL.Text;
- FChanged:=False;
- end;
- Result:=FBuffer;
- end;
- { TBatchSQL }
- constructor TBatchSQL.Create(const AConnection: TADOConnection);
- begin
- if Assigned(AConnection) then
- FConnection:=AConnection;
- FSQLList:=TList.Create;
- end;
- destructor TBatchSQL.Destroy;
- var
- i:Integer;
- begin
- FConnection:=nil;
- for i:= FSQLList.Count -1 downto 0 do
- TSQLString(FSQLList.Items[i]).Free;
- FSQLList.Free;
- inherited;
- end;
- procedure TBatchSQL.Execute;
- var
- i:Integer;
- Qry:TADOQuery;
- SQLString:TSQLString;
- begin
- Assert(Assigned(FConnection),'数据库衔接不能为nil.');
- Assert(FSQLList.count > 0,'请先加载SQL文件.');
- FConnection.LoginPrompt:=False;
- FConnection.Connected:=True;
- Qry:=TADOQuery.Create(nil);
- with Qry do
- begin
- Connection:=FConnection;
- Prepared:=True;
- for i:=0 to FSQLList.Count -1 do
- begin
- SQLString:=TSQLString(FSQLList.Items[i]);
- SQL.Clear;
- SQL.Add(SQLString.SQL);
- try
- ExecSQL;
- if Assigned(FOnSQLExecute) then
- FOnSQLExecute(SQLString.SQL,RowsAffected);
- except
- on E:Exception do
- if Assigned(FOnException) then
- FOnException(E)
- else
- raise Exception.Create('SQL语句出错:' + sLineBreak + SQLString.SQL);
- end;
- end;
- Free;
- end;
- end;
- procedure TBatchSQL.LoadFromFile(const FileName: string);
- var
- SqlStr,Tmp:string;
- F:TextFile;
- SQLString:TSQLString;
- begin
- Assert(FileExists(FileName),'SQL文件不存在,不能加载.');
- AssignFile(F,FileName);
- Reset(f);
- Repeat
- Readln(F,Tmp);
- if Tmp='GO' then
- begin
- SQLString:=TSQLString.Create;
- SQLString.Append(SqlStr);
- FSQLList.Add(SQLString);
- SqlStr:='';
- Tmp:='';
- end;
- SqlStrSqlStr:=SqlStr + Tmp;
- Until eof(F);
- Closefile(F);
- end;
- end.
以上是“<b>批量履行SQL语句的示例</b>[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论