Using SQL Compact Edition 3.5 in MFC

I would like to use SQL Compact Edition 3.5 in an MFC application. I am using Visual Studio 2010, but I have no clue, and I am failing to find any examples of how to do this. It looks as tho it can be done.

Database is C:\Temp\Sysconfig.sdf, created using Visual Studio 2010. It has one Table named "Radios" and Two Columns named "Radio ID" and "RSE ID".

I would like to connect to the database both to read and write to it.

If someone could post me a small sample of how to access this in MFC C++. I would be truly grateful.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Have you considered using ODBC to do this?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GegglesAuthor Commented:
There appears to be no ODBC driver for SQL Server CE. It appears I need to use OLEDB, but I am unsure how to do this.
evilrixSenior Software Engineer (Avast)Commented:
>>  I need to use OLEDB

Ah, ok I didn't suggest this cos ODBC is normally the easiest way to go :)

See if this helps.
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Hey Geggles, here you will find instructions on how to set this up: 
Ignore the first link I gave as it only has C#
Check this one out instead: 
>>>> a small sample of how to access this in MFC C++.

MFC surely can use OLEDB as well but MFC includes database and recordset class which would access the SQL Server DBMS via ODBC.

---------- sample code from MSDN --------------
// This example illustrates using CDatabase
// in a CDocument-derived class.

class CMyDocument : public CDocument
   // Declare a CDatabase embedded in the document
   CDatabase m_dbCust;
   // ...

// ...

// Initialize when needed
CDatabase* CMyDocument::GetDatabase( )
   // Connect the object to a data source
   if( !m_dbCust.IsOpen( ) && 
       !m_dbCust.OpenEx( "DSN=SQLServer_Source;UID=SA;PWD=abc123" ) )
      return NULL;

   return &m_dbCust;

The DSN 'SQLServer_Source' could be added to the registered ODBC data sources using odbcad32.exe (or the ODBC applet in system settings). Here, with the installation of the SQL Server (client) you should have got an ODBC driver that was included in the list.  

You could use the connection opened like above by feeding the CDatabase to a own recordset class derived from CRecordset. When you create the class using class wizard you can let it bind automatically to your table above (though I think you must remove the space from the column names).

  MyRecordset rs(&m_dbCust);
   int retopen = rs.Open(CRecordset::dynaset, "Radios");
   int retmove = rs.MoveFirst();

Now the rs contains the first record of your table.

   CString radID = rs.m_radioId;
   CString rseID = rs.m_rseId;
   // next record
   if ((retmove = rs.MoveNext()) == isEOF())

AndyAinscowFreelance programmer / ConsultantCommented:
>>It has one Table named "Radios" and Two Columns  named "Radio ID" and "RSE ID".

If that is the total extent of what is to be stored then a database (any form) is a bit of overkill, consider using a text file.
evilrixSenior Software Engineer (Avast)Commented:
Although a different solution was used valid help was provided for the original question asked.
GegglesAuthor Commented:
Ended up using SQLExpress as the backend DB engine. Apologies for not assigning the points.
From multiple sources...
Anyhow, I see this happening all too often. EE is an excellent site, but perhaps more awareness for new users on how to close their questions is needed..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.