bcp_init and SQL Native Client

bcp_init and SQL Native Client

am 13.06.2007 16:23:26 von Peter

Hi all,

I am having trouble to get the bulk copy operations working in
collaboration with SQL Native Client.

My test program crashed with an access violation in the call to
bcp_init.

I know that the error is probably mine, but I cannot find any
mistakes.

I have include the C++ source below, and I hope that someone can help
me:

#include
#include
#include
#include
#include
#define _SQLNCLI_ODBC_
#include

#include
#include

namespace
{

HENV createEnvironment()
{
HENV environment;
SQLRETURN result;

result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE,
&environment);

if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
{
throw std::runtime_error("SQLAllocHandle failed");
}

result = SQLSetEnvAttr(environment, SQL_ATTR_ODBC_VERSION,
reinterpret_cast (SQL_OV_ODBC3), SQL_IS_INTEGER);

if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
{
throw std::runtime_error("SQLSetEnvAttr failed");
}

return environment;
}

SQLHDBC createConnection(HENV environment)
{
SQLHDBC connection;
SQLRETURN result;

result = SQLAllocHandle(SQL_HANDLE_DBC, environment, &connection);

if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
{
throw std::runtime_error("SQLAllocHandle failed");
}

// Need to set this prior to connection.

result = SQLSetConnectAttr(connection, SQL_COPT_SS_BCP, (void*)
SQL_BCP_ON,
SQL_IS_INTEGER);

result = SQLConnect(connection, reinterpret_cast (
const_cast ("database")), SQL_NTS, reinterpret_cast
(
const_cast ("user")), SQL_NTS, reinterpret_cast
(
const_cast ("password")), SQL_NTS);

if ((result != SQL_SUCCESS) && (result != SQL_SUCCESS_WITH_INFO))
{
throw std::runtime_error("SQLConnect failed");
}

return connection;
}

int realMain(int argc, char *argv[])
{
HENV environment = createEnvironment();
SQLHDBC connection = createConnection(environment);
RETCODE result;

result = bcp_init(connection, "testdata", 0, 0, DB_IN);

assert(result != FAIL);

return 0;
}

} // anonymous namespace

int main(int argc, char* argv[])
{
try
{
return realMain(argc, argv);
}

catch (const std::exception& ex)
{
std::cerr << "exception: " << ex.what() << std::endl;
}

return 1;
}