如何使用ODBC(C ++)从MS SQL以字符串形式获取nvarchar? [英] How do I get an nvarchar from MS SQL as string using ODBC (C++)?

查看:197
本文介绍了如何使用ODBC(C ++)从MS SQL以字符串形式获取nvarchar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从SQL数据库中提取字符串,但是由于某种原因,我的参数错误,并且不确定为什么.这是我的代码:

I'm trying to extract a string from my SQL database, but for some reason my parameters are wrong and I'm not sure why. Here is my code:

SQLHENV environHandle;
SQLHDBC connectHandle;
SQLHSTMT statement;
SQLCHAR* connectString = "MY_CONNECTION_STRING";
string path;
int jobID;  
SQLINTEGER pathstrlen = SQL_NTS;

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &environHandle);
SQLSetEnvAttr(environHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
SQLAllocHandle(SQL_HANDLE_DBC, environHandle, &connectHandle);
SQLDriverConnect(connectHandle, NULL, connectString, SQL_NTS, NULL, 1024, NULL, SQL_DRIVER_NOPROMPT);
SQLAllocHandle(SQL_HANDLE_STMT, connectHandle, &statement);

//THIS IS THE BINDPARAMETER WITH THE ISSUE...
SQLBindParameter(statement, 1, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 400, 0, (SQLPOINTER)path.c_str(), path.length(), &pathstrlen);

SQLBindParameter(statement, 2, SQL_PARAM_OUTPUT, SQL_INTEGER, SQL_INTEGER, 10, 0, &jobID, 0, &pathstrlen);

SQLExecDirect(statement, (SQLCHAR*)"{CALL SP(?,?)}", SQL_NTS);

它运行正常,但是不会得到我请求的字符串信息,而第二个参数(获取整数)可以正常工作.我曾尝试将ParameterType更改为多种不同的东西,但是我要么会向我抛出错误(例如,已弃用SQL_LONGVARCHAR).

It runs fine, but won't get the string information I requested, while the second parameter (to get an integer) works fine. I've tried changing the ParameterType to multiple different things but I either get errors thrown at me (SQL_LONGVARCHAR for example, is deprecated).

在SQL中,我尝试获取的数据如下:

In SQL, the data I'm trying to get is as follows:

@Path nvarchar(4000) OUT
set @Path = 'test'

在此先感谢任何可以阐明这一点的人.我整天都在拔头发.

Thanks in advance to anyone who can shed light on this. I've been pulling my hair out all day.

推荐答案

ODBC支持

ODBC supports Unicode parameter types so use SQL_C_WCHAR and SQL_WVARCHAR instead of SQL_C_CHAR and SQL_VARCHAR respectively.

您还有另外两个问题.

首先,您要传递ANSI string作为参数.如果您使用的是Unicode,则需要使用宽字符串-wstring.

First you are passing an ANSI string as a parameter. If you are using Unicode you need to use a wide string - wstring instead.

第二,您没有将有效缓冲区传递给SQLBindParameter. string.c_str()返回的值是const char*只读缓冲区.将hat传递给需要可写缓冲区的函数是无效的-这样做会损坏您的字符串.但是,您不会看到任何损坏,因为对path.length()的调用将返回零,因此SQLBindParameter将永远不会返回任何数据.

Second you are not passing a valid buffer to SQLBindParameter. The value returned by string.c_str() is a const char* that is a read only buffer. It is not valid to pass hat to a function that requires a writable buffer - doing this will corrupt your string. However you won't see any corruption in your case because you the call to path.length() WILL return zero so SQLBindParameter will never return any data.

您将需要声明WCHAR数组缓冲区并将其传递给SQLBindParameter,这将为它提供一个有效的缓冲区以将数据写入其中.然后,如果需要C ++对象中的缓冲区,则可以将该缓冲区转移到wstring.

You will need to declare WCHAR array buffer and pass that to SQLBindParameter which will give it a valid buffer to write data into. You can then transfer that buffer to a wstring if you need it in a C++ object.

是这样的:

WCHAR path[401];  // 401 - width of you column + 1 for the null terminator
SQLBindParameter(statement, 1, SQL_PARAM_OUTPUT, SQL_C_WCHAR, SQL_WVARCHAR, 400, 0,     (SQLPOINTER)path, sizeof(path), &pathstrlen);

修改

从查看 ODBC数据转换表,如果您不想在应用程序中处理Unicode字符串,则应该可以使ODBC将ODBC的数据从Unicode转换为ANSI.

Ffrom looking at the ODBC data conversion table it appears that you should be able to get ODBC to convert that data from Unicode to ANSI for you if you to not want to deal with Unicode strings in your application.

char path[401];  // 401 - width of you column + 1 for the null terminator
SQLBindParameter(statement, 1, SQL_PARAM_OUTPUT, SQL_C_WCHAR, SQL_WVARCHAR, 400, 0,     (SQLPOINTER)path, sizeof(path), &pathstrlen);

这篇关于如何使用ODBC(C ++)从MS SQL以字符串形式获取nvarchar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