Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following is compiling and working:
rc = SQLConnect(ConHandle, (SQLWCHAR*)"dailyworkbook", SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS);

The following is compiling but I get a run time access violation:
c = SQLConnect(ConHandle, (SQLWCHAR*)L"dailyworkbook", SQL_NTS, NULL, SQL_NTS, NULL, SQL_NTS);

I am puzzled. Any insight would be helpful.

What I have tried:

Online help pages. Debugging in Visual studio.
Posted
Updated 15-Jun-18 0:39am

1 solution

I don't know for sure why the access violation occurs but it is probably due to the UserName and Authentication parameters. When those are NULL, you should also pass zero instead of SQL_NTS for the length:
C++
c = SQLConnect(ConHandle, (SQLWCHAR*)L"dailyworkbook", SQL_NTS, NULL, 0, NULL, 0);
or pass empty strings which may use SQL_NTS or zero
C++
c = SQLConnect(ConHandle, (SQLWCHAR*)L"dailyworkbook", SQL_NTS, (SQLWCHAR*)L"", SQL_NTS, (SQLWCHAR*)L"", SQL_NTS);

If you have to use char or wide strings depends on your project settings. The above assumes that you have a Unicode project.

I can also only guess why you did not get the access violation with the first call but I think this is a potential explanation:
When having a Unicode build and passing a char string, the connect will fail because the server can not be found and the function returns before using the additional parameters.
 
Share this answer
 
Comments
Member 13872723 15-Jun-18 1:52am    
I am now trying:
std::wstring dsn(L"dailyworkbook");
rc = SQLConnectW(ConHandle, (SQLWCHAR*)dsn.c_str(), dsn.length(), (SQLWCHAR*)L"", SQL_NTS, (SQLWCHAR*)L"", SQL_NTS);

The program compiles but run time access violation. If I replace the second parameter with 3/10/3000(any crazy value) the violation doesnt occur, but ODBC reports invalid string or buffer length. If I replace the second parameter with SQL_NTS/lenght()/strlen whatever, i get the access violation.
Jochen Arndt 15-Jun-18 3:02am    
What about my first example with NULL and 0?

In any case this should work and compile (with Unicode builds or using SQLConnectW):
SQLWCHAR dsn[] = L"dailyworkbook";
rc = SQLConnect(ConHandle, dsn, SQL_NTS, NULL, 0, NULL, 0);

The ugly casting is required here because the parameters are not defined const while there are not modified by the function. But string literals and std::string::c_str() are const. With C++ it would be better to use const_cast:
std::wstring dsn(L"dailyworkbook");
rc = SQLConnect(ConHandle, const_cast<SQLWCHAR*>(dsn.c_str()), SQL_NTS, NULL, 0, NULL, 0);
Member 13872723 15-Jun-18 5:20am    
Hi Jochen
Sorry for the repost but I am still finding my way around here.

I am getting an access violation on both of these. If I replace SQL_NTS with 0, in both cases there is no access violation but odbc says "[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".

I am getting desperate. Please help me.
Jochen Arndt 15-Jun-18 5:46am    
SQL_NTS means Null Terminated String.
That is, the function will determine the string length itself using the corresponding (preceeding) parameter.

If the string parameter is NULL, pass 0 (zero) for the length.
If the string parameter is a valid string (including an empty string), pass the correct length (in characters) or SQL_NTS.

If you pass zero for the DSN length, the name can be off course not found.

The documenattion and all examples are showing it the way from my examples.

So the only other source I can think of is your ConHandle. That will be also used only after the DSN has been found.
Has that been properly allocated?
Member 13872723 15-Jun-18 6:56am    
The connection handle is getting initialized properly. (At least it is not null).
I changed my build from Unicode to MultiByteCharSet. Got the access violation again when using SQL_NTS.
Am I missing an environmental setting here?
Thanks in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900