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() {};
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;",
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;
}
};
};