Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to make in this function
if connection = false
show message and close
MessageBoxA(0, "Failed to connect to Database!", "Error", MB_OK);
::ExitProcess(0);

if user, password, database = false
show message
C++
BOOL SQLCONNECT::Connect()
{
	GetPrivateProfileStringA("SQL", "User", "sa", this->m_szUser, 64, SQL_PATH);
	GetPrivateProfileStringA("SQL", "Password", "123456", this->m_szPassword, 64, SQL_PATH);
	GetPrivateProfileStringA("SQL", "Database", "MuOnline", this->m_szDatabase, 64, SQL_PATH);

	SQLHANDLE rgbValue;

	SQLAllocHandle(SQL_HANDLE_DBC, this->m_hEnviroment, &this->m_hConnection);
	SQLSetConnectAttr(this->m_hConnection, SQL_LOGIN_TIMEOUT, &rgbValue, 0);

	SQLRETURN Result = SQLConnect(this->m_hConnection, (SQLTCHAR *)this->m_szDatabase, SQL_NTS,
		(SQLTCHAR *)this->m_szUser, SQL_NTS, (SQLTCHAR *)this->m_szPassword, SQL_NTS);

	if (Result != SQL_SUCCESS && Result != SQL_SUCCESS_WITH_INFO)
	{
		SQLSMALLINT sRecord = 1, MsgLen;
		SQLTCHAR SqlState[6], SQLMsgError[SQL_MAX_MESSAGE_LENGTH];
		SQLINTEGER NativeError;

		if (SQLGetDiagRec(SQL_HANDLE_DBC, this->m_hConnection, sRecord, SqlState, &NativeError, SQLMsgError, sizeof(SQLMsgError), &MsgLen) != SQL_NO_DATA)
		{
			//cLog.ConsoleOutput("SQLSTATE:%s, Diagnosis:%s", SqlState, SQLMsgError);
		}

		return FALSE;
	}

	Result = SQLAllocHandle(SQL_HANDLE_STMT, this->m_hConnection, &this->m_hStmt);

	if (Result != SQL_SUCCESS && Result != SQL_SUCCESS_WITH_INFO)
	{
		return FALSE;
	}

	//cLog.ConsoleOutput("[SQL] Connection successfully");
	ReadyTableInstallation();
	return TRUE;
}
Posted
Updated 25-Dec-15 4:11am
v2
Comments
Richard MacCutchan 26-Dec-15 4:17am    
What is the problem?
diablo22 26-Dec-15 6:39am    
the problem is that even if i use wrong User and Password it still connect
but if i change Database Name to other wrong, Application is going on looopss.
To solve this i need an check IF USERNAME = WRONG ERROR
IF PASSWORD = WRONG ERROR
IF DATABASE = WRONG ERROR
Simply fix, if all are correct = CONTINUED
Richard MacCutchan 26-Dec-15 7:04am    
I do not think it is a sensible idea to store passwords as clear text in .INI files, and also have them hard coded in your program. That apart, I think the following statement is incorrect:

if (Result != SQL_SUCCESS && Result != SQL_SUCCESS_WITH_INFO)

You only get the details of the failure state if the Result is equal to SQL_SUCCESS_WITH_INFO.
diablo22 26-Dec-15 8:05am    
so can someone help me to fix this code, i must use information from .ini to get user password and database
Richard MacCutchan 26-Dec-15 10:41am    
Fix what?As far as using .INI files for passwords, it is a very poor idea. There is no point in using passwords if you are going to store them in clear text in that file and also in your code. You should use a dialog box and get the user to enter the details. You should also not use the sa userid for normal database access.

As to the other issue, you just need to change yor if statement as suggested in my previous comment.

1 solution

The MSDN topic for that function clearly states how to handle return code under Diagnostics paragraph

SQLConnect()[^]

if Result equals to SQL_ERROR or SQL_SUCCESS_WITH_INFO this means that something went wrong

in order to get more info about that you should use SQLGetDiagRec[^] funtion

here is a aircode sample ,based on yours

C++
if (Result == SQL_ERROR || Result == SQL_SUCCESS_WITH_INFO)
{
	SQLSMALLINT sRecord = 1, MsgLen;
	SQLTCHAR SqlState[6], SQLMsgError[SQL_MAX_MESSAGE_LENGTH];
	SQLINTEGER NativeError;
 
	if (SQLGetDiagRec(SQL_HANDLE_DBC, this->m_hConnection, sRecord, SqlState, &NativeError, SQLMsgError, sizeof(SQLMsgError), &MsgLen) != SQL_NO_DATA)
	{
		//WARNING : use a proper comparision function here
		//written as below to explain what to do
		if(SqlState == "28000")
		{
			MessageBox(NULL,"Incorrect User Name Or Password","Error",0);
		}
		
		//check other state codes here
		
	}
 
	return FALSE;
}
 
Share this answer
 

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