⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setodbc.cpp

📁 深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹
💻 CPP
字号:
// SetODBC.cpp : implementation file
//

#include "stdafx.h"
#include "Ex120700.h"
#include "SetODBC.h"
#include <afxdb.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSetODBC dialog


CSetODBC::CSetODBC(CWnd* pParent /*=NULL*/)
	: CDialog(CSetODBC::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSetODBC)
	//}}AFX_DATA_INIT
	m_strSectionName = "数据库设置" ;
	ReadFromFile();
}

void CSetODBC::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSetODBC)
	DDX_Text(pDX, IDC_DSN, m_strDSN);
	DDX_Text(pDX, IDC_PWD, m_strPWD);
	DDX_Text(pDX, IDC_UID, m_strUID);
	DDX_CBString(pDX, IDC_DBType, m_strDBType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSetODBC, CDialog)
	//{{AFX_MSG_MAP(CSetODBC)
	ON_BN_CLICKED(IDC_BTN_ODBC, OnBtnOdbc)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSetODBC message handlers
void CSetODBC::SetFileName(CString strFileName)
{
	m_strFileName = strFileName ;
}

void CSetODBC::SetSectionName(CString strSectionName)
{
	m_strSectionName = strSectionName ;
}

CString CSetODBC::GetDBType()
{
	return m_strDBType ;
}

bool CSetODBC::ReadFromFile(CString strFileName)
{
	//密码要解密
	if(strFileName.IsEmpty())
		strFileName = m_strFileName ;

	if(strFileName.IsEmpty())//读注册表或INI
	{
		CWinApp * pApp = AfxGetApp() ;
		ASSERT(NULL != pApp) ;
		m_strDSN	= pApp->GetProfileString(m_strSectionName,"DSN");
		m_strUID	= pApp->GetProfileString(m_strSectionName,"UID");
		m_strPWD	= pApp->GetProfileString(m_strSectionName,"PWD");
		m_strDBType	= pApp->GetProfileString(m_strSectionName,"DBType");
	}
	else
	{
		char temp[1000] ;
		GetPrivateProfileString(m_strSectionName,"DSN","",temp,999,strFileName);
		m_strDSN	= temp ;
		GetPrivateProfileString(m_strSectionName,"UID","",temp,999,strFileName);
		m_strUID	= temp ;
		GetPrivateProfileString(m_strSectionName,"PWD","",temp,999,strFileName);
		m_strPWD	= temp ;
		GetPrivateProfileString(m_strSectionName,"DBType","",temp,999,strFileName);
		m_strDBType	= temp ;
	}

	return true ;
}

bool CSetODBC::WriteToFile(CString strFileName)
{
	//密码要加密
	if(strFileName.IsEmpty())
		strFileName = m_strFileName ;

	if(strFileName.IsEmpty())//读注册表或INI
	{
		CWinApp * pApp = AfxGetApp() ;
		ASSERT(NULL != pApp) ;
		pApp->WriteProfileString(m_strSectionName,"DSN",m_strDSN);
		pApp->WriteProfileString(m_strSectionName,"UID",m_strUID);
		pApp->WriteProfileString(m_strSectionName,"PWD",m_strPWD);
		pApp->WriteProfileString(m_strSectionName,"DBType",m_strDBType);
	}
	else
	{
		WritePrivateProfileString(m_strSectionName,"DSN",m_strDSN,strFileName);
		WritePrivateProfileString(m_strSectionName,"UID",m_strUID,strFileName);
		WritePrivateProfileString(m_strSectionName,"PWD",m_strPWD,strFileName);
		WritePrivateProfileString(m_strSectionName,"DBType",m_strDBType,strFileName);
	}

	return true ;
}



bool CSetODBC::IsLegalODBC()
{
	if(m_strDSN.IsEmpty())
		return false ;

	CDatabase db ;
	try
	{
		CString strConnect ;
		strConnect.Format("DSN=%s;UID=%s;PWD=%s",m_strDSN,m_strUID,m_strPWD);
		if(!db.OpenEx(strConnect,CDatabase::noOdbcDialog))
			return false ;
		db.Close();
	}
	catch(...)
	{
		return false ;
	}
	return true ;
}

CString CSetODBC::GetConnectStr(bool bReSet)
{
	if(bReSet || !IsLegalODBC())
	{
		do
		{	
			if(IDOK != DoModal())
				return "" ;//用户点击了取消
		}while(!IsLegalODBC());
	}

	CString strConnect ;
	strConnect.Format("DSN=%s;UID=%s;PWD=%s",m_strDSN,m_strUID,m_strPWD);
	return strConnect ;
}


DWORD CSetODBC::GetWriteDBStyle()
{
	if("Access" == m_strDBType )//Access
		return CRecordset::snapshot ;
	else//其它类型,暂未完成
		return CRecordset::snapshot ;
}

void CSetODBC::OnBtnOdbc() 
{
	CDatabase db ;
	CString strConnect ;
	strConnect.Format("DSN=%s;UID=%s;PWD=%s",m_strDSN,m_strUID,m_strPWD);
	if(!db.OpenEx(strConnect,CDatabase::forceOdbcDialog))
		return  ;
	CString strFullConnect = db.GetConnect();
	db.Close();	

	if(-1 != strFullConnect.Find("DBQ"))//Access数据库
	{
		int nBeginPos	= strFullConnect.Find("DSN=") + strlen("DSN=");
		int nEndPos		= strFullConnect.Find(";",nBeginPos);
		m_strDSN	= strFullConnect.Mid(nBeginPos,nEndPos - nBeginPos );
		m_strUID.Empty();
		m_strPWD.Empty() ;
		m_strDBType = "Access" ;
	}
	else
	{//其它类型暂未完成
	}

	UpdateData(false);
}

void CSetODBC::OnOK() 
{
	UpdateData();
	WriteToFile();
	CDialog::OnOK();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -