📄 connectdb.cpp
字号:
// connectDB.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "connectDB.h"
#include "connectDBDlg.h"
#include "SetDBDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CConnectDBApp
BEGIN_MESSAGE_MAP(CConnectDBApp, CWinApp)
//{{AFX_MSG_MAP(CConnectDBApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CConnectDBApp construction
CConnectDBApp::CConnectDBApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CConnectDBApp object
CConnectDBApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CConnectDBApp initialization
BOOL CConnectDBApp::InitInstance()
{
AfxEnableControlContainer();
CString path,userN,Pwd;
if(GetConfig(path,userN,Pwd)==0)//如果数据库目录为空,则选择数据库
{
CSetDBDlg dbDlg;
if(dbDlg.DoModal()==IDOK)
{
path=dbDlg.m_path;
userN=dbDlg.m_user;
Pwd=dbDlg.m_pwd;
SetConfig(path,userN,Pwd);//保存数据源设置
}
}
// 初始化COM,创建ADO连接等操作
AfxOleInit();
m_pConnection.CreateInstance(__uuidof(Connection));
OpenDB(path,userN,Pwd);//打开数据库
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
int CConnectDBApp::ExitInstance()
{
// TODO: Add your specialized code here and/or call the base class
if(m_pConnection->State)
m_pConnection->Close();
m_pConnection= NULL;
return CWinApp::ExitInstance();
}
void CConnectDBApp::AddToRecentFileList(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
::CoUninitialize();
CWinApp::AddToRecentFileList(lpszPathName);
}
int CConnectDBApp::GetConfig(CString &path,CString &user,CString Pwd)
{
char cPath[50];
char cUser[50];
char cPwd[50];
GetPrivateProfileString("CONFIG","PATH","0",cPath,50,"c:/123.ini");
GetPrivateProfileString("CONFIG","USER","0",cUser,50,"c:/123.ini");
GetPrivateProfileString("CONFIG","PWD","0",cPwd,50,"c:/123.ini");
path=cPath;
user=cUser;
Pwd=cPwd;
if(path.IsEmpty()) return 0;
return 1;
}
int CConnectDBApp::SetConfig(CString path, CString user, CString pwd)
{
WritePrivateProfileString("CONFIG","PATH",(LPCTSTR)path,"c:/123.ini");
WritePrivateProfileString("CONFIG","USER",(LPCTSTR)user,"c:/123.ini");
WritePrivateProfileString("CONFIG","PWD",(LPCTSTR)pwd,"c:/123.ini");
return 1;
}
void CConnectDBApp::reSetDB()//重新设置数据库
{
CSetDBDlg dbDlg;
if(dbDlg.DoModal()==IDOK)
{
SetConfig(dbDlg.m_path,dbDlg.m_user,dbDlg.m_pwd);
}
else
exit(0);
OpenDB(dbDlg.m_path,dbDlg.m_user,dbDlg.m_pwd);
}
void CConnectDBApp::OpenDB(CString path, CString user, CString pwd)//打开数据库
{
// 因为它有时会经常出现一些想不到的错误。jingzhou xu
CString openStr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
openStr+=path;
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
try
{
// 打开本地Access库db.mdb
// m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb","","",adModeUnknown);
m_pConnection->Open((_bstr_t)openStr,(_bstr_t)user,(_bstr_t)pwd,adModeUnknown);//打开数据源
}
catch(_com_error e)
{
AfxMessageBox("数据库连接失败,确认数据库db.mdb是否在:"+path);
reSetDB();//重新设置数据库
// return FALSE;
}
CConnectDBDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -