简体   繁体   English

通过ODBC C ++将带有日期时间的记录插入SQL Server 2014的问题

[英]Issue to insert record with datetime to SQL Server 2014 via ODBC C++

I have a simple c++ code to insert a record in SQL Server 2014 via ODBC. 我有一个简单的c ++代码,可通过ODBC在SQL Server 2014中插入记录。 It works fine with varchar fields, but it does not work with datetime field. 它与varchar字段一起正常工作,但不适用于datetime字段。

SQLCHAR buf[64];
SQLLEN len;
SQLCHAR buf2[255];
SQLLEN len2;
SQLLEN len3;
double f;
rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_TYPE_TIMESTAMP, 19, 0, (SQLCHAR*)buf, 0, &len);
rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 255, 0, (SQLCHAR*)buf2, 0, &len2);
rc = SQLBindParameter(hstmt, 3, SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, 15, 0, &f, 0, &len3);
rc = SQLPrepare(hstmt, (SQLCHAR*)"insert into test(timepoint,strvalue,floatvalue) values (?,?,?)", SQL_NTS);

strcpy((char*)buf,"20171010 00:08:14");
len=strlen("20171017 00:08:14");
strcpy((char*)buf2,"simple string");
len2=strlen("simple string");
f=1.34e+8;

SQLSMALLINT NumParams;
SQLNumParams(hstmt, &NumParams);
rc = SQLExecute(hstmt);

Can anybody help to understand that is wrong? 任何人都可以帮助了解这是错误的吗? I've got always: 我总是:

"22018:1:0:[Microsoft][ODBC SQL Server Driver]Invalid character value for cast specification\\n" “ 22018:1:0:[Microsoft] [ODBC SQL Server驱动程序]强制转换规范的字符值\\ n”

If I'm wrong here with my question, please give a hint which forum fits better. 如果我在此处提出的问题有误,请提示一下哪个论坛更合适。 I've spent some days, but can't find the solution. 我已经花了几天时间,但找不到解决方案。 For any help would be very appreciate. 对于任何帮助将不胜感激。

SQL Server can cast various strings in different date formats to a real date implicitly, but you must be very careful how to use them: SQL Server可以将不同日期格式的各种字符串隐式转换为实际日期 ,但是您必须非常小心如何使用它们:

There is the unseparated date format "yyyyMMdd", which cannot carry a time! 日期格式为“ yyyyMMdd”, 无法 分隔不能包含时间!

SELECT CAST('20170102' AS DATETIME) --2017-01-02 00:00:00.000

The most universal format is ISO8601 , which is yyyy-MM-ddTHH:mm:ss (beware of the the T in the middle and the usage of 24 hours format!) 最通用的格式是ISO8601 ,它是yyyy-MM-ddTHH:mm:ss (请注意中间的T和24小时格式的用法!)

SELECT CAST('2017-01-02T12:23:59' AS DATETIME) --2017-01-02 12:23:59.000

And - last but not least - there are three ODBC formats: 并且-最后但并非最不重要-三种ODBC格式:

SELECT CAST({d'2017-01-02'} AS DATETIME)           --2017-01-02 00:00:00.000
SELECT CAST({t'23:35:51'} AS DATETIME)             --2018-01-19 23:35:51.000 (today!)
SELECT CAST({ts'2017-01-02 23:35:51'} AS DATETIME) --2017-01-02 23:35:51.000

And a last - important! 最后-重要! - hint: Some formats work, but depend on the system's settings. -提示:某些格式可以使用,但是取决于系统的设置。 This can work for you and pass all internal tests, but might break on a customer's machine: 这可以为您工作并通过所有内部测试,但可能会在客户的计算机上中断:

SET LANGUAGE ENGLISH;
SELECT CAST('2017-01-02 23:35:51' AS DATETIME) -- Jan 2
SET LANGUAGE GERMAN;
SELECT CAST('2017-01-02 23:35:51' AS DATETIME) -- Feb 1

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

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