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

📄 dbmodify.cpp

📁 VC6数据库开发指南
💻 CPP
字号:
// DBModify.cpp : implementation file
//

#include "stdafx.h"
#include "manager.h"
#include "DBModify.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDBModify dialog


CDBModify::CDBModify(CWnd* pParent /*=NULL*/)
	: CDialog(CDBModify::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDBModify)
	m_strDatabaseName = _T("");
	m_strTableName = _T("");
	//}}AFX_DATA_INIT
}


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


BEGIN_MESSAGE_MAP(CDBModify, CDialog)
	//{{AFX_MSG_MAP(CDBModify)
	ON_BN_CLICKED(IDC_FIELD, OnField)
	ON_BN_CLICKED(IDC_INDEX, OnIndex)
	ON_BN_CLICKED(IDC_DEL, OnDel)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_CBN_DROPDOWN(IDC_COMBO_TABLE_NAME, OnDropdownComboTableName)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDBModify message handlers
// user selected the "Fields" button to add or view the
// fields that make up the table definition for the specified
// table
void CDBModify::OnField() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	// table specified
	CDlgAddTable dlg(m_pDatabase, m_strTableName);
	dlg.DoModal();
}

void CDBModify::OnIndex() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	// can only continue if this is an existing table
	// new tables must have fields added first
	if (IsExistentTable(m_pDatabase, m_strTableName))
	{
		// existing table specified, so allow interaction
		CAddIndexDlg dlg(m_pDatabase, m_strTableName);
		dlg.DoModal();
	}
	else
		AfxMessageBox(_T("You can only define indexes on existing tables."));
}

void CDBModify::OnDel() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	// can only delete existing tables
	if (IsExistentTable(m_pDatabase, m_strTableName))
	{
		// is user sure?
		if (IDYES == AfxMessageBox(_T("Delete current table?  This will delete all fields and indexes for this table."), MB_YESNO))
		{
			// delete the table
			deleteTable(m_pDatabase, m_strTableName);

			// clear the table name
			m_strTableName = _T("");

			// update the dialog controls to erase deleted field
			UpdateData(FALSE);
		}
	}
}

// user selected the "Connect" button to
// a) connect to a specified database file
// b) create a new database file
// c) browse for a database file to open
void CDBModify::OnConnect() 
{
	// TODO: Add your control notification handler code here
	// close any open database
	closeDatabase(&m_pDatabase);

	// get the database name (if any) from the dialog control
	UpdateData(TRUE);

	// if user specified a name, then open it directly,
	if (m_strDatabaseName != _T(""))
	{
		m_pDatabase = new CDaoDatabase;

	    // now open the database object with error checking
	    m_pDatabase->Open(m_strDatabaseName);
	
		// once a database is opened, other controls are enabled
		enableControlsForOpenDatabase(TRUE);
	}
	else
	{
		// allow user to select database to open
		CFileDialog dlg (TRUE, _T("mdb"), NULL, OFN_HIDEREADONLY,
						 _T("Access Database文件 (*.mdb)|*.mdb||"),
						 this);

		// if user selected a file then open it as a database
		if (IDOK == dlg.DoModal())
		{
			// get full path to database file
			m_strDatabaseName = dlg.GetPathName();

			// display the name of the database on the dialog
			UpdateData(FALSE);

			// now open the database if possible
			int retCode = openDatabase(&m_pDatabase, m_strDatabaseName);
			if (SUCCESS == retCode)
			{
				// once a database is opened, other controls are enabled
				enableControlsForOpenDatabase(TRUE);
			}
			else    // can't interact with an unopened database
			{
				// if database not open, other controls are disabled
				enableControlsForOpenDatabase(FALSE);
			}
		}
}

void CDBModify::OnDropdownComboTableName() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);

	// can only delete existing tables
	if (IsExistentTable(m_pDatabase, m_strTableName))
	{
		// is user sure?
		if (IDYES == AfxMessageBox(_T("Delete current table?  This will delete all fields and indexes for this table."), MB_YESNO))
		{
			// delete the table
			deleteTable(m_pDatabase, m_strTableName);

			// clear the table name
			m_strTableName = _T("");

			// update the dialog controls to erase deleted field
			UpdateData(FALSE);
		}
	}
}

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

	CDialog::OnOK();
}

// can only interact with tables and queries if you have an open database
// so enable/disable table and query controls conditionally
void CDBModify::EnableControls(BOOL IsEnable)
{
	// handle controls subclassed via DDX_Control
	m_Tablelist.EnableWindow(IsEnable);

	// must enable/disable buttons directly
	CButton *button;
	button = (CButton *) GetDlgItem(IDC_FIELD);
	if (button != NULL)
		button->EnableWindow(IsEnable);
	button = (CButton *) GetDlgItem(IDC_INDEX);
	if (button != NULL)
		button->EnableWindow(IsEnable);
	button = (CButton *) GetDlgItem(IDC_DEL);
	if (button != NULL)
		button->EnableWindow(IsEnable);
}





⌨️ 快捷键说明

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