Using a datetime parameter with ADO (ODBC) loses time part

后端 未结 2 1858
余生分开走
余生分开走 2021-01-18 04:17

I stumbled onto this problem yesterday when I was busy writing some unit tests using SQLLite. My environment is Windows7/Delphi XE.

Using TADOQuery in conjunction wi

2条回答
  •  佛祖请我去吃肉
    2021-01-18 04:35

    I have tested this a bit using SQL Server and have the exact same problem when I use MSDASQL.1 (ODBC). Your code works fine with SQLOLEDB.1 and SQLNCLI10.1.

    If you specify the parameter type to be ftString it will save with time using ODBC, (at least on SQL Server).

    Qry.Parameters.ParamByName('d').DataType := ftString;
    Qry.Parameters.ParamByName('d').Value := DateTimeToStr(DT);
    

    Note: Be careful of local settings when you use DateTimeToStr it might not produce what your db wants it to be. A safe bet would be to use yyyy-mm-dd hh:mm:ss[.fff].

    Update:

    You could also set the data type of the ado parameter to adDBTimeStamp yourself. ADODB sets it to adDate when you use ftDateTime.

    Qry.Parameters.ParamByName('d').ParameterObject.Type_ := adDBTimeStamp;
    Qry.Parameters.ParamByName('d').Value := DT;
    

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题