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

📄 ljdbtestdlg.cpp

📁 小型文件数据库 一个文件对应一个表 最大只支持65535条记录(可以修改宏) 定长记录 一级索引 支持多关键字索引 不支持sql语句
💻 CPP
字号:
// ljdbTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ljdbTest.h"
#include "ljdbTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
using namespace filedbns;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLjdbTestDlg dialog

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

void CLjdbTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLjdbTestDlg)
	DDX_Control(pDX, IDC_TAB, m_tab);
	DDX_Text(pDX, IDC_EDDBNAME, m_dbname);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CLjdbTestDlg, CDialog)
	//{{AFX_MSG_MAP(CLjdbTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB, OnSelchangeTab)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_BN_CLICKED(IDC_BUTTON12, OnButton12)
	ON_BN_CLICKED(IDC_BUTTON13, OnButton13)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLjdbTestDlg message handlers

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here
	m_tab.InsertItem(0,"字段"); 
	m_tab.InsertItem(1,"记录"); 
	m_pgField.Create(IDD_DLGFIELD,GetDlgItem(IDC_TAB));
	m_pgRecord.Create(IDD_DLGRECORD,GetDlgItem(IDC_TAB));

	CRect rs; 
	m_tab.GetClientRect(rs); 
	rs.top+=20; 
	rs.bottom-=4; 
	rs.left+=4; 
	rs.right-=4; 
	
	m_pgField.MoveWindow(rs); 
	m_pgRecord.MoveWindow(rs); 
	m_pgField.ShowWindow(TRUE); 
	m_tab.SetCurSel(0); 
	
	m_pgField.m_fields.InsertColumn(0,"序号",LVCFMT_LEFT,100);
	m_pgField.m_fields.InsertColumn(1,"字段名称",LVCFMT_LEFT,150);
	m_pgField.m_fields.InsertColumn(2,"字段长度",LVCFMT_LEFT,100);
	m_pgField.m_fields.InsertColumn(3,"字段类型",LVCFMT_LEFT,100);
	m_pgField.m_fields.InsertColumn(4,"其它",LVCFMT_LEFT,100);
	m_pgField.m_fields.SetExtendedStyle(m_pgField.m_fields.GetExtendedStyle()|LVS_EX_FULLROWSELECT);
	m_pgRecord.m_records.SetExtendedStyle(m_pgRecord.m_records.GetExtendedStyle()|LVS_EX_FULLROWSELECT);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CLjdbTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CLjdbTestDlg::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();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CLjdbTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CLjdbTestDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	m_fileDB.Create("test.dat");
}

void CLjdbTestDlg::OnButton2() 
{
	m_fileDB.Clos();	
}

void CLjdbTestDlg::OnButton3() 
{
	m_fileDB.FieldSet.Add("no",4,FT_INT4,FT_KEY);
	m_fileDB.FieldSet.Add("xh",15,FT_STR,FT_KEY);
	m_fileDB.FieldSet.Add("name",10,FT_STR,FT_NOR);
	m_fileDB.FieldSet.Add("sex",3,FT_STR,FT_NOR);
	m_fileDB.FieldSet.Add("age",4,FT_INT4,FT_NOR);
}

void CLjdbTestDlg::OnButton4() 
{
	UpdateData();
	if( m_dbname.IsEmpty() ){
		AfxMessageBox("请输入数据库文件名");
		return;
	}
	if( m_fileDB.Open(m_dbname.GetBuffer(0))!=0 ){
		AfxMessageBox("打开数据失败");
		return ;
	}
	m_pgField.m_fields.DeleteAllItems();
	m_pgRecord.m_records.DeleteAllItems();
	while(m_pgRecord.m_records.DeleteColumn(0));
	char buf[36];
	for(int j=0;j<m_fileDB.FieldSet.Count();++j){
		TField field = m_fileDB.FieldSet[j];
		itoa(field._no,buf,10);
		int i = m_pgField.m_fields.InsertItem(m_pgField.m_fields.GetItemCount(),(const char*)buf);
		m_pgField.m_fields.SetItemText(i,1,(const char*)field._fn);
		itoa(field._fsz,buf,10);
		m_pgField.m_fields.SetItemText(i,2,(const char*)buf);
		itoa(field._ft,buf,10);
		m_pgField.m_fields.SetItemText(i,3,(const char*)buf);
		itoa(field._key,buf,10);
		m_pgField.m_fields.SetItemText(i,4,(const char*)buf);
		//itoa(field._ind,buf,10);
		//m_fields.SetItemText(i,5,(const char*)buf);

		m_pgRecord.m_records.InsertColumn(j,(const char*)field._fn,LVCFMT_LEFT,100);
	}
}

