SQL Server Database Connectivity with MFC

Hi everybody,

My question is in relation with the connectivity
with SQL Server Database. I want to connect my SQL
Server database with my MFC Application. I am able
to connect Access database smoothly but what is to
be done for SQL Server.

Please see the followings :

1) My project is a SDI application.
2) I have chosen "Header Files Only" in AppWizard
   Database Support screen.
3) I am not deriving any class from CDatabase. Only
   in OnNewDocument() of Document class I am defining
   an object of CDatabase.
4) I am deriving table specific class from CRecordset
   and in OnNewDocument() associating this with above
   CDatabse Class.
5) Ultimately I am using the CRecordset class in my
   Dialog taking the ptr of CRecordset class.

Considering the above please give me solution with
suitable example.

Who is Participating?
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.

A comment..
Just like you are not deriving from CDatabase, It'd be great if you use the CRecordset directly. If yoy derive from CRecordset and make it table specific then you are making it less flexible( You wont be able to do any kinda "JOIN")..
try the following code..

      CDatabase dbMine;
      if( dbMine.OpenEx(  _T(  "DSN= MYDATA;"  )  )  )
                 CRecordset tblTest( &dbMine );
                 CString csSql = "Select * from MyTable";
         if( !tblTest.Open( CRecordset::dynaset, csSql  ))
                  TRACE0( "Table open failed" );
          return ;


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
majorjohnAuthor Commented:

With ur answer I had to do some extra R&D and achieved the result. Anyway U answered me in right time. Can u leave ur email address. So in any future problem I can contact u directly.

Yup..with pleasure..:)) use..

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.