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

如何实现 加执行参数的存储过程转移到adocommand1中CommandText Editor

答題得分者是:tonnymarkx
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-11-16 04:37:51 IP:61.183.xxx.xxx 未訂閱
SQLSERVER2000的数据库

[code delphi]
adocommand1.CommandText:='exec 存储过程''' inputstring '''';//inputstring:string; inputstring是通过对话框输入的一个参数
adocommand1.Execute;
[/code]

存储过程的内容是:

[code delphi]
CREATE proc 存储过程 @old int
as
begin
delete from 人口表 where 年龄=@old
end
GO
[/code]

现在我想实现把加执行参数的存储过程 转移到CommandText Editor中去
没有执行参数的的转移好办

我想问的是 如上例 我在CommandText Editor应该怎样写呢?直接写delete from 人口表 where 年龄=@old 是肯定不行的
是不是要在adocommand1的Parameters进行设置?

谢谢指教
tonnymarkx
一般會員


發表:0
回覆:3
積分:10
註冊:2006-10-04

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-11-16 21:56:39 IP:203.68.xxx.xxx 訂閱
  1. CREATE proc 存儲過程 @old int
  2. as
  3. begin
  4. delete from 人口表 where 年齡=@old
  5. end
  6. GO
轉換成 ADOCommand
--> ADOCommand1.Parameters.CreateParameter( ' @old int ', ftInteger ,pdInput,8, 你的值);

注意一下 type 應該就可以跑了


Delphi syntax:

function CreateParameter(const Name: WideString; DataType: TDataType; Direction: TParameterDirection; Size: Integer
; Value: OleVariant): TParameter;

C syntax:

TParameter* __fastcall CreateParameter(const WideString Name, Db::TFieldType DataType, TParameterDirection Direction, int Size, const OleVariant &Value);

Description

Call CreateParameter to create a single TParameter object and add it to the TParameters collection. Property values for the newly added TParameter object are supplied via CreateParameter parameters.

Name is a WideString value containing the name of the newly created TParameter object.

DataType is a TDataType value indicating the data type of the parameter the new TParameter object represents.

Direction is a TParameterDirection value indicating the parameter type. A parameter represented by a TParameter may be any one of: input, output, input-output, return value. Designate a parameter as unknown if the parameter type is not know at the time the TParameter is created.

Size indicates the maximum size of the parameter value.

Value is an OleVariant containing the parameter value. A value need not be passed for Value when the TParameter is created. It can be assigned later. If no value is assigned at this time, pass a nil (Delphi) or NULL (C ) for Value.

ADOStoredProc1.CreateParameter('Parm1', ftString, pdInput, 10, nil);

不知道這樣是不是你要的

baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-11-17 01:56:18 IP:61.183.xxx.xxx 未訂閱
真诚感谢tonnymarkx的回复

我原来的程序代码是:
adocommand1.CommandText:='exec 存储过程''' inputstring '''';
adocommand1.Execute;


我的adocommand1.commandtype 设置为cmdText
现在我把存储过程中代码 如:delete from 人口表 where 年龄=@old 写到了 adocommand1CommandText Editor中
然后代码调整为:
adocommand1.Parameters.CreateParameter('@old int',ftInteger,pdInput,8,''' inputstring ''');
adocommand1.Execute;

执行后 提示 应用程序在当前操作中使用了错误类型的值

我也在 Editing adocommand1.Parameters中做了尝试 不过好象只能设定固定的值 而不能设定象''' inputstring '''这样变动的情况
編輯記錄
baby2321 重新編輯於 2007-11-17 01:57:41, 註解 無‧
baby2321
初階會員


發表:52
回覆:165
積分:48
註冊:2005-06-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-11-19 00:27:05 IP:61.183.xxx.xxx 未訂閱
我把inputstring的数据类型调整了一下

var
index:integer;
inputstring:string;
begin
...
index:=strtoint(inputstring);//inputstring的值是通过对话框的输入得到的
...
end;
然后 在CommandText Editor中 加入: delete from 人口表 where 年龄= index
执行后 提示 index附近有语法错误 呵呵 这里的 index是不是要加些处理符号啊
系統時間:2024-05-05 15:48:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!