如何在 ODBC 本机客户端中获取 SQL 服务器日期时间字段
How to get SQL Server DateTime field in ODBC native client
我有 SQL 服务器 table:
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]
我正在通过代码获取数据:
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);
但我在输出中得到 date
字段作为字符数组:2014-01-01 00:00:00.000
例如,如何以 FILETIME 格式或其他更方便的 C++ 数据类型获取此字段?谢谢
对于日期类型,您应该使用SQL_C_DATE或SQL_C_TIMESTAMP。示例:
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);
希望对您有所帮助。
我有 SQL 服务器 table:
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]
我正在通过代码获取数据:
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);
但我在输出中得到 date
字段作为字符数组:2014-01-01 00:00:00.000
例如,如何以 FILETIME 格式或其他更方便的 C++ 数据类型获取此字段?谢谢
对于日期类型,您应该使用SQL_C_DATE或SQL_C_TIMESTAMP。示例:
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);
希望对您有所帮助。