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.
GegglesAsked:
Who is Participating?
 
evilrixSenior Software Engineer (Avast)Commented:
Have you considered using ODBC to do this?
http://support.microsoft.com/kb/110093
0
 
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.
0
 
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.
http://msdn.microsoft.com/en-us/library/ms722784(VS.85).aspx
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Gene_CypCommented:
Hey Geggles, here you will find instructions on how to set this up:
http://www.connectionstrings.com/sql-server-2005-ce 
0
 
Gene_CypCommented:
Ignore the first link I gave as it only has C#
Check this one out instead:
http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnection.connectionstring(VS.71).aspx 
0
 
itsmeandnobodyelseCommented:
>>>> 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
{
public:
   // 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())
      return;
 

0
 
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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Although a different solution was used valid help was provided for the original question asked.
0
 
GegglesAuthor Commented:
Ended up using SQLExpress as the backend DB engine. Apologies for not assigning the points.
0
 
Gene_CypCommented:
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..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.