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

📄 accedlg.cpp

📁 DAO读写ACCESS文件 DAO读写ACCESS文件
💻 CPP
字号:
// BY:    QQ:316118740
//博客 http://hi.baidu.com/alalmn
//希望能结识更多数据库开发的朋友

#include "stdafx.h"
#include "ACCE.h"
#include "ACCEDlg.h"
#include <afxdao.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CDaoDatabase db;				//数据库   要加入头文件 #include <afxdao.h>
CDaoRecordset RecSet(&db);		//记录集
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CACCEDlg dialog

CACCEDlg::CACCEDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CACCEDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CACCEDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CACCEDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CACCEDlg)
	DDX_Control(pDX, IDC_LIST1, m_LineList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CACCEDlg, CDialog)
	//{{AFX_MSG_MAP(CACCEDlg)
	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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CACCEDlg message handlers

BOOL CACCEDlg::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);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//上线列表初始化
	LONG lStyle = m_LineList.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
	lStyle |=  LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
	m_LineList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0,(LPARAM)lStyle);
	m_LineList.InsertColumn(0,"姓名",NULL,150,-1);
	m_LineList.InsertColumn(1,"年龄",NULL,110,-1);
	///////////////////////////
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CACCEDlg::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 CACCEDlg::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 CACCEDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CACCEDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//获取主程序所在路径,存在sPath中
	CString sPath;
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer();  // 释放多余的内存
	int nPos;
	nPos=sPath.ReverseFind('\\');  //搜说自己串
	sPath=sPath.Left(nPos);  //拷贝字符串
	
	//默认创建数据名:Demo.mdb,内部表名:DemoTable,表内有二个字段:姓名、年龄
	CString lpszFile = sPath + "\\Demo.mdb";
	
	CFileFind  fFind;
	BOOL bSuccess;
	bSuccess=fFind.FindFile(lpszFile);  //查找文件是否存在
	fFind.Close();
    //是否已有创建好的Demo.mdb文件,没有则创建它
	if(!bSuccess)
	{
		db.Create(lpszFile);  //创建数据库
		
		CString SqlCmd = "CREATE TABLE DemoTable(Name VARCHAR(20),Age VARCHAR(10));";
		               //创建数据库 DemoTable数据库名 Name名称 VARCHAR(20)类型20个字节   
		db.Execute(SqlCmd);    //创建数据库表
		
		//打开已创建的数据表
		RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT * FROM DemoTable", 0);
		//加入第一个记录,用SQL语句
		db.Execute("INSERT INTO DemoTable (Name,Age) VALUES ('丁善本',26)");
		//添加数据 conn.execute("insert into 表名 (字段1,字段2,字段3) value (值1,值2,值3)  ") 
		
		//加入第二个记录,用DAO涵数
		RecSet.AddNew();
		RecSet.SetFieldValue("Name","程天蓝");
		RecSet.SetFieldValue("Age","21");
		RecSet.Update();
		
		//加入第三个记录,用DAO涵数
		RecSet.AddNew();
		RecSet.SetFieldValue("Name","钟守康");
		RecSet.SetFieldValue("Age","25");
		RecSet.Update();
		
		//关闭记录集及库
		RecSet.Close();
		db.Close();
		
		AfxMessageBox("Access文件写入成功!");
	}
	else
		AfxMessageBox("Demo.mdb数据库已经创建!");
}

void CACCEDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	COleVariant var;		// 字段类型
	var.ChangeType(VT_BSTR, NULL);
	CString strName,strAge,strFile; 
	
	//清空列表框
//	m_AccessList.ResetContent();
	
	//获取主程序所在路径,存在sPath中
	CString sPath;
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer ();
    int nPos;
	nPos=sPath.ReverseFind ('\\');
	sPath=sPath.Left (nPos);
	
	strFile = sPath + "\\demo.mdb";
	db.Open(strFile);		// 打开已创建的demo数据库及DamoTable表
	RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT * FROM DemoTable",NULL);
	
	CString s1,s2;
	m_LineList.DeleteAllItems();  //清空内容
	while(!RecSet.IsEOF())	// 有没有到表结尾
	{
		RecSet.GetFieldValue("Name",var);   //记录集中的字段数量
		s1 = (LPCSTR)var.pbstrVal;
		RecSet.GetFieldValue("Age",var);   //记录集中的字段数量
		s2 = (LPCSTR)var.pbstrVal;
		//m_AccessList.AddString( strName + " --> "+strAge );   //添加数据
		int n=m_LineList.GetItemCount();
		m_LineList.InsertItem(n,s1);  //姓名
		m_LineList.SetItemText(n,1,s2);  //年龄
		
		RecSet.MoveNext();  //移动到下一条记录
	}
	
	//关闭记录集及库
	RecSet.Close();
	db.Close();
}
#include "add.h"
Cadd add;
void CACCEDlg::OnButton3() 
{
	add.DoModal();  
	// TODO: Add your control notification handler code here
	//获取主程序所在路径,存在sPath中
/*	CString sPath;
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer ();
    int nPos;
	nPos=sPath.ReverseFind ('\\');
	sPath=sPath.Left (nPos);
	
	CString strFile = sPath + "\\demo.mdb";
	db.Open(strFile);		// 打开已创建的demo数据库及DamoTable表
	RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE, "SELECT * FROM DemoTable",NULL);
	
	RecSet.AddNew();  //允许我们加入一个新的数据
	RecSet.SetFieldValue("Name","Chris");  //加入新的数据
	RecSet.SetFieldValue("Age","13");
	RecSet.Update();     //保存记录的修改
	RecSet.MoveNext();   //移动到当前记录的下一条记录
	RecSet.AddNew();     //允许我们加入一个新的数据
	RecSet.SetFieldValue("Name","Joe");
	RecSet.SetFieldValue("Age","20");
	RecSet.Update();     //保存记录的修改    

	//关闭记录集及库
	RecSet.Close();
	db.Close();         */

}

void CACCEDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	//获取主程序所在路径,存在sPath中
	CString sPath;
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer ();
    int nPos;
	nPos=sPath.ReverseFind ('\\');
	sPath=sPath.Left (nPos);
	
	CString strFile = sPath + "\\demo.mdb";
	db.Open(strFile);		// 打开已创建的demo数据库及DamoTable表

  // 检测是否已选择书目
	int nCurSel =m_LineList.GetNextItem(-1,LVNI_SELECTED) ;
	if(nCurSel == -1 )
	{
	MessageBox ("没有选择中!");
	return ;
	}
	// 定义记录集对象,并移动指针到指定位置
	RecSet.Open( dbOpenDynaset, "SELECT * FROM DemoTable" );
	RecSet.Move (nCurSel) ;//将游标指针移动到指定的记录数位置
	RecSet.Delete();  //删除    

	//关闭记录集及库
	RecSet.Close();
	db.Close();
}

#include "hack.h"
Chack ModBookDlg;
void CACCEDlg::OnButton5() 
{
	// TODO: Add your control notification handler code here
/*		// 检测是否选择书目
	int nCurSel = this->m_LineList.GetNextItem ( -1,LVNI_SELECTED);
	if(nCurSel == -1 )
	{
		this->MessageBox("没有选择数据!");
		return ;
	}// 定义记录集对象,并移动指针到指定位置
	CString sPath;
	GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
	sPath.ReleaseBuffer ();
    int nPos;
	nPos=sPath.ReverseFind ('\\');
	sPath=sPath.Left (nPos);
	
	CString strFile = sPath + "\\demo.mdb";
	db.Open(strFile);		// 打开已创建的demo数据库及DamoTable表

	//CDaoRecordset Record ( strFile ) ;
	RecSet.Open ( dbOpenDynaset, "SELECT * FROM DemoTable" );
//	RecSet.Move ( nCurSel ) ;// 定义对话框对象,并设置为"修改"模式

	COleVariant OleVariant;
	RecSet.GetFieldValue(0,OleVariant);
	CString s1= (LPCSTR)OleVariant.pbstrVal;
	ModBookDlg.s1=s1;
//	MessageBox(s1);
//	ModBookDlg.SetBookName(OleVariant.bstrVal);
	RecSet.GetFieldValue(1,OleVariant);
	CString s2= (LPCSTR)OleVariant.pbstrVal;
	ModBookDlg.s2=s2;
//	MessageBox(s2);
//	ModBookDlg.SetAuthor(OleVariant.bstrVal);

//	ModBookDlg.SetMode(FALSE);// 把当前记录信息设置到对话框   */


	int nCurSel = this->m_LineList.GetNextItem ( -1,LVNI_SELECTED);
	if(nCurSel == -1 )
	{
		this->MessageBox("没有选择修改数据!");
		return ;
	}

	int nSelRows = m_LineList.GetSelectedCount();   //被选择的项的数目
	if(!nSelRows) //-- If there are no rows selected,jump out here
		return;
	POSITION pos =m_LineList.GetFirstSelectedItemPosition();  //返回一个用于迭代或获取对象指针的POSITION值;如果没有选择任何项,那么将返回NULL。
	int i = m_LineList.GetNextSelectedItem(pos);   //返回列表视图控件中被选择的下一项的索引值
	TRACE("First Item %d\n",i);
	CString s1= m_LineList.GetItemText(i, 0);    //获取信息
	CString s2= m_LineList.GetItemText(i, 1);    //获取信息
	ModBookDlg.s1=s1;
    ModBookDlg.s2=s2;
    ModBookDlg.DoModal();   

}

⌨️ 快捷键说明

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