線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2000
推到 Plurk!
推到 Facebook!

TQuery之ParamByName,當改用ADOQuery時該改為何

尚未結案
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-31 04:32:02 IP:61.59.xxx.xxx 未訂閱
請問TQuery之ParamByName,當改用ADOQuery時該改為何 ex.TQuery Query1.ParamByName('st_no').AsString := L_STNO; -->ADOQuery1.????
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-12-31 05:00:25 IP:61.59.xxx.xxx 未訂閱
原碼為 Query1.SQL.Clear; SQL := 'INSERT INTO PRD  '; SQL := SQL + '( sk_no ,sk_...... )'; Query1.SQL.Text := SQL; Query1.ParamByName('sk_no').AsString := L_STNO; .... Query1為TQuery. 欲改為TQuery為ADOQuery 因ADOQuery並無ParamByName選項,故改成 如改成ADOQuery1.FieldByName('sk_no').AsString := L_STNO; 但會出現找不到st_no欄位之錯誤    
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-12-31 05:11:30 IP:61.59.xxx.xxx 未訂閱
改 ADOCommand1.Parameters.ParamByName('sk_no').Value := L_SKENO; 一樣會出現找不到sk_no欄位之錯誤
l630521
資深會員


發表:15
回覆:349
積分:372
註冊:2003-02-17

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-12-31 08:49:54 IP:210.66.xxx.xxx 未訂閱
1.ADOQuery1.Parameters.ParamByName('st_no').Value := L_STNO 2.ADOQuery1.SQL.Clear; SQL := 'INSERT INTO PRD '; SQL := SQL '( :sk_no ,:sk_...... )'; ADOQuery1.SQL.Add(SQL); ADOQuery1.Parameters.ParamByName('sk_no').Value := L_STNO; Ian
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-12-31 09:10:37 IP:63.84.xxx.xxx 未訂閱
您好﹗    ADOQuery的Parameters屬性程式碼經常使用的有三種寫法﹕    ADOQuery1.Parameters.ParamByName('名稱').Value := *** ;    ADOQuery1.Parameters.ParamValues['名稱'] := *** ;    ADOQuery1.Parameters[0].Value := ***;    參考看看﹗    ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-12-31 13:22:44 IP:61.59.xxx.xxx 未訂閱
請問原本該Query為TQuery時正常,但改為ADOQuery時卻出現以下錯誤 Update執行時會出現找不到'ct_name'欄位 ps.ADO資料來源為SQLOLE    Update時出現找不到'ct_name'欄位知錯誤......................... Procedure TFMTrans.test_update(L_ctno,L_ctname:string); var SQL : String; Begin QueryP.SQL.Clear; SQL := 'Update CUST SET '; SQL := SQL + ' ct_name =:ct_name'; SQL := SQL + 'Where ct_no = '''+L_ctno+''' '; QueryP.SQL.Text := SQL; //QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; QueryP.Parameters.ParamValues['ct_name'] := L_ctname; try QueryP.ExecSQL; except ShowMessage('轉入失敗!'); end; end; Insert執行無誤............................................. Procedure TFMTrans.test_Insert(L_ctno,L_ctname : String); var SQL : String; Begin QueryP.SQL.Clear; SQL := 'INSERT INTO CUST '; SQL := SQL + '( ct_no ,ct_name )'; SQL := SQL + ' Values '; SQL := SQL + '( :ct_no ,:ct_name )'; QueryP.SQL.Text := SQL; QueryP.Parameters.ParamByName('ct_no').Value := L_ctno; QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; try QueryP.ExecSQL; except ShowMessage('轉入失敗!'); end; end;
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-12-31 13:34:12 IP:61.59.xxx.xxx 未訂閱
我再測試了一下 Procedure TFMTrans.test_update(L_ctno,L_ctname:string); var SQL : String; Begin QueryP.SQL.Clear; SQL := 'Update CUST SET '; SQL := SQL + ' ct_name =:ct_name'; SQL := SQL + 'Where ct_no = '''+L_ctno+''' '; QueryP.SQL.Text := SQL; 1.會有找不到'ct_name'欄位之錯誤 //QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; 2.會有找不到'ct_name'欄位之錯誤 //QueryP.Parameters.ParamValues['ct_name'] := L_ctname; 3.可以正確執行,但我覺得這非好方法,再修改程式或欄位時,請問有更好之方式?或上述二段哪裡有問題,因為在Insert一樣用Parameters.ParamByName卻可以執行 QueryP.Parameters[3].Value := L_ctname; try QueryP.ExecSQL; except ShowMessage('轉入失敗!'); end; end; QueryP.Parameters[4].Value := L_ctname;
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-12-31 14:19:11 IP:63.84.xxx.xxx 未訂閱
您好﹗    一般來講﹐在使用Update的SQL語法時﹐都不能把相關Param的語法使用在Set 欄位名= ***中﹐而是使用在Where 欄位名 ***中﹒ 就您的問題而言﹐Insert語句中的相關Param值的部分是寫在Where子句中﹐而Update語句中的相關Param值的部分是寫在Set 欄位名子句中﹐所以才會出現錯誤﹒ 參考看看﹗ ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-12-31 14:36:10 IP:61.59.xxx.xxx 未訂閱
感謝cashxin2002指教 那關於update語法--> update 表格 set 欄位 = 新值 又param不能使用在set 那新值又該如何取出 非得用Parameters[**].Value不可? 在TQuery之ParamByName為何卻是可以? 可否請cashxin2002兄與以指教 感謝您
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-12-31 15:02:30 IP:63.84.xxx.xxx 未訂閱
您好﹗    直接指定欄位新值或用SQL語法進行計算﹐列舉一些常用的範例如下﹕ 1. Update 資料表 Set 欄位1 = '大學' 2. Update 資料表 Set 欄位2 = 111 3. Update 資料表 Set 欄位3 = 欄位3 * 1.5 4. Update 資料表 Set 欄位4 = Mid(欄位4, 1, Len(欄位4)-3)    參考看看﹗    ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#11 引用回覆 回覆 發表時間:2003-12-31 15:28:18 IP:61.59.xxx.xxx 未訂閱
真是感謝cashxin2002兄的指教 小弟之想用該方法實因要寫入之資料有相當多筆 又先前有遇過某些資料用 QueryP.SQL.Add('ct_name = ' L_ctname ' , '); or QueryP.SQL.Add('ct_name = ' AnsiQuotedStr(L_ctname,'''') ' , '); 時會有問題,所以才用SQL := SQL ' ct_name =:ct_name'; 我不知是否有更佳之解決方式 請cashxin2002先進給予指教 thanks
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#12 引用回覆 回覆 發表時間:2003-12-31 16:06:50 IP:63.84.xxx.xxx 未訂閱
你好﹗    能否請您描述一下您需要使用SQL語法做到的工作目的﹐從實際問題上來想辦法可能直觀一些﹒    新年快樂﹗    ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#13 引用回覆 回覆 發表時間:2003-12-31 16:39:58 IP:61.59.xxx.xxx 未訂閱
再次感謝版主的指教,我主要是要將SQL中某一資料表更新到Access之資料表中 1.開啟SQL資料表,並取出所需之欄位值,並全部使用ADOQuery L_ctno := Trim(qypcust.FieldByName('ct_no').AsString);         L_ctname := Trim(qypcust.FieldByName('ct_name').AsString); .....不加敘述 2.開啟Access資料庫 qyBcust.SQL.Clear; qyBcust.SQL.Add('SELECT * FROM Bcust '); qyBcust.SQL.Add('WHERE ct_no = ''' L_ctno ''' '); qyBcust.SQL.Add('Order by ct_no '); qyBcust.CLOSE; qyBcust.OPEN; If qyBcust.Eof = False then Begin test_Update(L_ctno,L_ctname.....不加敘述);更新 End Else Begin test_Insert(L_ctno,L_ctname.....不加敘述);新增 ................................................. Procedure TFMTrans.test_update(L_ctno,L_ctname:string); var SQL : String; Begin QueryP.SQL.Clear; SQL := 'Update CUST SET '; SQL := SQL ' ct_name =:ct_name'................; SQL := SQL 'Where ct_no = ''' L_ctno ''' '; QueryP.SQL.Text := SQL; //QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; //QueryP.Parameters.ParamValues['ct_name'] := L_ctname; //有欄位比較清楚 QueryP.Parameters[3].Value := L_ctname; //這方法比較麻煩 .................. try QueryP.ExecSQL; except ShowMessage('轉入失敗!'); end; end; Insert程序執行無誤............................................. Procedure TFMTrans.test_Insert(L_ctno,L_ctname : String); var SQL : String; Begin QueryP.SQL.Clear; SQL := 'INSERT INTO CUST '; SQL := SQL '( ct_no ,ct_name....... )'; SQL := SQL ' Values '; SQL := SQL '( :ct_no ,:ct_name....... )'; QueryP.SQL.Text := SQL; QueryP.Parameters.ParamByName('ct_no').Value := L_ctno; QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; .......... try QueryP.ExecSQL; except ShowMessage('轉入失敗!'); end; end;
cashxin2002
版主


發表:231
回覆:2555
積分:1937
註冊:2003-03-28

發送簡訊給我
#14 引用回覆 回覆 發表時間:2003-12-31 16:54:03 IP:63.84.xxx.xxx 未訂閱
引言: Procedure TFMTrans.test_update(L_ctno,L_ctname:string); var SQL : String; Begin QueryP.SQL.Clear; SQL := 'Update CUST SET '; SQL := SQL ' ct_name =:ct_name'................; SQL := SQL 'Where ct_no = ''' L_ctno ''' '; QueryP.SQL.Text := SQL; //QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; //QueryP.Parameters.ParamValues['ct_name'] := L_ctname; //有欄位比較清楚 QueryP.Parameters[3].Value := L_ctname; //這方法比較麻煩 .................. try QueryP.ExecSQL; except ShowMessage('轉入失敗!'); end; end;
從以上這段看﹐三種指定Param值的語法都沒有錯誤之處﹒ 剛才自己測試了一下﹐發現Param的部分在Set 欄位名 = 中也是可以使用﹐不好意思﹐之前有些誤導您了﹒ 請檢查以下兩部分﹕ SQL := SQL ' ct_name =:ct_name^'................; 請將上句中紅色部分^以空格代替 因為您的SQL組合起來之后就會變成ct_namewhere﹐ 而不是ct_name where﹒ 參考看看﹗ ===================== 努力,相信會獲得美麗! 忻晟
------
忻晟
solnone
中階會員


發表:2
回覆:97
積分:69
註冊:2003-05-06

發送簡訊給我
#15 引用回覆 回覆 發表時間:2004-01-01 01:56:19 IP:218.168.xxx.xxx 未訂閱
請使用 Parameters.ParseSQL 來自動產生 Parameters QueryP.SQL.Text := SQL; QueryP.Parameters.ParseSQL(QueryP.SQL.Text, true); QueryP.Parameters.ParamByName('ct_name').Value := L_ctname; ADOQuery1.ExecSQL;
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#16 引用回覆 回覆 發表時間:2004-01-02 22:21:11 IP:61.59.xxx.xxx 未訂閱
謝謝指教 但既然需要 Parameters.ParseSQL 來自動產生 Parameters 那為何在Insert項中卻可以不經ParseSQL 產生 Parameters即可?
Paicaso
中階會員


發表:140
回覆:124
積分:52
註冊:2002-09-04

發送簡訊給我
#17 引用回覆 回覆 發表時間:2004-01-06 11:43:23 IP:61.59.xxx.xxx 未訂閱
我想應該不是空格之問題 從錯誤訊息中得知其找不到的欄位是ct_name而不是ct_namewhere 所以我想應該不是該問題
solnone
中階會員


發表:2
回覆:97
積分:69
註冊:2003-05-06

發送簡訊給我
#18 引用回覆 回覆 發表時間:2004-01-06 12:30:58 IP:210.68.xxx.xxx 未訂閱
Parameters.ParseSQL 會將先前的Parameters清空! 然後再重新產生Parameters, 如此可以確保當換掉sql時,Parameters沒有更新的問題。 ParseSQL可以用在所有的sql語法,不管是select、insert或是update。
系統時間:2024-06-29 16:36:13
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!