使用ODBC将值绑定到类型DATETIME2(7)
的存储过程参数的正确方法是什么。我想做点什么:
{CALL myproc (?,?)}
所以,我们假设第一个参数是一个int,第二个参数是DATETIME2
。我能做到:
// Bind parameter 1, which is an INT
int val=5;
SQLLEN len=sizeof(val);
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 4, 0,
&val, sizeof(val), &len);
// Bind parameter 2, a DATETIME2(7) value
// How do I do this??? How do I store the DATETIME2(7) value so that it can be
// bound and is not a string or degenerate DATETIME type
感谢您的帮助!
答案 0 :(得分:3)
首先让我说MSD中的ODBC datetime2交互是非常可靠的。经过反复试验,在MSDN上搜索(一直到SQL Server Denali CTP文档),无数的论坛等...,这里是正确绑定DATETIME2(7)参数的方法。 ODBC:
// This is a sample SQL_TIMESTAMP_STRUCT object, representing:
// 1987-06-05T12:34:45.1234567
// Notice the two zeroes at the end of the fractional part?
// These _must_ be zeroes, because the fractional part is in nanoseconds!
// If you put non-zeroes in this part, you _will_ get a binding error.
SQL_TIMESTAMP_STRUCT ts=
{1987,6,5,12,34,45,123456700};
SQLRETURN result=::SQLBindParameter(hStmt,
2, // Parameter idx in my original question
SQL_PARAM_INPUT,
SQL_C_TIMESTAMP,
SQL_TYPE_TIMESTAMP,
// Next is the length of the _string_ repr. of the DATETIME2(7) type! WTF???
// It should be 20 + {the value of the precision of the DATETIME2}
// Remember that we are using: 1987-06-05T12:34:45.1234567 for this example
27,
7, // This is the precision of the DATETIME2
&ts,
sizeof(ts),
NULL);
if (result!=SQL_SUCCESS)
...
我希望这对下一个遭受ODBC和DATETIME2
的愤怒的人有所帮助。