About Bcp_init

Nov 23, 2005

Hi All,
When I use bcp APIs to import data into sqlserver database, after batch rows
to the tableA, I want to import data into tableB, so I invoke bcp_init again
to init tableB, but it can't success. unless I re-connect database and then
do bcp_init.
But re-connect db will waste some time. My question is why it can't be
success while do bcp_init?
Thanks

View 1 Replies


ADVERTISEMENT

ODBC Bcp_init Run Time Dynamic Link Library

Aug 9, 2007

Hi,

I'm having problems successfully executing bcp_init through a function pointer retrieved at run time from sqlncli.dll.

The call works when the library is statically linked.



Can someone please point out the problem? The commented out code in green executes successfully when the library is statically linked. Code marked in red fails.


SQLGetDiagRecW does not provide any useful information.

Any help is appreciated.

Regards,
Venkat


typedef SQLRETURN (__stdcall *SQLALLOCHANDLEPROC)
(
SQLSMALLINT HandleType,
SQLHANDLE InputHandle,
SQLHANDLE OutputHandle
);

/// typedef native function SQLSetEnvAttr

typedef SQLRETURN (__stdcall *SQLSETENVATTRPROC)

(

SQLHANDLE EnvironmentHandle,

SQLINTEGER Attribute,

SQLPOINTER Value,

SQLINTEGER StringLength

);

/// typedef native function SQLSetConnectAttrW

typedef SQLRETURN ( __stdcall * SQLSETCONNECTATTRPROCW )

(

SQLHANDLE ConnectionHandle,

SQLINTEGER Attribute,

SQLPOINTER ValuePtr,

SQLINTEGER StringLength

);

typedef SQLRETURN ( __stdcall *SQLCONNECTW )

(

SQLHANDLE ConnectionHandle,

SQLWCHAR * ServerName,

SQLSMALLINT NameLength1,

SQLWCHAR * UserName,

SQLSMALLINT NameLength2,

SQLWCHAR * Authentication,

SQLSMALLINT NameLength3

);



typedef RETCODE ( __stdcall *BCPINIT )

(

SQLHANDLE ConnectionHandle,

const WCHAR* TableName,

const WCHAR* DataFileNae,

const WCHAR* ErrorFileName,

SQLINTEGER Direction

);



main()
{

// Load library and initialize function pointers.

HMODULE hModule = ::LoadLibrary(_T("SQLNCLI.dll"));


SQLALLOCHANDLEPROC fnAlloc = (SQLALLOCHANDLEPROC)::GetProcAddress( hModule, _T("SQLAllocHandle") );

SQLSETENVATTRPROC fnEnvAttSet = (SQLSETENVATTRPROC)::GetProcAddress( hModule, _T("SQLSetEnvAttr") );

SQLSETCONNECTATTRPROCW fnConnectAttSet = (SQLSETCONNECTATTRPROCW)::GetProcAddress( hModule, _T("SQLSetConnectAttrW") );;

SQLCONNECTW fnConnectProc = (SQLCONNECTW)::GetProcAddress( hModule, _T("SQLConnectW") );

BCPINIT fnBCPInit = (BCPINIT)::GetProcAddress( hModule, _T("bcp_initW") );


// Allocate ODBC handles.

SQLHENV m_SQLEnvironment;

SQLHDBC m_SQLConnection;

SQLHSTMT m_SQLStatement;

SQLRETURN iReturn;


iReturn = fnAlloc( SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_SQLEnvironment);

//iReturn = SQLAllocHandle( SQL_HANDLE_ENV,SQL_NULL_HANDLE,&m_SQLEnvironment);




iReturn = fnEnvAttSet(m_SQLEnvironment,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0);

//iReturn = SQLSetEnvAttr(m_SQLEnvironment,SQL_ATTR_ODBC_VERSION,(SQLPOINTER) SQL_OV_ODBC3,0);


iReturn = fnAlloc(SQL_HANDLE_DBC,m_SQLEnvironment,&m_SQLConnection);

//iReturn = SQLAllocHandle(SQL_HANDLE_DBC,m_SQLEnvironment,&m_SQLConnection);


// Set up bulk copy.
int nType = SQL_COPT_SS_BCP;

SQLPOINTER pvAttribute = (SQLPOINTER)SQL_BCP_ON;

SQLINTEGER nValue = SQL_IS_INTEGER;

iReturn = fnConnectAttSet(m_SQLConnection, nType, pvAttribute, nValue );

//iReturn = SQLSetConnectAttrW(m_SQLConnection, nType, pvAttribute, nValue );

// Connect.

BSTR ODBCSource = :: SysAllocString(L"SQL2005");

BSTR UserName = :: SysAllocString(L"sa");

BSTR Password = :: SysAllocString(L"xxx");

iReturn = fnConnectProc(m_SQLConnection, ODBCSource,7,UserName,2,Password,3);

//iReturn = SQLConnectW(m_SQLConnection, ODBCSource,7,UserName,2,Password,3);

// Init bulk copy.
BSTR sTableName = :: SysAllocString(L"test");

BSTR sErrorFile = :: SysAllocString(L"c:\test4.txt");

int nDirection = DB_IN;

RETCODE nRet = fnBCPInit( m_SQLConnection, sTableName, NULL, sErrorFile, nDirection ); // nRet = 0

//RETCODE nRet = bcp_initW( m_SQLConnection, sTableName, NULL, sErrorFile, nDirection ); // nRet = 1


}

View 10 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved