全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2468
推到 Plurk!
推到 Facebook!

如何在Delphi搜尋下SQL : select ...from...where...like...

 
paulgogo
一般會員


發表:1
回覆:0
積分:0
註冊:2002-08-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-08-09 23:28:35 IP:218.164.xxx.xxx 未訂閱
這是我的程式, compile時出現 imcompatible type or invalid type transfer 請高手幫忙 錯誤出現的地方 connection := datamodule1.ADOConnection1; ---出現address錯誤 sql.Text := 'select DebtID from CrewDebt_Master where DebtID like ''%' str1 '%''' ; open; last; str:=fields[0].Value;--invalid type transfer str3:=copy(str,4,4);//取年月 str2:=copy(str,8,3); //取流水號 下面是完整程式 ******************************************* //自動產生借支單單號 function Tcrewdebtform.getdebtID:string; //自動產生借支單單號 var tempQuery : Tadoquery; str,str1,str2,str3 : string; begin tempQuery := Tadoquery.Create(nil); //如果是今天的話 {if Datetimepicker1.Date = getSQLServerDate then begin with tempQuery do begin connection :=datamodule1.ADOConnection1; sql.Text:='select DebtID from CrewDebt_Master order by DebtID' ; open; // 打開資料庫執行SQL 指令 // last; // 打開資料庫之後到最後一筆 //str:=fields[0].Value; //因為DebtID 在table得第一個欄位 // str3:=copy(str,4,4); //因為DebtID前三碼是英文字母, 從第四碼開始,有七個數字 // str2:=copy(str,8,3); //取後面的流水號 // if length(inttostr(monthof(getSQLServerDate))) = 1 then // legnth 只有計算string 長度, 無法計算integer長度 //如果月份長度等於1 // str1:=copy(inttostr(yearof(getSQLServerDate)),3,2) '0' inttostr(monthof(getSQLServerDate))//所以必須先把int轉成str, 另外 monthof and yearof 是 word 型態,涵蓋在integer // else str1 :=copy(inttostr(yearof(getSQLServerDate)),3,2) inttostr(monthof(getSQLServerDate)); // monthof(date) 選出日期的月份, 要use --DateUtils //str1 是 今天日期轉成string 型態 // if strtoint(str1) > strtoint(str3) then //現今日期轉成數值一定是大於或等於最後一筆的的年月數值 // str:='CWD' str1 '001' // 如果大於的話,表今天日期已是不同月份 // else str := 'CWD' str3 copy(inttostr((strtoint(str2) 1001)),2,3); //等如果相等的話 ,就直接轉成integer 加 1 //CWD str(年月 0212) str2 (流水號) // end; // end with // end // end 'if begin' //else } begin // 如果不是今天 with tempQuery do begin //先把datetimepicker 所選的日期改成 符合編碼所用的年月格式 : 如0205 if length(inttostr(monthof(Datetimepicker1.Date))) = 1 then str1 :=copy(inttostr(yearof(Datetimepicker1.Date)),3,2) '0' inttostr(monthof(Datetimepicker1.Date)) else str1 := copy(inttostr(yearof(Datetimepicker1.Date)),3,2) inttostr(monthof(Datetimepicker1.Date)); //聯結資料庫, 搜尋所需要的條件 connection := datamodule1.ADOConnection1; sql.Text := 'select DebtID from CrewDebt_Master where DebtID like ''%' str1 '%''' ; open; last; str:=fields[0].Value; str3:=copy(str,4,4);//取年月 str2:=copy(str,8,3); //取流水號 if strtoint(str1) > strtoint(str3) then str:='CWD' str1 '001' else str:= 'CWD' str3 copy(inttostr((strtoint(str2) 1001)),2,3); end; // end with end;// end 'else begin' result:=str; // function執行完後須有result,來回傳值 free; end; // end getDebtID function
jthwang
一般會員


發表:1
回覆:21
積分:4
註冊:2002-07-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-08-10 09:31:46 IP:203.73.xxx.xxx 未訂閱
s_d:=FormatDateTime('yyyymmdd',set_date_cds1.fieldbyname ('start_date').AsDateTime); 1.請將type 為日期轉成yyyymmdd格式,當月份為1月份,則自動補0為01. 2.s_d為string型態. 3.從這個角度思考看看.
P.D.
版主


發表:603
回覆:4038
積分:3874
註冊:2006-10-31

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-08-11 11:39:40 IP:210.85.xxx.xxx 未訂閱
引言: connection := datamodule1.ADOConnection1; ---出現address錯誤
你使用的 datamodule1 有 create 嗎? 一般如果在引用 database 出現 address 錯誤, 多半是所宣告的 database不存在, 亦即沒有被建立
引言: sql.Text := 'select DebtID from CrewDebt_Master where DebtID like ''%' str1 '%''' ; open; last;
一般使用 C/S 資料結構, 最好不要用 last, 像我的記錄如果有上千萬筆, 使用 last會造成 server 將資料全部丟上來, 那不塞爆 client, 如果你這個功能要 取得最後一號, 建議你 'select DebtID from CrewDebt_Master where DebtID like ''%' str1 '%'' order by DebtID desc' (降序) open; first;
引言: str:=fields[0].Value;--invalid type transfer
不知道你table 的定義為何, 很有可能 datatime 格式與 string 格式不符 jthwang網友已有提供你讀取的方式, 其他如DatetoStr()也可以啦, 很多方法你自個試試?
系統時間:2024-05-02 17:20:12
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!