📄 daosamp1dlg.cpp
字号:
// daosamp1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "daosamp1.h"
#include "daosamp1Dlg.h"
#include "RecordNew.h"
#include "crack.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CDaosamp1Dlg dialog
CDaosamp1Dlg::CDaosamp1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CDaosamp1Dlg::IDD, pParent),m_pDB(NULL)
{
//{{AFX_DATA_INIT(CDaosamp1Dlg)
// 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 CDaosamp1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDaosamp1Dlg)
DDX_Control(pDX, IDC_TREE1, m_DBTree);
DDX_Control(pDX, IDC_LIST1, m_DBList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDaosamp1Dlg, CDialog)
//{{AFX_MSG_MAP(CDaosamp1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_RECORD_INSERT, OnRecordInsert)
ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_COMMAND(ID_MENUITEM32773, OnMenuitem32773)
ON_COMMAND(ID_MENUITEM32774, OnMenuitem32774)
ON_COMMAND(ID_MENUITEM32775, OnMenuitem32775)
ON_COMMAND(ID_MENUITEM32776, OnMenuitem32776)
ON_COMMAND(ID_MENUITEM32777, OnMenuitem32777)
ON_COMMAND(ID_MENUITEM32778, OnMenuitem32778)
ON_COMMAND(ID_MENUITEM32779, OnMenuitem32779)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDaosamp1Dlg message handlers
BOOL CDaosamp1Dlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CDaosamp1Dlg::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 CDaosamp1Dlg::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 CDaosamp1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDaosamp1Dlg::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog m_DBDialog(TRUE,"mdb","",OFN_OVERWRITEPROMPT,"Access Database文件(*.mdb)|*.mdb||");
if(m_DBDialog.DoModal()==IDOK)
{
UpdateData(TRUE);
m_DBList.DeleteAllItems();
while(m_DBList.DeleteColumn(0));
CDaoDatabase* tmpDB=new CDaoDatabase;
try
{
tmpDB->Open(m_DBDialog.GetPathName());
}
catch(CDaoException* e)
{
DisplayDaoException(e);
delete tmpDB;
e->Delete();
return;
}
if(m_pDB)
{
m_DBTree.Expand(m_hTreeItem1,TVE_TOGGLE);
if(m_pDB->IsOpen()) m_pDB->Close();
m_pDB=NULL;
}
m_pDB=tmpDB;
RefreshTree();
}
UpdateData(FALSE);
}
void CDaosamp1Dlg::OnRecordInsert()
{
// TODO: Add your command handler code here
if(m_pDB)
{
if(m_pDB->IsOpen())
{
if(m_DBTree.GetItemData(m_DBTree.GetSelectedItem())!=100) return;
CString TableName=m_DBTree.GetItemText(m_DBTree.GetSelectedItem());
CRecordNew m_RecordNew(m_pDB,TableName);
if(m_RecordNew.DoModal()==IDOK)
{
InsertRecord(TableName,m_RecordNew.m_RecordValue);
int nFields=m_RecordNew.m_RecordValue.GetSize();
int nItem=m_DBList.GetItemCount();
for(int i=0;i<nFields;i++)
{
LV_ITEM m_ListItem;
m_ListItem.mask=LVIF_TEXT;
m_ListItem.iItem=nItem;
m_ListItem.iSubItem=i;
CString FieldValue=m_RecordNew.m_RecordValue.GetAt(i);
m_ListItem.pszText=FieldValue.GetBuffer(200);
m_DBList.InsertItem(&m_ListItem);
m_DBList.SetItemText(m_ListItem.iItem,m_ListItem.iSubItem,m_ListItem.pszText);
}
}
}
}
}
void CDaosamp1Dlg::OnRecordDelete()
{
// TODO: Add your command handler code here
UpdateData(TRUE);
if(m_DBList.GetSelectedCount()==0) return;
CString TableName=m_DBTree.GetItemText(m_DBTree.GetSelectedItem());
int DelCount=DeleteRecords(TableName);
int j=m_DBList.GetItemCount();
for(int i=0;i<j;i++)
{
if(m_DBList.GetItemState(i,LVIS_SELECTED)==LVIS_SELECTED)
{
m_DBList.DeleteItem(i);
i--;
j--;
}
}
}
void CDaosamp1Dlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
UpdateData(TRUE);
switch(pNMTreeView->itemNew.lParam)
{
case 0:
case 10:
case 20:break;
case 100:
{
CString TableName=m_DBTree.GetItemText(m_DBTree.GetSelectedItem());
RefreshList(100,TableName);
break;
}
case 200:
{
CString QueryName=m_DBTree.GetItemText(m_DBTree.GetSelectedItem());
RefreshList(200,QueryName);
break;
}
}
UpdateData(FALSE);
*pResult = 0;
}
void CDaosamp1Dlg::RefreshTree()
{
UpdateData(TRUE);
m_DBTree.DeleteAllItems();
ASSERT(m_pDB);
ASSERT(m_pDB->IsOpen());
TV_INSERTSTRUCT m_TreeItem;
m_TreeItem.hInsertAfter=TVI_LAST;
m_TreeItem.item.mask=TVIF_TEXT|TVIF_PARAM;
m_TreeItem.hParent=TVI_ROOT;
CString dbName=m_pDB->GetName();
m_TreeItem.item.pszText=(LPTSTR) dbName.GetBuffer(200);
m_TreeItem.item.lParam=0;
m_hTreeItem1=m_DBTree.InsertItem(&m_TreeItem);
m_TreeItem.hParent=m_hTreeItem1;
m_TreeItem.item.pszText=(LPSTR) _T("Tables");
m_TreeItem.item.lParam=10;
HTREEITEM m_hTreeItem11=m_DBTree.InsertItem(&m_TreeItem);
m_TreeItem.item.lParam=20;
HTREEITEM m_hTreeItem12=m_DBTree.InsertItem(&m_TreeItem);
CDaoTableDefInfo tabInfo;
try
{
int nTableDefCount=m_pDB->GetTableDefCount();
for(int i=0;i<nTableDefCount;i++)
{
m_pDB->GetTableDefInfo(i,tabInfo);
if(tabInfo.m_lAttributes & dbSystemObject) continue;
m_TreeItem.hParent=m_hTreeItem11;
m_TreeItem.item.pszText=(LPSTR)(tabInfo.m_strName.GetBuffer(200));
m_TreeItem.item.lParam=100;
HTREEITEM m_hTreeItem111=m_DBTree.InsertItem(&m_TreeItem);
}
}
catch(CDaoException * e)
{
DisplayDaoException(e);
e->Delete();
return;
}
try
{
CDaoQueryDefInfo queryInfo;
int nQueryDefCount=m_pDB->GetQueryDefCount();
for(int i=0;i<nQueryDefCount;i++)
{
m_pDB->GetQueryDefInfo(i,queryInfo);
m_TreeItem.hParent=m_hTreeItem12;
m_TreeItem.item.pszText=(LPSTR)(queryInfo.m_strName.GetBuffer(200));
m_TreeItem.item.lParam=200;
HTREEITEM m_hTreeItem121=m_DBTree.InsertItem(&m_TreeItem);
}
}
catch(CDaoException* e)
{
DisplayDaoException(e);
e->Delete();
}
m_DBTree.Expand(m_hTreeItem1,TVE_EXPAND);
UpdateData(FALSE);
}
void CDaosamp1Dlg::RefreshList(int rType, CString strTableName)
{
UpdateData(TRUE);
m_DBList.DeleteAllItems();
while(m_DBList.DeleteColumn(0));
ASSERT(m_pDB);
ASSERT(m_pDB->IsOpen());
CDaoFieldInfo fieldInfo;
int nFields;
if(rType==100)
{
CDaoTableDef td(m_pDB);
try
{
td.Open(strTableName);
nFields=td.GetFieldCount();
for(int j=0;j<nFields;j++)
{
td.GetFieldInfo(j,fieldInfo);
m_DBList.InsertColumn(j,fieldInfo.m_strName,LVCFMT_LEFT,90,j);
}
td.Close();
}
catch(CDaoException* e)
{
DisplayDaoException(e);
e->Delete();
return;
}
}
else if(rType==200)
{
CDaoQueryDef td(m_pDB);
try
{
td.Open(strTableName);
nFields=td.GetFieldCount();
for(int j=0;j<nFields;j++)
{
td.GetFieldInfo(j,fieldInfo);
m_DBList.InsertColumn(j,fieldInfo.m_strName,LVCFMT_LEFT,90,j);
}
td.Close();
}
catch(CDaoException* e)
{
DisplayDaoException(e);
e->Delete();
return;
}
}
CDaoRecordset rs(m_pDB);
int nItem=0;
try
{
CString strSelect(_T("Select * From ["));
strSelect+=strTableName;
strSelect+=_T("]");
rs.Open(dbOpenDynaset,strSelect);
while(!rs.IsEOF())
{
COleVariant var;
for(int i=0;i<nFields;i++)
{
var=rs.GetFieldValue(i);
LV_ITEM m_ListItem;
m_ListItem.mask=LVIF_TEXT;
m_ListItem.iItem=nItem;
m_ListItem.iSubItem=i;
CString FieldValue=CCrack::strVARIANT(var);
m_ListItem.pszText=FieldValue.GetBuffer(200);
m_DBList.InsertItem(&m_ListItem);
m_DBList.SetItemText(m_ListItem.iItem,m_ListItem.iSubItem,m_ListItem.pszText);
}
nItem++;
rs.MoveNext();
}
}
catch(CDaoException* e)
{
DisplayDaoException(e);
e->Delete();
return;
}
UpdateData(FALSE);
}
bool CDaosamp1Dlg::InsertRecord(CString pTableName, CStringArray &pFieldValue)
{
CDaoTableDef td(m_pDB);
td.Open(pTableName);
CDaoFieldInfo fieldInfo;
CDaoRecordset rs(m_pDB);
try
{
rs.Open(&td,dbOpenDynaset,dbAppendOnly|dbInconsistent);
}
catch(CDaoException* e)
{
DisplayDaoException(e);
e->Delete();
return FALSE;
}
int nFieldCount=rs.GetFieldCount();
rs.AddNew();
COleVariant tempFieldValue;
int IsString;
char tempFieldStringValue[200];
for(int i=0;i<nFieldCount;i++)
{
rs.GetFieldInfo(i,fieldInfo,AFX_DAO_PRIMARY_INFO);
switch(fieldInfo.m_nType)
{
case (dbBoolean):
{
short ss=(short) atoi(pFieldValue.GetAt(i).GetBuffer(100));
tempFieldValue=COleVariant(ss,VT_I2);
IsString=0;
break;
}
case (dbByte):
{
BYTE bb=(BYTE) (pFieldValue.GetAt(i).GetBuffer(100));
tempFieldValue=COleVariant(bb);
IsString=0;
break;
}
case (dbInteger):
{
short ss=(short) atoi(pFieldValue.GetAt(i).GetBuffer(100));
tempFieldValue=COleVariant(ss,VT_I2);
IsString=0;
break;
}
case (dbLong):
{
long ll=(long) atol(pFieldValue.GetAt(i).GetBuffer(100));
tempFieldValue=COleVariant(ll,VT_I4);
IsString=0;
break;
}
case (dbSingle):
{
float ff=(float) atof(pFieldValue.GetAt(i).GetBuffer(100));
tempFieldValue=COleVariant(ff);
IsString=0;
break;
}
case (dbDouble):
{
double dd=(double) atof(pFieldValue.GetAt(i).GetBuffer(100));
tempFieldValue=COleVariant(dd);
IsString=0;
break;
}
case (dbDate):
{
CString cc=(CString) (pFieldValue.GetAt(i));
tempFieldValue=COleVariant(cc);
IsString=0;
break;
}
case (dbText):
{
CString cc=(CString) (pFieldValue.GetAt(i));
strcpy(tempFieldStringValue,cc.GetBuffer(200));
IsString=1;
break;
}
case (dbMemo):
{
CString cc=(CString) (pFieldValue.GetAt(i));
strcpy(tempFieldStringValue,cc.GetBuffer(200));
IsString=1;
break;
}
}
if(IsString)
rs.SetFieldValue(fieldInfo.m_strName,tempFieldStringValue);
else
rs.SetFieldValue(fieldInfo.m_strName,tempFieldValue);
}
try
{
rs.Update();
}
catch(CDaoException* e)
{
DisplayDaoException(e);
e->Delete();
return FALSE;
}
if(rs.IsOpen()) rs.Close();
if(td.IsOpen()) td.Close();
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -