Connect SQLServer failed in c++

I connect to the database but it goes to the case 'SQL_SUCCESS_WITH_INFO', I don't know what am I missing or where is wrong, hope everyone helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <windows.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <sql.h>

int Connect_SQL() {
	#define SQL_RESULT_LEN 240
	#define SQL_RETURN_CODE_LEN 1000
	#define MAX_STRING_LEN 255

	//define handles and variables
	SQLHANDLE sqlConnHandle;
	SQLHANDLE sqlStmtHandle;
	SQLHANDLE sqlEnvHandle;
	SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
	//initializations
	sqlConnHandle = NULL;
	sqlStmtHandle = NULL;
	//allocations
	if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle))
		goto COMPLETED;
	if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
		goto COMPLETED;
	if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle))
		goto COMPLETED;


	switch (
		SQLDriverConnect(sqlConnHandle,
		NULL,
		(SQLWCHAR*)L"DRIVER={SQL Server};SERVER = 103.151.23.147;DATABASE=test;UID=sa;PWD=44448888;",
		SQL_NTS,
		retconstring,
		1024,
		NULL,
		SQL_DRIVER_NOPROMPT))
	{
	case SQL_SUCCESS:
		MessageBox(NULL, L"Successfully connected to SQL Server", L"", MB_OK);
		break;
	case SQL_SUCCESS_WITH_INFO:
		MessageBox(NULL, L"SQL_SUCCESS_WITH_INFO", L"", MB_OK);
		break;
	case SQL_INVALID_HANDLE:
		MessageBox(NULL, L"SQL_INVALID_HANDLE", L"", MB_OK);
		goto COMPLETED;
	case SQL_ERROR:
		MessageBox(NULL, L"SQL_ERROR", L"", MB_OK);
		goto COMPLETED;
	default:
		break;
	}
	//if there is a problem connecting then exit application
	if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle))
		goto COMPLETED;
	//output
	MessageBox(NULL, L"Executing T-SQL query...", L"", MB_OK);
	//if there is a problem executing the query then exit application
	//else display query result
	if (SQL_SUCCESS != SQLExecDirect(sqlStmtHandle, (SQLWCHAR*)L"SELECT @@VERSION", SQL_NTS)) {
		MessageBox(NULL, L"Error querying SQL Server", L"", MB_OK);
		goto COMPLETED;
	}
	else {
		//declare output variable and pointer
		SQLCHAR sqlVersion[SQL_RESULT_LEN];
		SQLINTEGER ptrSqlVersion;
		while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
			SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlVersion, SQL_RESULT_LEN, &ptrSqlVersion);
			//display query result
			MessageBox(NULL, L"Query Result", L"", MB_OK);
		}
	}
	//close connection and free resources
COMPLETED:
	SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
	SQLDisconnect(sqlConnHandle);
	SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
	SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);

	return 0;
}
Last edited on
error message : changed database context to 'test'

Database name changed maybe because of unicode (SQLWCHAR*), how do I fix it
Last edited on
I connect to the database but it goes to the case 'SQL_SUCCESS_WITH_INFO'
See this:
https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlexecute-function?view=sql-server-ver15
When SQLExecute returns either SQL_ERROR or SQL_SUCCESS_WITH_INFO
So it is supposed to be ok. What is the problem?

Where does this error message come from?
1
2
3
4
5
6
7
void showSQLError(unsigned int handleType, const SQLHANDLE& handle)
	{
		SQLWCHAR SQLState[1024];
		SQLWCHAR message[1024];
		SQLGetDiagRec(handleType, handle, 1, SQLState, NULL, message, 1024, NULL);
		MessageBox(NULL,  message, L"", MB_OK);
	}


showSQLError : changed database context to 'test'


I don't know how to fix this problem

So where do you call showSQLError(....)?

Again:
changed database context to 'test'
appears not to be an error since 'test' is your database.
So where do you call showSQLError(....)?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
switch (
	SQLDriverConnect(sqlConnHandle,
	NULL,
	(SQLWCHAR*)L"DRIVER={SQL Server};SERVER = 103.151.23.147;DATABASE=test;UID=sa;PWD=44448888;",
	SQL_NTS,
	retconstring,
	1024,
	NULL,
	SQL_DRIVER_NOPROMPT))
	{
	case SQL_SUCCESS:
		MessageBox(NULL, L"Successfully connected to SQL Server", L"", MB_OK);
		break;
	case SQL_SUCCESS_WITH_INFO:
		MessageBox(NULL, L"SQL_SUCCESS_WITH_INFO", L"", MB_OK);
		showSQLError(SQL_HANDLE_DBC, sqlConnHandle);
		break;
	case SQL_INVALID_HANDLE:
		MessageBox(NULL, L"SQL_INVALID_HANDLE", L"", MB_OK);
		showSQLError(SQL_HANDLE_DBC, sqlConnHandle);
		goto COMPLETED;
	case SQL_ERROR:
		MessageBox(NULL, L"SQL_ERROR", L"", MB_OK);
		showSQLError(SQL_HANDLE_DBC, sqlConnHandle);
		goto COMPLETED;
	default:
		break;
        }


appears not to be an error since 'test' is your database

yes 'test' is my database
Last edited on
After i found out SQL_SUCCESS_WITH_INFO was a successful connection and i got the total number of rows of the table
I want to get the data in the table, how to do it, can you give me an example for reference?
See this:
https://docs.microsoft.com/en-us/sql/odbc/reference/syntax/sqlgetdata-function?view=sql-server-ver15

The Code Example shows how to read multiple entries:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#define NAME_LEN 50  
#define PHONE_LEN 50  
  
SQLCHAR      szName[NAME_LEN], szPhone[PHONE_LEN];  
SQLINTEGER   sCustID, cbName, cbAge, cbBirthday;  
SQLRETURN    retcode;  
SQLHSTMT     hstmt;  
  
retcode = SQLExecDirect(hstmt,  
   "SELECT CUSTID, NAME, PHONE FROM CUSTOMERS ORDER BY 2, 1, 3",  
   SQL_NTS);  
  
if (retcode == SQL_SUCCESS) {  
   while (TRUE) {  
      retcode = SQLFetch(hstmt);  
      if (retcode == SQL_ERROR || retcode == SQL_SUCCESS_WITH_INFO) {  
         show_error();  
      }  
      if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){  
  
         /* Get data for columns 1, 2, and 3 */  
  
         SQLGetData(hstmt, 1, SQL_C_ULONG, &sCustID, 0, &cbCustID);  
         SQLGetData(hstmt, 2, SQL_C_CHAR, szName, NAME_LEN, &cbName);  
         SQLGetData(hstmt, 3, SQL_C_CHAR, szPhone, PHONE_LEN,  
            &cbPhone);  
  
         /* Print the row of data */  
  
         fprintf(out, "%-5d %-*s %*s", sCustID, NAME_LEN-1, szName,   
            PHONE_LEN-1, szPhone);  
      } else {  
         break;  
      }  
   }  
}
I did it, thank you very much coder777
Registered users can post here. Sign in or register to post.