如何实现(edit1.text) not in (adoquery1.FieldByName('姓名').AsString)? |
答題得分者是:eaglewolf
|
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
我在 edit1.text 中 输入姓名 希望在数据库中查找该姓名是否存在 可否通过not in 这种结构实现?
我的代码是: procedure TForm.Button1Click(Sender: TObject); begin adoquery1.Close; adoquery1.SQL.Clear; adoquery1.SQL.Add('select 姓名 from 姓名表');//ADO连接数据库OK adoquery1.Open; if not (edit1.text in [adoquery1.FieldByName('姓名').AsString]) then //执行到此处时 无法通过 提示 ordinal type required showmessage(...); end; 自己感觉 if not (edit1.text in (adoquery1.FieldByName('姓名').AsString)) then 也有点问题 但不知如何调整 恳请赐教 谢谢大家 |
eaglewolf
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:4 回覆:268 積分:429 註冊:2006-07-06 發送簡訊給我 |
請使用locate函數
if not adoquery1.locate(edit1.text , '姓名' ,[]) then showmessage(......); locate 用法 請自行參考Help
------
先查HELP 再查GOOGLE 最後才發問 沒人有義務替你解答問題 在標題或文章中標明很急 並不會增加網友回答速度 Developing Tool: 1.Delphi 6 2.Visual Studio 2005 3.Visual Studio 2008 DBMS: MS-SQL |
P.D.
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
in 的用法不可以是一組複雜的變值,你去查help ordinal 的定義,開宗明義
Ordinal types include integer, character, Boolean, enumerated, and subrange types 所以使用 locate 是一個解決方案,但特別注意, locate 會導致資料庫大量的載入行為模式,速度上也十分可怕 所以建議你,另外創建一個 ADOQuery, 利用這個Query來執行 in 的功能,如下 ADOQuery2.SQL.Text:= 'select count(*) as nofound from xxxx where not (姓名 in ("' Edit1.Text "))' ADOQuery2.Open; if ADOQuery2.FieldbyName(nofound).AsInteger=0 then showmessge('not found') ADOQuery2.Close; 我想這樣的寫法速度上不僅快,而且耗用的資源很小,試試看 (註:以上語法未經現場實證,可能會有一些錯用,請參考 in 的用法) ===================引 用 baby2321 文 章=================== if not (edit1.text in [adoquery1.FieldByName('姓名').AsString]) then //执行到此处时 无法通过 提示 ordinal type required showmessage(...); end; 自己感觉 if not (edit1.text in (adoquery1.FieldByName('姓名').AsString)) then 也有点问题 但不知如何调整 恳请赐教 谢谢大家 |
eaglewolf
資深會員 ![]() ![]() ![]() ![]() ![]() 發表:4 回覆:268 積分:429 註冊:2006-07-06 發送簡訊給我 |
ms-sql in 的語法, 參考如下:
select count(*) as nofound from 姓名表 where 姓名 not in ( 'aaa', 'bbb' , 'ccc' ) 另外 如果edit1.text 的內容限定 只有一個名字的話 用不到in 直接下不等式 就好了 select count(*) as nofound from 姓名表 where 姓名 <> 'aaa'
------
先查HELP 再查GOOGLE 最後才發問 沒人有義務替你解答問題 在標題或文章中標明很急 並不會增加網友回答速度 Developing Tool: 1.Delphi 6 2.Visual Studio 2005 3.Visual Studio 2008 DBMS: MS-SQL |
baby2321
初階會員 ![]() ![]() 發表:52 回覆:165 積分:48 註冊:2005-06-11 發送簡訊給我 |
谢谢 P.D.版主 和 eaglewolf
最后我是这样实现的: procedure TForm.Button1Click(Sender: TObject); begin adoquery1.Close; adoquery1.SQL.Clear; adoquery1.SQL.Add('select 姓名 from 姓名表'); adoquery1.SQL.Add('where 姓名=''' Edit1.Text ''''); adoquery1.Open; if adoquery1.RecordCount=0 then showmessage(...); end; 再一次感谢两位 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |