i am trying to use a parameterized query against ADO:
INSERT INTO Foo (Name, Value) VALUES(@name, @value)
In SQL Server the Name
column is a varchar
type. The Value
column is an nvarchar(max)
.
What size do i pass when creating a parameter when i don't know, or want to specify, the size?
procedure SaveTheThing(Connection: TADOConnection);
var
sql: WideString;
cmd: _Command;
begin
sql := 'INSERT INTO Foo (Name, Value) VALUES(@name, @value)';
cmd := CoCommand.Create;
cmd.Set_ActiveConnection(Connection.ConnectionObject);
cmd.Set_CommandType(adCmdText);
cmd.Set_CommandText(sql);
//and now add the parameters
cmd.Parameters.Append(
cmd.CreateParameter('@name', adVarChar, adParamInput, -1, filename)
);
cmd.Parameters.Append(
cmd.CreateParameter('@value', adVarWChar, adParamInput, -1, GetXmlToWideString)
);
cmd.Execute({out}recordsAffected, EmptyParam, adCmdSomeThatDoesntCauseAnExcetpion or adExecuteNoRecords);
end;
The simple alternative was going to be:
sql := 'INSERT INTO Foo (Name, Value)'#13#10+
'VALUES (+QuotedStr(filename)+', '+QuotedStrW(GetXmlToWideString)+')';
and be done already. But i thought i'd burn a few days trying to make parameterized queries a viable solution, and avoid having to write a QuotedStrW
.