Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Singleton Design Pattern for Database connection in MFC vc++

Singleton Design Pattern for Database connection in MFC vc++

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasedesignregexarchitecture
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_14575556
    wrote on last edited by
    #1

    How to implement singleton design pattern for database connection in MFC vc++?.

    _ 1 Reply Last reply
    0
    • M Member_14575556

      How to implement singleton design pattern for database connection in MFC vc++?.

      _ Offline
      _ Offline
      _Flaviu
      wrote on last edited by
      #2

      Use this singleton pattern into extention of CDatabase class. A little sample of how to extend this class can be found here: CDatabaseExt - A Simple Way to Connect to a Database and Retrieve Data[^]. You can easily change CDatabaseExt constructor as private and made access method GetInstance() as public.

      M L 2 Replies Last reply
      0
      • _ _Flaviu

        Use this singleton pattern into extention of CDatabase class. A little sample of how to extend this class can be found here: CDatabaseExt - A Simple Way to Connect to a Database and Retrieve Data[^]. You can easily change CDatabaseExt constructor as private and made access method GetInstance() as public.

        M Offline
        M Offline
        Member_14575556
        wrote on last edited by
        #3

        I have created like this. Am I going in the right direction? Thanks in advance.

        #include #pragma once

        // DatabaseSingleton command target

        class DatabaseSingleton : public CDatabase
        {
        static DatabaseSingleton* _instance;
        DatabaseSingleton();
        public:
        //DatabaseSingleton();
        static DatabaseSingleton* GetInstance();
        virtual ~DatabaseSingleton();
        private:
        CString DSN;
        CString UID;
        CString PWD;
        CString ConnectionString;
        };

        // DatabaseSingleton.cpp : implementation file
        //

        #include "stdafx.h"
        #include "Medication_Administration.h"
        #include "DatabaseSingleton.h"

        // DatabaseSingleton

        DatabaseSingleton::DatabaseSingleton():DSN(_T("DemoDB")),UID(_T("root")),PWD(_T("root"))
        {
        ConnectionString.Format(_T("DSN=%s;UID=%s;PWD=%s"), DSN, UID, PWD);
        OpenEx(ConnectionString);
        }

        DatabaseSingleton::~DatabaseSingleton()
        {
        Close();
        }

        // DatabaseSingleton member functions
        DatabaseSingleton* DatabaseSingleton::GetInstance()
        {
        if (_instance == NULL)
        {
        _instance = new DatabaseSingleton();
        }
        return _instance;
        }

        _ 1 Reply Last reply
        0
        • M Member_14575556

          I have created like this. Am I going in the right direction? Thanks in advance.

          #include #pragma once

          // DatabaseSingleton command target

          class DatabaseSingleton : public CDatabase
          {
          static DatabaseSingleton* _instance;
          DatabaseSingleton();
          public:
          //DatabaseSingleton();
          static DatabaseSingleton* GetInstance();
          virtual ~DatabaseSingleton();
          private:
          CString DSN;
          CString UID;
          CString PWD;
          CString ConnectionString;
          };

          // DatabaseSingleton.cpp : implementation file
          //

          #include "stdafx.h"
          #include "Medication_Administration.h"
          #include "DatabaseSingleton.h"

          // DatabaseSingleton

          DatabaseSingleton::DatabaseSingleton():DSN(_T("DemoDB")),UID(_T("root")),PWD(_T("root"))
          {
          ConnectionString.Format(_T("DSN=%s;UID=%s;PWD=%s"), DSN, UID, PWD);
          OpenEx(ConnectionString);
          }

          DatabaseSingleton::~DatabaseSingleton()
          {
          Close();
          }

          // DatabaseSingleton member functions
          DatabaseSingleton* DatabaseSingleton::GetInstance()
          {
          if (_instance == NULL)
          {
          _instance = new DatabaseSingleton();
          }
          return _instance;
          }

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          Yes you do :)

          1 Reply Last reply
          0
          • _ _Flaviu

            Use this singleton pattern into extention of CDatabase class. A little sample of how to extend this class can be found here: CDatabaseExt - A Simple Way to Connect to a Database and Retrieve Data[^]. You can easily change CDatabaseExt constructor as private and made access method GetInstance() as public.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Interested to know how you can answer this, not so simple, issue, but struggle with the ones below, which are basic C.

            _ 1 Reply Last reply
            0
            • L Lost User

              Interested to know how you can answer this, not so simple, issue, but struggle with the ones below, which are basic C.

              _ Offline
              _ Offline
              _Flaviu
              wrote on last edited by
              #6

              I have never learn C, just c++ ... and this code where I am working on is written for Linux, and for other compilers than VS. That is why I got tones of errors, one of them I don't know how to handle them without broke the functionality (I cannot try what I modify, so I am working blind).

              L 1 Reply Last reply
              0
              • _ _Flaviu

                I have never learn C, just c++ ... and this code where I am working on is written for Linux, and for other compilers than VS. That is why I got tones of errors, one of them I don't know how to handle them without broke the functionality (I cannot try what I modify, so I am working blind).

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                OT means "Off topic". I am replying to a message but discussing a different subject.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups