我有这样的SQL语句,但它返回: “错误转换VARCHAR到数字”错误转化为varchar到数字:SQL Server 2008中
ADOTailles.SQL.Text := 'INSERT INTO tailles (numOF, taille, quantite, prixVente) VALUES(''' + numOF.Text + ''',''' + C.Caption + ''',''' + Q.Text + ''',''' + P.Text + ''')';
ADOTailles.ExecSQL
的数值字段是prixVente;
我用这个,但仍是同样的错误:
ADOTailles.SQL.Text := 'INSERT INTO tailles (numOF, taille, quantite, prixVente) VALUES(''' + numOF.Text + ''',''' + C.Caption + ''',''' + Q.Text + ''',CAST(''' + P.Text + ''' AS numeric(5, 2)))');
ADOTailles.ExecSQL
注意:如果我把一个INTEGER没有错误
完整的代码是:
var
I: Int8;
C: TCheckBox;
Q, P: TEdit;
for I := 1 to 16 do Begin
C := FindComponent('T' + IntToStr(I)) as TCheckBox;
Q := FindComponent('Q' + IntToStr(I)) as TEdit;
P := FindComponent('P' + IntToStr(I)) as TEdit;
if C.Checked = True then begin
ADOTailles.SQL.Text := 'INSERT INTO tailles (numOF, taille, quantite, prixVente) VALUES(''' + numOF.Text + ''',''' + C.Caption + ''',''' + Q.Text + ''',''' + P.Text + ''')';
ADOTailles.ExecSQL
end;
End;
没有SQL注入,因为我使用此代码:
StringReplace(aricleFilter.Text, '''', '', [rfReplaceAll]);
您应该**不要**将您的SQL语句连接在一起 - 这非常容易出错并且存在SQL注入**的风险。相反 - 使用**参数化查询!**使用参数也经常使得提供不同类型的值(如字符串,整型等)变得更加容易 –
SQL注入==非常不专业。 –
[在这里阅读关于如何在Delphi中使用ADOQuery参数](http://delphi.about.com/od/database/l/aa050101a.htm) –