Using ParseSQL Command for ADO Parameters Cause Invalid Parameter DataType

懵懂的女人 提交于 2019-12-12 08:59:21

问题


I have some SQL Command that contains a parameter such:(Note that myID has "int" type in SQL)

vSqlString :='Select * From myTable Where myID= :paramID';

and Use ParseSQL Command to execute this command:

myADOQuery.Parameters.ParseSQL(vSqlString , True);

Now myADOQuery.Parameters.ParamByName('paramID').DataType is smallint Type and it can't accept negative integer values.

I can exactly show to compiler that my Parameter[0].DataType is ftIneteger and it works properly, but what is a good solution for this problem?


回答1:


My question is asked by Mr. imanShadabi Here: Using TAdoQuery.ParseSql and has resolved by user1008646




回答2:


Hope this will help.

If you want to create in runtime parameters, you can use something like this:

ADOQuery1.Close;
ADOQuery1.SQL.Text := vSqlString;
ADOQuery1.Parameters.Clear;
ADOQuery1.Parameters.CreateParameter('paramID', ftInteger, pdInput, 10, vIntegerValue);
ADOQuery1.Open;

Or you can concatenate values to the query. For example:

//For Integer values:
vSqlString: = 'Select * From myTable Where myID =' + IntToStr (vIntegerValue); 

//For String values:
vSqlString: = 'Select * From myTable Where myID =' + QuotedStr (vStringValue); 

//For float values: 
//Be careful with this, usually in a query, the comma is separator values, 
//so make sure that the decimal separator is '.'
vDS := DecimalSeparator; //I keep the value it had 
DecimalSeparator := '.'; 
try
  ADOQuery1.close;
  ADOQuery1.SQL.Text := 'Select * From myTable Where myID='+FloatToStr(vFloatValue);
  ADOQuery1.Open;
finally
  DecimalSeparator := vDS; //Restore the value that had
end;

The third option is to set the parameters at design time. But I think this is not what you want.



来源:https://stackoverflow.com/questions/20813585/using-parsesql-command-for-ado-parameters-cause-invalid-parameter-datatype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!