Singleton Design Pattern for Database connection in MFC vc++
-
How to implement singleton design pattern for database connection in MFC vc++?.
-
How to implement singleton design pattern for database connection in MFC vc++?.
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.
-
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.
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;
} -
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;
} -
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.
-
Interested to know how you can answer this, not so simple, issue, but struggle with the ones below, which are basic C.
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).
-
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).