资料上说C语言数据类型名称是SQL_C_TYPE_TIMESTAMP, ODBC 数据类型定义是SQL_TIMESTAMP_STRUCT
我测试不对.SQL_TIMESTAMP_STRUCT tmCol6;
SQLINTEGER iCol6;
..........SQLGetData(hSTMT, 6, SQL_C_TYPE_TIMESTAMP, &tmCol6, 0, &iCol6);这个tmCol6值不对.
我测试不对.SQL_TIMESTAMP_STRUCT tmCol6;
SQLINTEGER iCol6;
..........SQLGetData(hSTMT, 6, SQL_C_TYPE_TIMESTAMP, &tmCol6, 0, &iCol6);这个tmCol6值不对.
我用的是ODBC API 不是 MFC ,
在sql.h和sqlext.h里找不到COleDateTime
You have run the following SQL script to create a table and insert a value:
if exists (select * from sysobjects where id = object_id('dbo.TTimeStamp') and sysstat & 0xf = 3)
drop table dbo.TTimeStamp
GOCREATE TABLE dbo.TTimeStamp (
col1 char (10) NOT NULL ,
myTimeStamp timestamp NOT NULL
)
GOInsert into TTimestamp (col1) values("hello")
GO
Sample Code
#include <windows.h>
#include <stdio.h>
#include <sql.h>
#include <sqlext.h>int main(int argc, char* argv[])
{
SQLCHAR* theDiagState = new SQLCHAR[50];
SQLINTEGER theNativeState;
SQLCHAR* theMessageText = new SQLCHAR[255];
SQLSMALLINT iOutputNo; SQLHENV m_SQLEnvironment;
SQLHDBC m_SQLConnection;
SQLHSTMT m_SQLStatement; SQLRETURN iReturn;
SQLINTEGER iData;
SQLCHAR* cData = new SQLCHAR[20];
SQLCHAR* cData1 = new SQLCHAR[20];
SQLCHAR* cMyTimeStamp = new SQLCHAR[20]; //Connect
//Allocate Environment Handle
iReturn = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_SQLEnvironment); //Set environment to ODBC_3
iReturn = SQLSetEnvAttr(m_SQLEnvironment,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0); //Allocate connection handle
iReturn = SQLAllocHandle(SQL_HANDLE_DBC,m_SQLEnvironment,&m_SQLConnection); //Connect to the database.
//In this example we have used the following:
//LocalServer as the DSN name.
//sa is the login name with out a password.
//CHANGE THE DSN NAME and UserId and Password here.
iReturn = SQLConnect(m_SQLConnection,(SQLCHAR*) "LocalServer",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"",SQL_NTS);
if (iReturn != SQL_ERROR)
{
//Run the query.
//Allocate the statement handle.
iReturn = SQLAllocHandle(SQL_HANDLE_STMT,m_SQLConnection,&m_SQLStatement);
//CHANGE THE TABLE/COLUMN NAME HERE.
//In this case we have used a table named TTimeStamp in SQL Server 6.5 or SQL Server 7.0 with two fields.
//Col1 = Char(10) Primary
//myTimeStamp = TimeStamp
//Execute the statement to get some timestamp value. iReturn = SQLExecDirect(m_SQLStatement,(SQLCHAR*) "Select * from TTimeStamp",SQL_NTS);
iReturn = SQLBindCol(m_SQLStatement,2,SQL_C_CHAR,cData,17,&iData);
while( TRUE)
{
iReturn = SQLFetch(m_SQLStatement);
if (!((iReturn == SQL_SUCCESS) || (iReturn == SQL_SUCCESS_WITH_INFO)))
break; }
//Copying timestamp data to another location. This saves the last time stamp value. We will use this
//to query the table a second time.
strcpy((char*)cMyTimeStamp, (const char*) cData);
iReturn = SQLCancel(m_SQLStatement); iData = SQL_NTS;
iReturn = SQLBindParameter(m_SQLStatement,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_BINARY,8,0,cMyTimeStamp,0,&iData);
//Executing select statement with the timestamp as parameter.
iReturn = SQLExecDirect(m_SQLStatement,(SQLCHAR*) "Select * from tTimeStamp where myTimeStamp = ?",SQL_NTS);
if (iReturn != SQL_SUCCESS)
{
SQLGetDiagRec(SQL_HANDLE_STMT,m_SQLStatement,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); }
//Bind the column again to see the data.
iReturn = SQLBindCol(m_SQLStatement,2,SQL_C_CHAR,cData1,17,&iData);
while( TRUE)
{
//We will get only one record this time.
iReturn = SQLFetch(m_SQLStatement);
if (!((iReturn == SQL_SUCCESS) || (iReturn == SQL_SUCCESS_WITH_INFO)))
break; }
//DISCONNECT iReturn = SQLFreeHandle(SQL_HANDLE_STMT,m_SQLStatement);
iReturn = SQLDisconnect(m_SQLConnection);
iReturn = SQLFreeHandle(SQL_HANDLE_DBC,m_SQLConnection);
iReturn = SQLFreeHandle(SQL_HANDLE_ENV,m_SQLEnvironment); m_SQLStatement = NULL;
m_SQLConnection = NULL;
m_SQLEnvironment = NULL;
}
else
{
//If it fails to connect theMessageText contains the reason for the failure.
SQLGetDiagRec(SQL_HANDLE_DBC,m_SQLConnection,1,theDiagState,&theNativeState,theMessageText,100,&iOutputNo); }
delete cData;
delete cData1;
delete cMyTimeStamp;
delete theMessageText;
delete theDiagState;
return 1;}