2
我正在編寫使用SQL Server本地客戶端sqlncli.h的小型C++控制檯應用程序(VS 2013/Windows7)。我在程序中的NULL字段有問題 - 我看不到正確的值。從SQL Server返回C++本機客戶端中的NULL數據類型
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sqlncli.h>
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int main()
{
SQLHENV environHandle;
SQLHDBC connectHandle;
SQLHSTMT statement;
SQLWCHAR* connectString = (SQLWCHAR*)TEXT("Driver={SQL Server};Server=XX.XX.XX.XX;Database=DBNAME;Trusted_Connection=yes;");
SQLRETURN status;
status = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &environHandle);
cout << "SQLAllocHandle returned " << status << "\r\n";
if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
{
status = SQLSetEnvAttr(environHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3_80, SQL_IS_INTEGER);
cout << "SQLSetEnvAttr returned " << status << "\r\n";
if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
{
status = SQLAllocHandle(SQL_HANDLE_DBC, environHandle, &connectHandle);
cout << "SQLAllocHandle SQL_HANDLE_DBC returned " << status << "\r\n";
if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
{
status = SQLDriverConnect(connectHandle, NULL, connectString, SQL_NTS, NULL, 256, NULL, SQL_DRIVER_NOPROMPT);
cout << "SQLDriverConnect returned " << status << "\r\n";
if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
{
status = SQLAllocHandle(SQL_HANDLE_STMT, connectHandle, &statement);
cout << "SQLAllocHandle SQL_HANDLE_STMT returned " << status << "\r\n";
if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
{
status = SQLExecDirect(statement, (SQLWCHAR*)TEXT("SELECT * FROM [DBNAME].[dbo].[Table1]"), SQL_NTS);
cout << "SQLExecDirect returned " << status << "\r\n";
if (status == SQL_SUCCESS_WITH_INFO || status == SQL_SUCCESS)
{
int rec_id;
int id;
char date[64];
char ps[201];
while (SQLFetch(statement) == SQL_SUCCESS)
{
SQLGetData(statement, 1, SQL_C_ULONG, &rec_id, 0, NULL);
SQLGetData(statement, 2, SQL_C_ULONG, &id, 0, NULL);
SQLGetData(statement, 3, SQL_C_CHAR, date, 64, NULL);
SQLGetData(statement, 4, SQL_C_CHAR, ps, 201, NULL);
cout << rec_id << " " << id << " " << date << " " << ps << endl;
}
}
}
cout << "Connected;";
cin.get();
}
}
}
}
return 0;
}
表1結構:
CREATE TABLE [dbo].[Table1](
[rec_id] [int] IDENTITY(1,1) NOT NULL,
[id] [int] NOT NULL,
[date] [datetime] NOT NULL,
[ps] [varchar](200) NULL
) ON [PRIMARY]
在控制檯出來我最先看到的3場是正確的,但第四場就像╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠如何捕獲VARCHAR,NVARCHAR,FLOAT類型的NULL字段?