void CLjdbTestDlg::OnButton5() 
{
	
	//m_fileDB.RecordSet.Insert("9","abc",NULL);

	m_fileDB.RecordSet.Insert("0","10010001","zm","n","20",NULL);
	m_fileDB.RecordSet.Insert("1","10010002","oyz","y","40",NULL);
	m_fileDB.RecordSet.Insert("2","10010003","qq","n","20",NULL);
	m_fileDB.RecordSet.Insert("3","10010004","slj","y","40",NULL);
	m_fileDB.RecordSet.Insert("4","10010005","wgj","y","40",NULL);/**/

	/*for(int i=1001;i>0;--i){
		char buf[5];
		itoa(i,buf,10);
		if( m_fileDB.RecordSet.Insert(buf,buf,buf,buf,NULL) == NULL ){
			if( m_fileDB.Erro() == ER_KEYCONFICT ){
				AfxMessageBox("主键冲突");
				return;
			}
		}
	}*/
}

void CLjdbTestDlg::OnButton6() 
{
	if( m_fileDB.Open() != 0 ){
		AfxMessageBox("打开表失败");
	}
}

void CLjdbTestDlg::OnButton7() 
{
	/*m_records.DeleteAllItems();
	TWHERE where;
	strcpy(where.fn,"xh");
	where.val = "10010002";
	TWHEREEXPRESS whereExpress;
	whereExpress.push_back(where);
	if( m_fileDB.RecordSet.Select(whereExpress) == ER_DBNOTOPEN ){
		AfxMessageBox("数据库已经关闭");
		return ;
	}
	if( m_fileDB.RecordSet.Count() == 0 ){
		AfxMessageBox("查询无数据");
		return;
	}
	TRecord record = m_fileDB.RecordSet[0];
	vector<char*>::iterator iter = record._values.begin();
	int i = m_records.InsertItem(m_records.GetItemCount(),(const char*)*iter);
	iter++;
	for(int j=1;iter!=record._values.end();++iter,++j){
		m_records.SetItemText(i,j,(const char*)*iter);
	}*/
}

void CLjdbTestDlg::OnButton8() 
{
	/*TWHERE where;
	strcpy(where.fn,"xh");
	where.val = "10010002";
	TWHEREEXPRESS whereExpress;
	whereExpress.push_back(where);

	TWHEREEXPRESS valSet;
	strcpy(where.fn,"name");
	where.val = "modified";
	valSet.push_back(where);
	
	m_fileDB.RecordSet.Update(whereExpress,valSet);*/
}

void CLjdbTestDlg::OnButton9() 
{
	m_pgRecord.m_records.DeleteAllItems();
	m_fileDB.RecordSet.Select();
	for(int i=0;i<m_fileDB.RecordSet.Count();++i){
		TRecord record = m_fileDB.RecordSet[i];
		vector<char*>::iterator iter = record._values.begin();
		int t = m_pgRecord.m_records.InsertItem(m_pgRecord.m_records.GetItemCount(),(const char*)*iter);
		iter++;
		for(int j=1;iter!=record._values.end();++iter,++j){
			m_pgRecord.m_records.SetItemText(t,j,(const char*)*iter);
		}
	}
}

