从SQL Server返回C ++本机客户端中的NULL数据类型

时间:2015-03-25 10:09:34

标签: c++ sql-server odbc type-conversion sql-server-native-client

我正在编写使用SQL Server本机客户端sqlncli.h的小型C ++控制台应用程序(VS 2013 / Windows7)。我在程序中使用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]

在控制台输出中我看到前三个字段正确,但第四个字段就像╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠如何捕获VARCHAR,NVARCHAR,FLOAT类型的NULL字段?

1 个答案:

答案 0 :(得分:1)

SQLGetData函数的最后一个参数是

  

StrLen_or_IndPtr

     

[Output]指向要返回的缓冲区的指针   长度或指标值。如果这是一个空指针,没有长度或   返回指标值。这在数据存在时返回错误   fetched是NULL。 SQLGetData可以返回以下值   长度/指标缓冲区:

     

可返回的数据长度
  SQL_NO_TOTAL
  SQL_NULL_DATA

您目前正在传递NULL,但如果您提供指向有效SQLLEN变量的指针,则可以检查SQL_NULL_DATA返回。

此外,如果您初始化缓冲区(例如char ps[201] = {0};),那么如果没有为某个字段返回数据,您将无法获得垃圾打印输出。