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

📄 daotdlg1.cpp

📁 VC6数据库开发指南
💻 CPP
字号:
#include "stdafx.h"
#include "resource.h"
#include "DAOTDlg1.h"

#include "database.h"
#include "tabledef.h"

#include "AddDbDlg1.h"
#include "listctrl.h"
#include "addixdlg1.h"
#include "AddTbDlg1.h"

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

#define SUCCESS 1
#define FAILURE 0
#define FATAL -1

/////////////////////////////////////////////////////////////////////////////
// CDAOTableDlg dialog

CDAOTableDlg::CDAOTableDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDAOTableDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDAOTableDlg)
	m_strDatabaseName = _T("");
	m_strTableName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	// initialize the database pointer
	m_pDatabase = NULL;
}

void CDAOTableDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDAOTableDlg)
	DDX_Control(pDX, IDC_COMBO_TABLE_NAME, m_TableNameComboControl);
	DDX_Text(pDX, IDC_EDIT_DATABASE_NAME, m_strDatabaseName);
	DDX_Text(pDX, IDC_COMBO_TABLE_NAME, m_strTableName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDAOTableDlg, CDialog)
	//{{AFX_MSG_MAP(CDAOTableDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
	ON_BN_CLICKED(IDC_BUTTON_TABLE_FIELDS, OnButtonTableFields)
	ON_BN_CLICKED(IDC_BUTTON_TABLE_INDEXES, OnButtonTableIndexes)
	ON_CBN_DROPDOWN(IDC_COMBO_TABLE_NAME, OnDropdownComboTableName)
	ON_BN_CLICKED(IDC_BUTTON_DELETE_TABLE, OnButtonDeleteTable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CDAOTableDlg message handlers

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


	CMenu* pSysMenu = GetSystemMenu(FALSE);
	CString strAboutMenu;
	if (!strAboutMenu.IsEmpty())
	{
		pSysMenu->AppendMenu(MF_SEPARATOR);
	}

	SetIcon(m_hIcon, TRUE);         
	SetIcon(m_hIcon, FALSE);        

	// 设置输入焦点为数据库名编辑框
	CEdit *pEdit;
	pEdit = (CEdit *)GetDlgItem(IDC_EDIT_DATABASE_NAME);
	if (pEdit != NULL)
		pEdit->SetFocus();

	return FALSE;  // return TRUE  unless you set the focus to a control
}

void CDAOTableDlg::OnSysCommand(UINT nID, LPARAM lParam)
{

}

void CDAOTableDlg::OnDestroy()
{
	WinHelp(0L, HELP_QUIT);
	CDialog::OnDestroy();
}

void CDAOTableDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CDAOTableDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


//用户按下“连接”按钮能够:
//a) 连接到指定的数据库
//b) 创建新数据库
//c) 浏览数据库文件
void CDAOTableDlg::OnButtonConnect()
{
	// 关闭任何打开的数据库
	closeDatabase(&m_pDatabase);

	UpdateData(TRUE);

	// 如果指定了数据库名则直接打开
	if (m_strDatabaseName != _T(""))
	{
		int retCode = openDatabase(&m_pDatabase, m_strDatabaseName, FALSE);
		if (SUCCESS == retCode)
		{
			//如果数据库被打开则允许其他控件
			enableControlsForOpenDatabase(TRUE);
		}
		else if (FAILURE == retCode) // 如果用户指定了一个新数据库名
		{
			// 创建新数据库
			createNewDatabase();
		}
		else 
			enableControlsForOpenDatabase(FALSE);
	}
	else
	{
		// 允许用户选则数据库
		CFileDialog dlg (TRUE, _T("mdb"), NULL, OFN_HIDEREADONLY,
						 _T("Access Database Files (*.mdb)|*.mdb||"),
						 this);

		if (IDOK == dlg.DoModal())
		{
			m_strDatabaseName = dlg.GetPathName();

			UpdateData(FALSE);

			int retCode = openDatabase(&m_pDatabase, m_strDatabaseName);
			if (SUCCESS == retCode)
			{
				enableControlsForOpenDatabase(TRUE);
			}
			else  
			{
				enableControlsForOpenDatabase(FALSE);
			}
		}
	}
}


void CDAOTableDlg::createNewDatabase()
{
	if (IDYES == AfxMessageBox(_T("数据库不存在,是否希望新建一个数据库?"), MB_YESNO))
	{
		CAddDatabaseDlg dlg(&m_pDatabase, m_strDatabaseName);
		dlg.DoModal();

		if (m_pDatabase != NULL)
		{
			enableControlsForOpenDatabase(TRUE);
		}
		else 
		{
			enableControlsForOpenDatabase(FALSE);
		}
	}
}

void CDAOTableDlg::enableControlsForOpenDatabase(BOOL bEnable /*= TRUE*/)
{
	m_TableNameComboControl.EnableWindow(bEnable);

	CButton *pButton;
	pButton = (CButton *) GetDlgItem(IDC_BUTTON_TABLE_FIELDS);
	if (pButton != NULL)
		pButton->EnableWindow(bEnable);
	pButton = (CButton *) GetDlgItem(IDC_BUTTON_TABLE_INDEXES);
	if (pButton != NULL)
		pButton->EnableWindow(bEnable);
	pButton = (CButton *) GetDlgItem(IDC_BUTTON_DELETE_TABLE);
	if (pButton != NULL)
		pButton->EnableWindow(bEnable);
	pButton = (CButton *) GetDlgItem(IDC_BUTTON_QUERY_DEFINITION);
	if (pButton != NULL)
		pButton->EnableWindow(bEnable);
}

void CDAOTableDlg::OnCancel()
{
	closeDatabase(&m_pDatabase);

	CDialog::OnCancel();
}

void CDAOTableDlg::OnOK()
{
	closeDatabase(&m_pDatabase);

	CDialog::OnOK();
}

void CDAOTableDlg::OnButtonTableFields()
{
	UpdateData(TRUE);

	CDlgAddTable dlg(m_pDatabase, m_strTableName);
	dlg.DoModal();
}

void CDAOTableDlg::OnButtonTableIndexes()
{
	UpdateData(TRUE);

	if (IsExistentTable(m_pDatabase, m_strTableName))
	{
		CAddIndexDlg dlg(m_pDatabase, m_strTableName);
		dlg.DoModal();
	}
	else
		AfxMessageBox(_T("You can only define indexes on existing tables."));
}


void CDAOTableDlg::OnDropdownComboTableName()
{
	int index = 0;
	CDaoTableDefInfo TableInfo;

	m_TableNameComboControl.ResetContent();

	while (getTableInfo(m_pDatabase, &TableInfo, index, FALSE))
	{
		if (!(TableInfo.m_lAttributes & dbSystemObject)
			&& !(TableInfo.m_lAttributes & dbHiddenObject))
			m_TableNameComboControl.AddString(TableInfo.m_strName);

		index += 1;
	}
}


void CDAOTableDlg::OnButtonDeleteTable()
{
	UpdateData(TRUE);

	if (IsExistentTable(m_pDatabase, m_strTableName))
	{
		if (IDYES == AfxMessageBox(_T("Delete current table?  This will delete all fields and indexes for this table."), MB_YESNO))
		{
			deleteTable(m_pDatabase, m_strTableName);

			m_strTableName = _T("");

			UpdateData(FALSE);
		}
	}
}

⌨️ 快捷键说明

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