TQuery寫法問題 |
尚未結案
|
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
我想請問一下以下兩種寫法有什麼差別 寫法一:
Query->Close();
Query->SQL->Clear();
Query->SQL->Add("SELECT * FROM TestTable WHERE Field1>100 and Field2=5");
Query->Open(); 寫法二:
Query->Close();
Query->SQL->Clear();
Query->SQL->Add("SELECT * FROM TestTable WHERE Field1>:V1 and Field2=:V2");
Query->ParamByName("V1")->AsInteger=100;
Query->ParamByName("V2")->AsInteger=5;
Query->Prepare();
Query->Open(); 有人說寫法二會比較快,為什麼呢
|
tech_state
版主 發表:44 回覆:638 積分:641 註冊:2003-02-10 發送簡訊給我 |
|
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
|
jcjroc
高階會員 發表:21 回覆:279 積分:115 註冊:2002-09-18 發送簡訊給我 |
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
HI, 就這兩種來看, 確實是一樣快的沒錯, 但是若是這樣子來看就不一樣了. 第一種:
for i = 1 to 10 do begin Query->Close(); Query->SQL->Clear(); Query->SQL->Add("SELECT * FROM TestTable WHERE Field1>100 and Field2=" IntToStr(i)); Query->Open(); end;================================= 第二種: Query->SQL->Clear(); Query->SQL->Add("SELECT * FROM TestTable WHERE Field1>:V1 and Field2=:V2"); Query->Prepare(); for i=1 to 10 do begin Query->Close(); Query->ParamByName("V1")->AsInteger=100; Query->ParamByName("V2")->AsInteger=i; Query->Open(); end;============================ 原因如下(引敍help說明) Description Call Prepare to have the BDE and a remote database server allocate resources for the query and to perform additional optimizations. Preparing a query consumes some database resources, so it is good practice to call UnPrepare once the query is no longer needed. If the query will only be executed once, the application does not need to explicitly call Prepare or UnPrepare. Executing an unprepared query generates these calls automatically. However, if the same query is to be executed repeatedly, it is more efficient to prevent these automatic calls by calling Prepare and UnPrepare explicitly. Note: When you change the text of a query at runtime, the query is automatically closed and unprepared. 發表人 - timhuang 於 2003/10/31 16:54:04 |
davidchang
一般會員 發表:4 回覆:10 積分:2 註冊:2002-08-08 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |