在數(shù)據(jù)庫(kù)應(yīng)用中,一般我們是用企業(yè)管理器來(lái)創(chuàng)建數(shù)據(jù)庫(kù),表,存儲(chǔ)過(guò)程,觸發(fā)器等。然后再導(dǎo)出SQL語(yǔ)句到一個(gè) *.sql 文件里。然后我們?cè)诓樵?xún)分析器執(zhí)行這些語(yǔ)句即可。
當(dāng)然,我們更想在程序中去執(zhí)行這些語(yǔ)句,而不是再去打開(kāi)查詢(xún)分析器。
當(dāng)我們要在程序中批量執(zhí)行SQL時(shí),需要明白AdoConnection,或AdoQuery(假設(shè)我們使用ADO組件)可以執(zhí)行的語(yǔ)句有什么要求。
1、在查詢(xún)分析器里,一條SQL語(yǔ)句,我們可以直接寫(xiě)成
Select * from aTable where ID=123
或者
Select * from aTable where ID=123;
這兩條語(yǔ)句結(jié)果相同。不同的地方只是第二條多了一個(gè)“;”,用來(lái)表示一條語(yǔ)句結(jié)束。
2、在查詢(xún)分析器里,多條SQL語(yǔ)句的寫(xiě)法
insert into aTable(Field1,Field2) values( '值一','值二')
go
這里的 go 表示一個(gè)批處理
?
根據(jù)以上兩點(diǎn),我們可以看出,我們要在程序中批量執(zhí)行SQL語(yǔ)句,需要判斷一個(gè)完整的操作包含幾條語(yǔ)句,這些語(yǔ)句是不是可以單獨(dú)執(zhí)行等。
下面是一個(gè)示例程序(程序中不涉及到Select操作)

{?
???批量執(zhí)行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語(yǔ)句}?
??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), ' 數(shù)據(jù)庫(kù)連接不能為nil. ' );?
??Assert(FSQLList.count? > ? 0 , ' 請(qǐng)先加載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語(yǔ)句出錯(cuò): ' ? + ?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;?
????SqlStr: = SqlStr? + ?Tmp;?
??Until?eof(F);?
??Closefile(F);?
end;?
end.
?
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
