Sql server 2008 如何使用ODBC将值绑定到DATETIME2(7)类型的存储过程参数
使用ODBC将值绑定到类型为Sql server 2008 如何使用ODBC将值绑定到DATETIME2(7)类型的存储过程参数,sql-server-2008,odbc,sql-server-2008-r2,Sql Server 2008,Odbc,Sql Server 2008 R2,使用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_IN
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
谢谢你的帮助 首先让我说,ODBC datetime2交互在MSDN中被可怕地记录了下来。通过反复试验、搜索MSDN(一直到SQL Server Denali CTP文档)、无数论坛等,进行了大量工作后,以下是在ODBC中正确绑定DATETIME2(7)参数的方法:
// 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
愤怒的人这对我来说是一个很大的帮助。非常感谢。(*)