Click here to Skip to main content
15,885,278 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 6:17
mvaKenneth Haugland19-Mar-18 6:17 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 7:07
professional Randor 19-Mar-18 7:07 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 9:06
mvaKenneth Haugland19-Mar-18 9:06 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 9:14
professional Randor 19-Mar-18 9:14 
Questionproblem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Tarun Jha17-Mar-18 1:30
Tarun Jha17-Mar-18 1:30 
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Victor Nijegorodov17-Mar-18 5:52
Victor Nijegorodov17-Mar-18 5:52 
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
CPallini17-Mar-18 11:58
mveCPallini17-Mar-18 11:58 
QuestionConnection String Couldn't Connect Pin
Brennan Salibrici15-Mar-18 10:17
Brennan Salibrici15-Mar-18 10:17 
Hi Everyone.

I'm new to the site and new to C++. I'm coming from a background in VB and database admin.

I've got a console app which is trying to connect to an MS Access database and I'm having a terrible time trying to make this happen. All I'm trying to do here is execute a simple SQL SELECT statement and have the results come back.

The code below compiles just fine and runs fine but it keeps telling me it can't connect to the data source. I'm using VS 2017 and I've been able to set up a data connection which tests successfully every time. I've done my best to make sure I have all the correct drivers and other connection dlls. I'm a bit confused as to how I can have a successful connection within the VS environment (meaning I can browse tables, make queries, etc in VS) but I can't connect in my code. I've tried to copy and paste the 'build connection string' when I built the data connection and I also tried to reference the connection by name and no luck so far.

Thanks for your help!

Brennan

C++
// DTP_console.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include <Windows.h>
#include <sql.h>
#include <sqltypes.h>
#include <sqlext.h>
#include <comutil.h>
#include <winerror.h>

SQLCHAR* dsnName = (SQLCHAR*)"Driver={Microsoft Access Driver (*.mdb, *.accdb)};dbq=C:\Program Files\MyTrading\DTP\DTP_DB.accdb;defaultdir=C:\Program Files\MyTrading\DTP;driverid=25;fil=MS Access;filedsn=C:\Program Files\MyTrading\DTP\DTP_DB.accdb.dsn;maxbuffersize=2048;maxscanrows=8;pagetimeout=5;safetransactions=0;threads=3;uid=admin;usercommitsync=Yes";

#define db "C:\\Program Files\\MyTrading\\DTP\\DTP_DB.accdb"

using namespace std;


int main()
{
	/*Data Access Method used in this example*/
	const char* DAM = "Direct ODBC";

	HENV hEnv;
	HDBC hDbc;

	//ODBC API Return Status

	RETCODE rc;

	int iConnStrLength2Ptr;
	char szConnStrOut[256];

	
	SQLWCHAR* strSQL = (SQLWCHAR*)"SELECT Exchanges.[X Name], Exchanges.[X Full Name] FROM Exchanges;";

	SQLCHAR		chval1[128], chval2[128];
	SQLWCHAR	colName[128];
	int ret1;
	int ret2;

	//Number of rows and columns in result set

	SQLINTEGER	rowCount = 0;
	SQLSMALLINT fieldCount = 0, currentField = 0;
	HSTMT		hStmt;

	//Allocate an environment handle
	rc = SQLAllocEnv(&hEnv);
	//Allocate a connection handle
	rc = SQLAllocConnect(hEnv, &hDbc);

	//Connect to database
	rc = SQLDriverConnect(hDbc, NULL, (wchar_t*)dsnName, SQL_NTS, (wchar_t*)szConnStrOut, 255, (SQLSMALLINT*)&iConnStrLength2Ptr, SQL_DRIVER_NOPROMPT);
		
	if (SQL_SUCCEEDED(rc))
	{
		printf("%s: Successfully connected to database. Data Source Name: \n %s\n",
			DAM, szConnStrOut);

		//Prepare SQL Query
		printf("%s: SQL query:\n %s\n", DAM, strSQL);

		rc = SQLAllocStmt(hDbc, &hStmt);
		rc = SQLPrepare(hStmt, strSQL, SQL_NTS);

		//Bind result set columns to the local buffers
		rc = SQLBindCol(hStmt, 1, SQL_C_CHAR, chval1, 128, (SQLLEN*)&ret1);
		rc = SQLBindCol(hStmt, 2, SQL_C_CHAR, chval2, 128, (SQLLEN*)&ret2);

		//Execute the query and create a recordset
		rc = SQLExecute(hStmt);
		if(SQL_SUCCEEDED(rc))
		{
			printf("%s: Retrieve schema info for the given result set :\n", DAM);
			SQLNumResultCols(hStmt, &fieldCount);
			if (fieldCount > 0)
			{
				for (currentField = 1; currentField <= fieldCount; currentField++)
				{
					SQLDescribeCol(hStmt, currentField, colName, sizeof(colName), 0, 0, 0, 0, 0);
					printf(" | %s", colName);
				}
				printf("\n");
			}
			else
			{
				printf("%s: Error: Number of fields in the result set is 0.\n", DAM);
			}

			printf("%s: Fetch the actual data:\n", DAM);

			//Loop through the rows in the result set

			rc = SQLFetch(hStmt);
			while (SQL_SUCCEEDED(rc))
			{
				printf(" \ %s | %s\n", chval1, chval2);
				rc = SQLFetch(hStmt);
				rowCount++;
			};

			printf("%s: Total Row Count: %d\n", DAM, rowCount);
			rc = SQLFreeStmt(hStmt, SQL_DROP);
		}

	}
	else
	{
		printf("%s: Couldn't connect to %s.\n", DAM, dsnName);
	}

	//Disconnect and free up allocated handles
	SQLDisconnect(hDbc);
	SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
	SQLFreeHandle(SQL_HANDLE_ENV, hEnv);

	printf("%s: Cleanup. Done.\n", DAM);

    return 0;
}

AnswerRe: Connection String Couldn't Connect Pin
Gerry Schmitz15-Mar-18 11:01
mveGerry Schmitz15-Mar-18 11:01 
AnswerRe: Connection String Couldn't Connect Pin
Victor Nijegorodov15-Mar-18 11:25
Victor Nijegorodov15-Mar-18 11:25 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 17:12
leon de boer15-Mar-18 17:12 
GeneralRe: Connection String Couldn't Connect Pin
Jochen Arndt15-Mar-18 22:10
professionalJochen Arndt15-Mar-18 22:10 
GeneralRe: Connection String Couldn't Connect Pin
Victor Nijegorodov16-Mar-18 8:41
Victor Nijegorodov16-Mar-18 8:41 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer18-Mar-18 4:20
leon de boer18-Mar-18 4:20 
AnswerRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 16:49
leon de boer15-Mar-18 16:49 
GeneralRe: Connection String Couldn't Connect Pin
Brennan Salibrici16-Mar-18 2:45
Brennan Salibrici16-Mar-18 2:45 
QuestionRe: Connection String Couldn't Connect Pin
David Crow16-Mar-18 16:43
David Crow16-Mar-18 16:43 
Questionproblem in operator overloading of >> & << ? Pin
Tarun Jha15-Mar-18 7:47
Tarun Jha15-Mar-18 7:47 
AnswerRe: problem in operator overloading of >> & << ? Pin
CPallini15-Mar-18 10:47
mveCPallini15-Mar-18 10:47 
QuestionCan we include two Message Maps in one class? Pin
Sampath57914-Mar-18 23:23
Sampath57914-Mar-18 23:23 
AnswerRe: Can we include two Message Maps in one class? Pin
Richard MacCutchan14-Mar-18 23:32
mveRichard MacCutchan14-Mar-18 23:32 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57914-Mar-18 23:39
Sampath57914-Mar-18 23:39 
AnswerRe: Can we include two Message Maps in one class? Pin
Jochen Arndt15-Mar-18 0:53
professionalJochen Arndt15-Mar-18 0:53 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57915-Mar-18 1:08
Sampath57915-Mar-18 1:08 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57915-Mar-18 4:19
Sampath57915-Mar-18 4:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.