English 中文(简体)
“ 以 ADO 参数化查询声明变量@ my可变性” 错误“ 。
原标题:"Must declare the variable @myvariable" error with ADO parameterized query

执行 Command 对象丢弃错误 :

必须声明变量@ filename

i 声明参数 filename 使用 CreateParameter/Append :

sql :=  INSERT INTO Sqm(Filename, data) VALUES(@filename, @data) ;

command := CoCommand.Create;
command.Set_ActiveConnection(Connection.ConnectionObject);
command.Set_CommandText(sql);
command.Set_CommandType(adCmdText);
command.Parameters.Append(Command.CreateParameter( @filename , adLongVarWChar, adParamInput, -1, Filename));
command.Parameters.Append(Command.CreateParameter( @data , adLongVarWChar, adParamInput, -1, xml);

command.Execute({out}recordsAffected, EmptyParam, adCmdText or adExecuteNoRecords);

我做错什么了?

最佳回答

据我所知, ADO 不支持 SQL 句子( SELECT、 INSERT、 UdDATE) 中命名的参数, 所以您必须使用 < code>? char 来表示参数

sql :=  INSERT INTO Sqm(Filename, data) VALUES(?, ?) ;

然后按照 sql 句中使用的顺序指定参数值。

ADO 2. 6 引入 < a href=" "http://msdn.microsoft.com/ en-us/library/windows/desktop/ms675840%28v=vs.85%29.aspx" rel="noreferr"\ code > NamedParameters 属性,但似乎只有存储程序才有效。

问题回答

试这个

uses ADODB, DB;
...
...
... and then in some event handler (e.g. button click),
var 
  aCommand :TADOCommand;
begin
  aCommand := TADOCommand.create(self);
  aCommand.ConnectionString :=  build the connection string or use TADOConnection and assign to Connection property instead of ConnectionString property ;
  aCommand.commandText :=  INSERT INTO Sqm(Filename, data) VALUES(:filename, :data); ;
  aCommand.parameters.paramByName( filename ).value :=  test ;
  aCommand.parameters.paramByName( data ).value :=  some data ;
  aCommand.execute;
  aCommand.free;
end;

我一直这样使用参数名称 来帮助TADOCommand和TADOQuery 没有问题

使用参数。 添加值如下

  connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Database Password=RainbowTrout;";
  InsertQry = "Insert into Sections(Name, PartNumber, VersionNumber, Channel, Address, Status, IPAddr) "
        + "values(@SectionName, @PartNumber, @VersionNumber, @Channel, @Address, @Status, @IPAddr)";


  NewCfgConnection.ConnectionString = string.Format(connectionString, ConfigFN);
  NewCfgCommand.Connection = NewCfgConnection;
  NewCfgCommand.CommandText = InsertQry;
  NewCfgConnection.Open();

  // Clear parameter values from last record
  NewCfgCommand.Parameters.Clear();

  // Insert record into sections table - set parameters
  NewCfgCommand.Parameters.AddWithValue("@SectionName", sSectionName);
  NewCfgCommand.Parameters.AddWithValue("@PartNumber", sPartNumber);
  NewCfgCommand.Parameters.AddWithValue("@VersionNumber", sVersionNumber);
  NewCfgCommand.Parameters.AddWithValue("@Channel", iChannel);
  NewCfgCommand.Parameters.AddWithValue("@Address", iAddress);
  NewCfgCommand.Parameters.AddWithValue("@Status", iStatus);
  NewCfgCommand.Parameters.AddWithValue("@IPAddr", iIP);

  NewCfgCommand.ExecuteNonQuery();




相关问题
determining the character set to use

my delphi 2009 app has a basic translation system that uses GNUGetText. i had used some win API calls to prepare the fonts. i thought it was working correctly until recently when someone from Malta ...

Help with strange Delphi 5 IDE problems

Ok, I m going nuts here. For the last (almost) four years, I ve been putting up with some extremely bad behavior from my Delphi 5 IDE. Problems include: Seemingly random errors in coride50.bpl ...

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How convert string to integer in Oxygene

In Delphi, there is a function StrToInt() that converts a string to an integer value; there is also IntToStr(), which does the reverse. These functions doesn t appear to be part of Oxygene, and I can ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签