// DlgDBConnect.cpp : 实现文件
//

#include "stdafx.h"
#include "Draw.h"
#include "DlgDBConnect.h"


// CDlgDBConnect 对话框

IMPLEMENT_DYNAMIC(CDlgDBConnect, CDialog)

CDlgDBConnect::CDlgDBConnect(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgDBConnect::IDD, pParent)
	, m_strPath(g_strDBPath)
	, m_strTips(_T("-->数据库未连接"))
	, m_pConnection(NULL)
{

}

CDlgDBConnect::~CDlgDBConnect()
{
}

void CDlgDBConnect::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_DBPATH, m_strPath);
	DDX_Text(pDX, IDC_STATIC_TIPS, m_strTips);
}


BEGIN_MESSAGE_MAP(CDlgDBConnect, CDialog)
	ON_BN_CLICKED(IDC_BTN_BROWSE, &CDlgDBConnect::OnBnClickedBtnBrowse)
	ON_BN_CLICKED(IDC_BTN_CONNECT, &CDlgDBConnect::OnBnClickedBtnConnect)
	ON_BN_CLICKED(IDC_BTN_DISCONNECT, &CDlgDBConnect::OnBnClickedBtnDisconnect)
END_MESSAGE_MAP()


// CDlgDBConnect 消息处理程序

void CDlgDBConnect::OnBnClickedBtnBrowse()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog fileDlg(TRUE, NULL, NULL);

	fileDlg.m_ofn.lpstrFilter =	_T("mdb Files (*.mdb)\0*.mdb\0All Files (*.*)\0*.*\0");
	fileDlg.m_ofn.lpstrTitle = _T("请选择ACESS数据库文件");
	fileDlg.m_ofn.Flags |= OFN_FILEMUSTEXIST;

	if (IDOK == fileDlg.DoModal())
	{
		if (fileDlg.GetFileExt()!= _T("mdb") && fileDlg.GetFileExt()!= _T("MDB"))
		{
			AfxMessageBox(_T("选择的数据库文件格式不对!"));
			return ;
		}
		else
		{
			m_strPath = fileDlg.GetPathName();
			g_strDBPath = m_strPath;
			UpdateData(FALSE);
		}
	}
}

void CDlgDBConnect::OnBnClickedBtnConnect()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_strPath.IsEmpty())
	{
		AfxMessageBox(_T("请查找目标Access数据库！"));
		return;
	}
	if (m_pConnection == NULL)
	{
		m_pConnection.CreateInstance(__uuidof(Connection));
	}
	HRESULT hr;
	try
	{
		CString strConnect;
		strConnect  = _T("Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=");
		strConnect += m_strPath;
		hr = m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
	}
	catch (_com_error e)
	{
		AfxMessageBox(_T("数据库连接错误！"));
	}

	if (hr == S_OK)
	{
		m_strTips = _T("-->数据库连接成功！");
		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);
	}
	else
	{
		m_strTips = _T("-->数据库连接错误！");
		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);
	}
	UpdateData(FALSE);
}

void CDlgDBConnect::OnBnClickedBtnDisconnect()
{
	// TODO: 在此添加控件通知处理程序代码
	if (m_pConnection != NULL && m_pConnection->GetState() != adStateClosed)
	{
		m_pConnection->Close();
		m_strTips = _T("-->数据库已关闭！");
		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(FALSE);
		UpdateData(FALSE);
	}
}

BOOL CDlgDBConnect::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	if (m_pConnection != NULL && m_pConnection->GetState() != adStateClosed)
	{
		m_strTips = _T("-->数据库已连接！");
		GetDlgItem(IDC_BTN_CONNECT)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_DISCONNECT)->EnableWindow(TRUE);
		UpdateData(FALSE);
	}
    
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
