Click here to Skip to main content
13,769,349 members

C / C++ / MFC

 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 7:17
professionalKenneth Haugland19-Mar-18 7:17 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 8:07
professional Randor 19-Mar-18 8:07 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Kenneth Haugland19-Mar-18 10:06
professionalKenneth Haugland19-Mar-18 10:06 
GeneralRe: Compiling MPI with VS2017 Linux Pin
Randor 19-Mar-18 10:14
professional Randor 19-Mar-18 10:14 
Questionproblem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Tarun Jha17-Mar-18 2:30
memberTarun Jha17-Mar-18 2:30 
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
Victor Nijegorodov17-Mar-18 6:52
memberVictor Nijegorodov17-Mar-18 6:52 
AnswerRe: problem encountered while doing mathematical operations on strings by overloaded '+'. Pin
CPallini17-Mar-18 12:58
mvpCPallini17-Mar-18 12:58 
QuestionConnection String Couldn't Connect Pin
Brennan Salibrici15-Mar-18 11:17
memberBrennan Salibrici15-Mar-18 11: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

// 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 12:01
memberGerry Schmitz15-Mar-18 12:01 
AnswerRe: Connection String Couldn't Connect Pin
Victor Nijegorodov15-Mar-18 12:25
memberVictor Nijegorodov15-Mar-18 12:25 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 18:12
memberleon de boer15-Mar-18 18:12 
GeneralRe: Connection String Couldn't Connect Pin
Jochen Arndt15-Mar-18 23:10
mvpJochen Arndt15-Mar-18 23:10 
GeneralRe: Connection String Couldn't Connect Pin
Victor Nijegorodov16-Mar-18 9:41
memberVictor Nijegorodov16-Mar-18 9:41 
GeneralRe: Connection String Couldn't Connect Pin
leon de boer18-Mar-18 5:20
memberleon de boer18-Mar-18 5:20 
AnswerRe: Connection String Couldn't Connect Pin
leon de boer15-Mar-18 17:49
memberleon de boer15-Mar-18 17:49 
GeneralRe: Connection String Couldn't Connect Pin
Brennan Salibrici16-Mar-18 3:45
memberBrennan Salibrici16-Mar-18 3:45 
QuestionRe: Connection String Couldn't Connect Pin
David Crow16-Mar-18 17:43
memberDavid Crow16-Mar-18 17:43 
Questionproblem in operator overloading of >> & << ? Pin
Tarun Jha15-Mar-18 8:47
memberTarun Jha15-Mar-18 8:47 
AnswerRe: problem in operator overloading of >> & << ? Pin
CPallini15-Mar-18 11:47
mvpCPallini15-Mar-18 11:47 
QuestionCan we include two Message Maps in one class? Pin
Sampath57915-Mar-18 0:23
memberSampath57915-Mar-18 0:23 
AnswerRe: Can we include two Message Maps in one class? Pin
Richard MacCutchan15-Mar-18 0:32
protectorRichard MacCutchan15-Mar-18 0:32 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57915-Mar-18 0:39
memberSampath57915-Mar-18 0:39 
AnswerRe: Can we include two Message Maps in one class? Pin
Jochen Arndt15-Mar-18 1:53
mvpJochen Arndt15-Mar-18 1:53 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57915-Mar-18 2:08
memberSampath57915-Mar-18 2:08 
GeneralRe: Can we include two Message Maps in one class? Pin
Sampath57915-Mar-18 5:19
memberSampath57915-Mar-18 5: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.


Advertise | Privacy | Cookies | Terms of Service
Web06-2016 | 2.8.181117.1 | Last Updated 18 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid