Replace the connection from MS Access to SQL Server

Hi - I have an old C++ (MFC, Win32) application running behind ASP.NET to do some calculation running. I converted ASP.NET part to WebAPI and now all the data is placed in SQL Server. So instead of passing the path of Access file, now I want to make C++ to connect with SQL Server (That's the only change desired in C++). It is currently using DAO but because of limited time - I won't be able to upgrade much. The original connection and running the queries appear like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int C_Data4Glazing::GetGlazingIDfromNFRCid( UINT idNRFC )
{
		CString			laa;
		CString			locst_DataConnection;
		CString			locst_SQLstatement;
		CDaoDatabase	lDaoDatabase;
		CDaoQueryDef	lQuery(&lDaoDatabase);
		CDaoRecordset	lRecSet(&lDaoDatabase);
		int				ii ;
	// Get the database file location typically "c:\WebFPlus\w5.mdb"
	laa = C_Data4Glazing::GetDefaultLibName();
	locst_DataConnection.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s"), laa );
	// Open the database
	lDaoDatabase.Open( laa );

	// Create a non-permanent query object (not stored in database file)
	locst_SQLstatement.Empty();
	locst_SQLstatement.Format(_T("SELECT test FROM test  WHERE (((test.NFRC_ID)=%d));"), idNRFC ) ;
	lQuery.Create(_T(""), locst_SQLstatement ); // create a non-permanent query object in the database

	// Use the Query object to create the record set...
	lRecSet.Open(&lQuery, dbOpenSnapshot, dbReadOnly );

	// Only a single record should be returned, by definition is should be Glass.ID should be what we are looking for...
	int recordcount = lRecSet.GetRecordCount();
	lRecSet.MoveFirst();
	ii = lRecSet.GetFieldValue( "test" ).intVal;

	// Clean up!
	lRecSet.Close();
	lQuery.Close();
	lDaoDatabase.Close();
	return( ii );
} 

I can connect with SQL server through connection string using ADO following (http://stackoverflow.com/questions/5795606/sql-connectionstring-debug-step-by-step)
in a standalone C++ program but when I tried to import msado in the main application, as in example - it did not work- got error related to Window library from afx.
I tried this link http://www.tidytutorials.com/2009/08/connecting-to-sql-server-using-c-odbc.html but not moving forward to SQLDriverConnect and getting the case SQL_ERROR. I tried different combination of connection strings. I have DSN entry and can verify odbc in registry. A newbie to C++ and getting confused with all the web suggestions - some are suggesting odbc is obsolete too but some are defending it. Also I don't want the structure whole lot . I have these SQLs running snippets at many location and don't want to create unnecessary classes/ libraries if I don't have too. Thanks in advance!
Last edited on
Topic archived. No new replies allowed.