简体   繁体   English

如何在ODBC本机客户端中获取SQL Server DateTime字段

[英]How to get SQL Server DateTime field in ODBC native client

I'm have SQL Server table: 我有SQL Server表:

CREATE TABLE [dbo].[Table1](
    [rec_id] [int] IDENTITY(1,1) NOT NULL,
    [id] [int] NOT NULL,
    [date] [datetime] NOT NULL,
    [ps] [varchar](200) NULL
) ON [PRIMARY]

I'm getting data by a code: 我通过代码获取数据:

status = SQLExecDirect(statement, (SQLWCHAR*)TEXT("SELECT * FROM [DBNAME].[dbo].[Table1]"), SQL_NTS);
cout << "SQLExecDirect returned " << status << "\r\n";
    if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
    {
        int rec_id;
        int id;
        char date[64];
        char ps[201] = { 0 };
        while (SQLFetch(statement) == SQL_SUCCESS)
        {
            SQLGetData(statement, 1, SQL_C_ULONG, &rec_id, 0, NULL);
            SQLGetData(statement, 2, SQL_C_ULONG, &id, 0, NULL);
            SQLGetData(statement, 3, SQL_C_CHAR, date, 64, NULL);
            SQLGetData(statement, 4, SQL_C_CHAR, ps, 201, &len);
            cout << rec_id << " " << id << " " << date << " " << ps << " " << len << endl;
        }
    }
    cout << "Connected;";
    cin.get();
    SQLDisconnect(connectHandle);

But I'm get date field as char array in output: 2014-01-01 00:00:00.000 How to get this field, for example, in FILETIME format or in other more convenient C++ data type? 但是我在输出中将date字段作为字符数组:2014-01-01 00:00:00.000如何获取此字段,例如,以FILETIME格式或其他更方便的C ++数据类型? Thanks. 谢谢。

For date type, you should use SQL_C_DATE or SQL_C_TIMESTAMP. 对于日期类型,您应该使用SQL_C_DATE或SQL_C_TIMESTAMP。 Example: 例:

   TIMESTAMP_STRUCT ts;
   SQLLEN           len;
   SQLGetData(hstmt, 3, SQL_C_TIMESTAMP, &ts, sizeof(ts), &len);
   printf("Date: %d-%d-%d %d:%d:%d.%d (%ld)\n",
           ts.year, ts.month,ts.day, ts.hour, ts.minute, ts.second, ts.fraction, len);

Hope this help. 希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM