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

📄 testdlg.cpp

📁 随着计算机信息技术的飞速发展
💻 CPP
字号:
// testDlg.cpp : implementation file
//

#include "stdafx.h"
#include "test.h"
#include "testDlg.h"

//    添加的类头文件
#include "ADODataBase.h"     //  数据库操作类
#include "TableList.h"       //  列表控件操作
#include "NewTableDlg.h"     //  新建表单对话框
#include "AddRecordDlg.h"    //  添加记录
#include "Wcedb.h" 

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

/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog

CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTestDlg)
	DDX_Control(pDX, IDC_RecordLstCtrl, m_cRecordLstCtrl);
	DDX_Control(pDX, IDC_TableLst, m_cTableLst);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
	//{{AFX_MSG_MAP(CTestDlg)
	ON_BN_CLICKED(IDC_OpenDataBtm, OnOpenDataBtm)
	ON_BN_CLICKED(IDC_DelRecordBtm, OnDelRecord)
	ON_BN_CLICKED(IDC_DelTableBtm, OnDelTable)
	ON_BN_CLICKED(IDC_NewDBataBaseBtm, OnNewDBataBase)
	ON_BN_CLICKED(IDC_NewTableBtm, OnNewTable)
	ON_LBN_SELCHANGE(IDC_TableLst, OnSelchangeTable)
	ON_BN_CLICKED(IDC_NewRecordBtm, OnNewRecord)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers

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

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen
   CCeDBDatabase db;
 /*  if(!CCeDBDatabase::Exists(_T("My Database"))) //检查数据库是否存在
   {
	   db.Create(_T("My Database")); //如果数据库不存在,就创建它
   }
*/	TableLst.m_pListCtrl=&m_cRecordLstCtrl;   // 控件操作指向
	sDBName=_T("");
	return TRUE;  // return TRUE  unless you set the focus to a control
}


//打开数据库按钮
void CTestDlg::OnOpenDataBtm() 
{
   //用打开文件对话框来获取我们要连接的
	 //数据库文件名以及数据库所在的路径
	CString str;
    CFileDialog dlg(TRUE,_T("cdb"),_T("Pxg.cdb"),OFN_HIDEREADONLY |
		 OFN_OVERWRITEPROMPT,_T("PocketAccess文件(*.cdb)|*.cdb||"),NULL);
	 if(dlg.DoModal() == IDCANCEL)
	 {
	   return;
	 }
	 else 
		 sDBName=dlg.GetPathName();
	 delete dlg;
//	 AfxMessageBox(sDBName);
	CADODataBase DataBase;                  // 创建ADO类对象
	DataBase.GetTableLst(sDBName);          // 获得数据库表名称
	nTableNum=DataBase.m_values.GetSize();  //  表单数目
	if(nTableNum==0)    // 数据库为空
		return;
	m_cTableLst.ResetContent();
	for(int i=0; i<nTableNum; i++)       //  获得表单名称并显示出来
	{
		m_cTableLst.AddString(DataBase.m_values.GetAt(i));
	}
	m_cTableLst.SetCurSel(0);
	DataBase.CloseTable();
	OnSelchangeTable();                   //  刷新记录显示列表框
}

//删除记录
void CTestDlg::OnDelRecord() 
{
	if(sDBName==_T(""))          //  没有关联数据库
		return;
	if(sTableName==_T(""))       //  没有关联数据表
		return;
	if(TableLst.m_nRows==0)  //没有记录
		return;
	
	int nSelect;   //选中的记录
	nSelect=m_cRecordLstCtrl.GetSelectionMark();
	if(nSelect==-1)   //没有选择
		return; 

	CADODataBase Table;                    // 创建ADO类对象
	if(!Table.OpenTable(sDBName,sTableName))   //  打开选择的表单
	{
		AfxMessageBox(_T("打开数据表不成功!"));
	}
	if(!Table.DelRecord(nSelect))
	{
		AfxMessageBox(_T("删除记录不成功!"));
	}
	m_cRecordLstCtrl.DeleteItem(nSelect);
}

//删除表单
void CTestDlg::OnDelTable() 
{
	if(sDBName==_T(""))          //  没有关联数据库
		return;

	if(sTableName==_T(""))       //  没有关联数据表
		return;

	CADODataBase Table;
	Table.DeleteTable(sTableName,sDBName);   //  删除数据表
	 
	int num=m_cTableLst.GetCurSel();         //  当前表的序列号
	m_cTableLst.DeleteString(num);           //  在空间中删除表名称

	nTableNum--;
	for(int i=0; i<TableLst.m_nCols; i++)
	{
		m_cRecordLstCtrl.DeleteColumn(0);
	}

	if(nTableNum==0)    // 数据库为空
		return;

	m_cTableLst.GetText(0,sTableName);       //  获得新的表名
	m_cTableLst.SetCurSel(0);                //  设置选中状态
	OnSelchangeTable();                      //  刷新纪录显示
}

//新建数据库
void CTestDlg::OnNewDBataBase() 
{
    CFileDialog dlg(FALSE,_T("cdb"),_T("Test.cdb"),OFN_HIDEREADONLY |
		 OFN_OVERWRITEPROMPT,_T("PocketAccess文件(*.cdb)|*.cdb||"),NULL);
	if(dlg.DoModal() == IDCANCEL)
	 {
	   return;
	 }
	 else 
		 sDBName=dlg.GetPathName();
	 delete dlg;
	CADODataBase DataBase;
	DataBase.CreateDatabase(_T("\\test\\仪器检验数据库"));
}

//新建表单
void CTestDlg::OnNewTable() 
{
	if(sDBName==_T(""))          //  没有关联数据库
		return;
	CNewTableDlg dlg;            //  新建表单对话框
	if(dlg.DoModal()==IDCANCEL)
		return;
	CString str=dlg.m_sTableNameEdt;   //  输入的表单名
	delete dlg;

	//创建表单
	CADODataBase Table;
	CString sField;             //  字段字符串
	Table.DeleteTable(str,sDBName);
	//  示例中的字段任意输入几个真正操作时根据需要写入。
	sField =_T(" ID1    integer");
	sField+=_T(",ID2    uint");
	sField+=_T(",ID3    SmallInt");
	sField+=_T(",ID4    Usmallint");
	sField+=_T(",ID5    Text");
	sField+=_T(",ID6    VarChar(6)");
	sField+=_T(",ID7    float");
	sField+=_T(",ID8    VarBinary(6)");
	sField+=_T(",ID9    Long Varbinary");
	sField+=_T(",ID10   DateTime");
	sField+=_T(",ID11   Bit");
	if(!Table.CreateTable(str,sField,sDBName))  //  创建数据表单
		return;
	m_cTableLst.AddString(str);                 //  在列表控件中显示表名称
	nTableNum++;
	if(nTableNum==1)  //  第一张表
		m_cTableLst.SetCurSel(0);
	OnSelchangeTable();
}

//选择表单
void CTestDlg::OnSelchangeTable() 
{
	if(sDBName==_T(""))          //  没有关联数据库
		return;
	UpdateData();
	
	CString str;
	int Num=m_cTableLst.GetCurSel();  //  选择的表单序列号
	m_cTableLst.GetText(Num,str);     //  获得表名称
	if(sTableName==str)               //  表名没有改变
		return;
	sTableName=str;
	//设置记录显示
	TableLst.ClearAllItems();
	for(int i=0; i<TableLst.m_nCols; i++)
	{
		m_cRecordLstCtrl.DeleteColumn(0);
	}
	CADODataBase Table;                    // 创建ADO类对象
	if(!Table.OpenTable(sDBName,sTableName))   //  打开选择的表单
	{
		AfxMessageBox(_T("打开数据表不成功!"));
	}
	Table.GetFieldName();                  //  获取字段名称
	for(i=0; i<Table.m_values.GetSize(); i++)  //显示字段名称
	{
		str=Table.m_values.GetAt(i);
		TableLst.SetTableHead(i,str,str.GetLength()*12);  // 显示字段名称
	}
	int nRecord=Table.GetRecordNumber();   //  获得记录数目;
	TableLst.SetRows(nRecord);             //  设置显示的行数
	for(i=0; i<nRecord; i++)               //  读取并显示记录值
	{
		Table.ReadRecord(i);
		for(int j=0; j<Table.m_values.GetSize(); j++)
		{
			str=Table.m_values.GetAt(j);   //  字段值
			TableLst.SetTableText(i,j,str);//  显示到列表控件
		}
	}
}

void CTestDlg::OnNewRecord() 
{
	if(sDBName==_T(""))          //  没有关联数据库
		return;

	if(sTableName==_T(""))       //  没有关联数据表
		return;
	
	CAddRecordDlg dlg;   //添加记录对话框	
	dlg.sDBName   =sDBName;     // 数据库路径
	dlg.sTableName=sTableName;  // 数据表名称
	if(dlg.DoModal()==IDCANCEL)
		return;

	//设置记录显示
	TableLst.ClearAllItems();
	CADODataBase Table;                    // 创建ADO类对象
	if(!Table.OpenTable(sDBName,sTableName))   //  打开选择的表单
	{
		AfxMessageBox(_T("打开数据表不成功!"));
	}
	int nRecord=Table.GetRecordNumber();   //  获得记录数目;
	TableLst.SetRows(nRecord);             //  设置显示的行数
	for(int i=0; i<nRecord; i++)               //  读取并显示记录值
	{
		Table.ReadRecord(i);
		for(int j=0; j<Table.m_values.GetSize(); j++)
		{
			CString str=Table.m_values.GetAt(j);   //  字段值
			TableLst.SetTableText(i,j,str);//  显示到列表控件
		}
	}

}

⌨️ 快捷键说明

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