Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I spent too much trying to connect to a database, but no use, any ideas Will greatly be appreciated.
It is an MFC project, The problem is with the String Connection (maybe).

What I have tried:

I tried some libraries and some tutorials, but nothing is clear ( i can't believe it is too complicated)
I am working on this class now

<pre>#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <sqlext.h>
using namespace std;
#define SQL_RESULT_LEN 240
#define SQL_RETURN_CODE_LEN 1000
class Connect
{
	SQLHANDLE sqlConnHandle;
	SQLHANDLE sqlStmtHandle;
	SQLHANDLE sqlEnvHandle;
	SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
public :Connect() {
	sqlConnHandle = NULL;
	sqlStmtHandle = NULL;
	if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle))
		MessageBox(NULL, " connected to database\n", NULL, NULL);
	if (SQL_SUCCESS != SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
		MessageBox(NULL, " connected to database\n", NULL, NULL);
	if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle))
		MessageBox(NULL, " connected to database\n", NULL, NULL);
	};
	 ~Connect() {};
	//define handles and variables

	//initializations
public: void connect()
{
	
	switch (SQLDriverConnectW(sqlConnHandle,
		NULL,
		(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=TN1DEVLVTS49\\SQL2K14, 1433;UID=sa;PWD=Linedata1;DATABASE=Training_Project_DataBase;Trusted=true;",
		//DRIVER={SQL Server};SERVER=TN1DEVLVTS49\\SQL2K14, 1433;UID=sa;PWD=Linedata1;DATABASE=Training_Project_DataBase;Trusted=true;
		//Driver={SQL Server};Server=TN1DEVLVTS49\\SQL2K14;UID=sa;PWD=Linedata1;Database=Training_Project_DataBase;
		//(SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=Training_Project_DataBase;",
		SQL_NTS,
		retconstring,
		1024,
		NULL,
		SQL_DRIVER_NOPROMPT)) {
	case SQL_SUCCESS:
		MessageBox(NULL, "Successfully connected to SQL Server", NULL, NULL);

		break;
	case SQL_SUCCESS_WITH_INFO:

		MessageBox(NULL, "Successfully connected to SQL Server", NULL, NULL);
		break;
	case SQL_INVALID_HANDLE:

		MessageBox(NULL, " Could not connect to SQL Server", NULL, NULL);
	case SQL_ERROR:

		MessageBox(NULL, " Could not connect to SQL Server", NULL, NULL);
	default:
		break;
	}
};
};
Posted
Updated 9-Oct-18 0:17am
v2
Comments
OriginalGriff 9-Oct-18 4:40am    
That's going to depend on what kind of C++ project you are trying to write, and for what environment. A MFC / Native app would use different functions to a .NET C++ solution, and a non-Windows OS would probably use another set completely.

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
amine zawix 9-Oct-18 6:09am    
OriginalGriff. I am working on an MFC project and using Visual Studio and i already created a System SDN ODBC
I updated my question

1 solution

There are many ways, and you may find tons of tutorials, just Googling. See, for instance: Connect to SQL Server from Visual C++ | Tech How To's[^].
 
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