void CLjdbTestDlg::OnButton10() 
{
	CFileDialog dlgFile(true);
	dlgFile.DoModal();
	m_dbname = dlgFile.GetPathName();
	UpdateData(false);
}

void CLjdbTestDlg::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int CurSel; 
	CurSel=m_tab.GetCurSel(); 
	switch(CurSel) 
	{ 
	case 0: 
		m_pgField.ShowWindow(TRUE); 
		m_pgRecord.ShowWindow(FALSE); 
		break; 
	case 1: 
		m_pgField.ShowWindow(FALSE); 
		m_pgRecord.ShowWindow(TRUE); 
		break; 
	default: ; 
	}
}

void CLjdbTestDlg::OnButton11() 
{
	m_fileDB.Create("a.db");
	m_fileDB.FieldSet.Add("GNo",4,FT_INT4,FT_KEY);
	m_fileDB.FieldSet.Add("GName",17,FT_STR,FT_NOR);
	m_fileDB.Open();
	m_fileDB.RecordSet.Insert("0","abc",NULL);
	m_fileDB.RecordSet.Insert("1","dsdf",NULL);
	m_fileDB.RecordSet.Insert("2","adsfsdfdsfbc",NULL);
	m_fileDB.RecordSet.Select();
	char buf[10];memset(buf,0,10);
	itoa(m_fileDB.RecordSet.Count(),buf,10);
	AfxMessageBox(buf);
	m_fileDB.Clos();
}

int GNo = 22027;
void CLjdbTestDlg::OnButton12() 
{
	for(int n=0;n<1000;++n){

		if( m_fileDB.Open(m_dbname.GetBuffer(0))!=0 ){
			AfxMessageBox("打开数据失败");
			return ;
		}

	TWHERE where;
	strcpy(where.fn,"UserSerial");
	char* p1= where.val = new char[20];
	memset(where.val,0,20);
	strcpy(where.val,"10011000");
	TWHEREEXPRESS whereExpress;
	whereExpress.push_back(where);

	strcpy(where.fn,"GNo");
	char* p2= where.val = new char[20];
	memset(where.val,0,4);
	//strcpy(where.val,"88");
	itoa(GNo,where.val,10);
	whereExpress.push_back(where);

	TWHEREEXPRESS valSet;
	strcpy(where.fn,"NickName");
	where.val = "77";
	valSet.push_back(where);
	
	GNo++;
	valSet;
	strcpy(where.fn,"GNo");
	char* p3= where.val = new char[20];
	itoa(GNo,where.val,10);
	//where.val = "99";
	valSet.push_back(where);

	m_fileDB.RecordSet.Update(whereExpress,valSet);
	//m_fileDB.RecordSet.Select(whereExpress);

	delete []p1;
	delete []p2;
	delete []p3;
	
	m_pgRecord.m_records.DeleteAllItems();
	for(int i=0;i<m_fileDB.RecordSet.Count();++i){
		TRecord record = m_fileDB.RecordSet[i];
		vector<char*>::iterator iter = record._values.begin();
		int t = m_pgRecord.m_records.InsertItem(m_pgRecord.m_records.GetItemCount(),(const char*)*iter);
		iter++;
		for(int j=1;iter!=record._values.end();++iter,++j){
			m_pgRecord.m_records.SetItemText(t,j,(const char*)*iter);
		}
	}
		m_fileDB.Clos();
	}
}

void CLjdbTestDlg::OnButton13() 
{
	TWHERE where;
	strcpy(where.fn,"no");
	char* p1= where.val = new char[4];
	memset(where.val,0,4);
	strcpy(where.val,"3");
	TWHEREEXPRESS whereExpress;
	whereExpress.push_back(where);

	strcpy(where.fn,"xh");
	char* p2= where.val = new char[15];
	memset(where.val,0,15);
	strcpy(where.val,"10010004");
	whereExpress.push_back(where);
	
	m_fileDB.RecordSet.Delete(whereExpress);
}

⌨️ 快捷键说明

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