开发者

How to make ADO parameter to update SQL Server datetime column?

edit I should have mentioned that I'm trying to do this with Delphi 2006.

OK, I think I have hit on a question with no previous answers.

I have a SQL Server database with columns of type datetime. When I try to insert a row with a parametrized command, I get

Exception class EOleException with message
'[Microsoft][ODBC SQL Server Driver]Optional feature not implemented'. 

My insert procedure looks like this:

procedure TForm1.btnZoomClick(Sender: TObject);
const
  InsCmd = 'insert into dbo.foo (added, edited, editor, narrative) ' +
           'values (:dateAdded, :dateEdited, :theEditor, :theNarrative);';
begin
  dmDbToy2.DataModule2.ADOCommand1.CommandText := InsCmd;
  with DataModule2.ADOCommand1.Parameters do
  begin
// the following line was an attempt to trick VarAsType into making开发者_开发百科 a
// adDbTimeStamp: VarAsType is having none of it.
//    FindParam('dateAdded').Value := VarAsType(VarFromDateTime(Now), 135);
    FindParam('dateAdded').Value := VarFromDateTime(Now);
    FindParam('dateEdited').Value := Null;
    FindParam('theEditor').Value := 'wades';
    FindParam('theNarrative').Value := Null;
  end;
  DataModule2.ADOCommand1.Execute;
end;

I found some postings via google which seem to indicate that SQL Server wants a adDbTimeStamp type to update these columns, but VarAsType does not want to make one for me.

Is there a way to create a value for the dateAdded and dateEdited parameters in the code sample?


In the comments thread on the original question, user RRUZ made a suggestion that turned out to resolve the issue: The problem was with the provider. Namely, I was using the OLEDB Provider for ODBC rather than the OLEDB Provider for SQL Server. Changing the provider as suggested made the 'Optional feature not implemented' error message go away and enabled the insert to work with a simple assignment of TDateTime to TParameter.Value, thusly:

FindParam('dateAdded').Value := Now;


Set the datatype for the parameter, it might do a difference in how the parameters is treated. I would also recommend that you use ParamByName instead of FindParam. With ParamByName you get a Param xx not found exception if the parameters does not exist in the Parameters collection. FindParam returns nil if it is not found. I have never needed to use any variant conversion stuff when assigning parameters for a TADOCommand so think you should remove that as well.

Try this.

    with ParamByName('dateAdded') do
    begin
        DataType := ftDateTime;
        Value := Now;
    end;

    with ParamByName('dateEdited') do
    begin
        DataType := ftDateTime;
        Value := Null;
    end;

    with ParamByName('theEditor') do
    begin
        DataType := ftString; // or ftWideString if you use nchar/nvarchar
        Value := 'wades';
    end;

    with ParamByName('theNarrative') do
    begin
        //DataType := ftString // Don't  know datatype here
        Value := Null;
    end;


Just set the parameter as a datetime. I do it all the time in ADO and other conection layers

DataModule2.ADOCommand1.Parameters.ParamByName('dateAdded').Value := Now();
//other code
DataModule2.ADOCommand1.Parameters.ParamByName('dateEdited').Value := Null;
//other code
DataModule2.AdoCommand1.Execute;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