Hi all
I try to insert the datetime into the database by ODBC, the data can be insert into the data base but the second and millisecond has missing, (for example :2019-4-15 15:35:00.000) , and the sqlstate is 01004, but there is no problem when I use sqlserer 2012
and 2014.
I also write the example to test it ,but get some problem when the code go to the SQLExecDirect (SQLstate
42000), here is the code:
SQLHENV *hEnv = NULL; // ODBC Environment Handle
SQLHANDLE *hDbc = NULL; // ODBC Connection Handle
SQLHANDLE *hStmt = NULL; // ODBC Statement Handle
/*
** Connection Information
*/
TCHAR *dsn = _T("test1");
TCHAR *uid = _T("TestUser");
TCHAR *pwd = _T("P@ssw0rd");
TCHAR *szSelect = _T("INSERT INTO dbo.Table_1(time1) VALUES(?)");
TCHAR *pParameterArray = _T("{ts '2018-07-13 13:32:57.632'}");
SQLRETURN rc;
/*
** Allocate handles
*/
rc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, (SQLHANDLE *)&hEnv);
rc = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
rc = SQLAllocHandle(SQL_HANDLE_DBC, hEnv, (SQLHANDLE *)&hDbc);
/*
** Connect to the database
*/
rc = SQLConnect(hDbc, dsn, (SQLSMALLINT)_tcslen(dsn),
uid, (SQLSMALLINT)_tcslen(uid),
pwd, (SQLSMALLINT)_tcslen(pwd));
SQLWCHAR SqlState[6], SQLStmt[100], Msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER NativeError;
SQLSMALLINT i, MsgLen;
SQLRETURN rc1, rc2;
SQLHSTMT hstmt;
SDWORD nSQLErrorCode;
SWORD nMsgSize;
if ((rc == SQL_SUCCESS_WITH_INFO) || (rc == SQL_ERROR)) {
SQLLEN numRecs = 0;
SQLGetDiagField(SQL_HANDLE_STMT, hDbc, 0, SQL_DIAG_NUMBER, &numRecs, 0, 0);
// Get the status records.
int nError = SQLError(hEnv,
hDbc,
hStmt,
SqlState,
&nSQLErrorCode,
Msg, sizeof(Msg),
&nMsgSize);
i = 1;
while (i <= numRecs && (rc2 = SQLGetDiagRec(SQL_HANDLE_STMT, hDbc, i, SqlState, &NativeError,
Msg, sizeof(Msg), &MsgLen)) != SQL_NO_DATA) {
//
DisplayError(SqlState, NativeError, Msg, MsgLen);
i++;
}
}
/*
** Allocate the statement handle
*/
rc = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, (SQLHANDLE *)&hStmt);
//// Step 1: Prepare
//rc = SQLPrepare(hStmt, szSelect, SQL_NTS); // select
#if 1
rc = SQLBindParameter(hStmt,
1,
/* ipar: Param number, start at 1 */
SQL_PARAM_INPUT,
/* fParamType: SQL_PARAM_INPUT, ... */
SQL_C_TCHAR,
/* fCType: SQL_C_FLOAT, ... */
SQL_C_TIMESTAMP,
/* fSqlType: SQL_FLOAT, ... */
19,
/* cbColDef: precision, ... */
3,
/* ibScale: ... */
pParameterArray,
/* rgbValue: pointer to data buffer */
128,
/* cbValueMax: 64*/
NULL
/* pcbValue: Return length, SQL_NTS, SQL_NULL_DATA NULL*/
);
#endif
if (rc != 0)
{
SQLTCHAR MessageText[256];
SQLTCHAR SqlState[5 + 1];
SQLINTEGER NativeError;
SQLSMALLINT TextLength;
SQLRETURN sts = SQL_SUCCESS;
rc = SQLGetDiagRec(SQL_HANDLE_STMT, hStmt, 1, SqlState, &NativeError,
(SQLTCHAR *)&MessageText, sizeof(MessageText),
&TextLength);
AfxMessageBox(MessageText);
}
rc = SQLExecDirect(hStmt,szSelect,_tcslen(szSelect)*sizeof(TCHAR));