Sql – Best way to connect To Sql Server with MFC


Soooo, I am starting this new job soon where most of the code is legacy MFC. The end goal is to convert/rewrite to C#.

I'm a little rusty with MFC so writing some code so I can try and spark my memory.
Anyway, was wondering the best way to connect to a SS2005 database with MFC. Back in the day CDatabase via ODBC was the way to go, has that changed in the last few years? I see you can use ATL and ADO, but looked a little, well verbose, for my liking?

Best Solution

The ATL consumer templates for OleDb are an option - start here. The ClassWizard is still there to assist you so the verbosity isn't too much of a hurdle at first. Very soon you will need to hand-code though. There is a lot of careful twiddling, for example ensuring that your command string has exactly the right number of ? marks corresponding to the COLUMN_ENTRYs for an accessor. Then you'll probably have a million CopyToCommandFromObject and CopyToObjectFromCommand methods.

This app doesn't have any data access yet and you're going to be adding it? If so, I would seriously consider implementing a modern DAL (ADO.Net, linq if you're lucky enough to be on 2008) in a separate managed assembly and doing some interop.