[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

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.

Thanks,
0
majorjohn
Asked:
majorjohn
  • 2
1 Solution
 
anoopsCommented:
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 ;
                };
              }

0
 
majorjohnAuthor Commented:
anoops

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.

Thanks
0
 
anoopsCommented:
Yup..with pleasure..:)) use..

anoops@hiso.honeywell.com
or
anoop_sreedhar@hotmail.com
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now