level 1
下一章爽歪歪
楼主
#include "stdafx.h"
#include "test.h"
#include "MainFrm.h"
#include "testDoc.h"
#include "testView.h"
#include "LoadDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int m_nPurview;
extern BOOL m_bAccept;
/////////////////////////////////////////////////////////////////////////////
// CTestApp
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
//{{AFX_MSG_MAP(CTestApp)
ON_COMMAND(ID_APP_ABOUT,OnAppAbout)
//NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks ofgenerated code!
//}}AFX_MSG_MAP
//Standard file based document commands
ON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)
//Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP,CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestApp construction
CTestApp::CTestApp()
{
//TODO: add construction code here,
//Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CTestApp object
CTestApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CTestApp initialization
BOOL CTestApp::InitInstance()
{
AfxEnableControlContainer();
//Standard initialization
//If you are not using these features and wish to reduce the size
// of your final executable, you should removefrom the following
// the specific initialization routines you donot need.
CLoadDlg dlg;
aa: if(dlg.DoModal()== IDOK)
{
if(!m_bAccept)
{
AfxMessageBox("用户名或密码错误,请重新输入!");
gotoaa;
}
}
else
returnFALSE;
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFCin a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
//Change the registry key under which our settings are stored.
//TODO: You should modify this string to be something appropriate
//such as the name of your company or organization.
SetRegistryKey(_T("LocalAppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (includingMRU)
//Register the application*s document templates. Document templates
// serve as the connection between documents,frame windows and views.
CSingleDocTemplate*pDocTemplate;
pDocTemplate= new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CTestView));
AddDocTemplate(pDocTemplate);
//Parse command line for standard shell commands, DDE, file open
CCommandLineInfocmdInfo;
ParseCommandLine(cmdInfo);
//Dispatch commands specified on the command line
if(!ProcessShellCommand(cmdInfo))
returnFALSE;
//The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 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:
virtualvoid DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//No message handlers
//}}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()
// App command to run the dialog
void CTestApp::OnAppAbout()
{
CAboutDlgaboutDlg;
aboutDlg.DoModal();
}
// testDoc.cpp : implementation of theCTestDoc class
//
#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "database.h"
#include "field.h"
#include "index.h"
#include "tabledef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CStringArray m_arrayFieldName;
CStringArray m_arrayFieldValue;
CWordArray m_arrayFieldSize;
CString m_strDatabase;
CString m_strConnect;
CString m_strQuery;
int m_nRowCount;
int m_nFields;
int m_nTables;
int m_nRows = 0;
extern int m_nPurview;
extern CString m_strOldCode;
extern CString m_strUserID;
extern CString m_strNewCode;
extern BOOL m_bAccept;
extern int m_nTableNO;
extern CString m_strReaderID;
extern CString m_strBookNumber;
extern int m_nFieldRow;
int flag;
/////////////////////////////////////////////////////////////////////////////
// CTestDoc
void DaoErrorMsg(CDaoException* e)
{
char errorMsg[301];
wsprintf(errorMsg, "DAO error %d, SOURCE= %s, DESCR = %s",
e->m_pErrorInfo->m_lErrorCode,
(const char*) e->m_pErrorInfo->m_strSource,
(const char*) e->m_pErrorInfo->m_strDescription);
AfxMessageBox(errorMsg);
}
IMPLEMENT_DYNCREATE(CTestDoc, CDocument)
BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
//{{AFX_MSG_MAP(CTestDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDoc construction/destruction
CTestDoc::CTestDoc()
{
m_arrayTableName.SetSize(5);
m_arrayTableName[0]= "读者信息";
m_arrayTableName[1]= "库存图书";
m_arrayTableName[2]= "员工信息";
m_arrayTableName[3]= "库存图书";
m_arrayTableName[4]= "";
m_arrayField.SetSize(3);
m_arrayField[0]= "ID";
m_arrayField[1]= "书名";
m_arrayField[2]= "借阅日期";
}
CTestDoc::~CTestDoc()
{
if(m_database.IsOpen())
CloseMdb();
}
BOOL CTestDoc::OnNewDocument()
{
if(!CDocument::OnNewDocument())
returnFALSE;
ConnectMdb();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTestDoc serialization
void CTestDoc::Serialize(CArchive& ar)
{
if(ar.IsStoring())
{
//TODO: add storing code here
}
else
{
//TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CTestDoc diagnostics
#ifdef _DEBUG
void CTestDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CTestDoc::Dump(CDumpContext& dc)const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestDoc commands
void CTestDoc::ConnectMdb()
{
intnTables;
intn = 0;
BeginWaitCursor();
try {
// nonexclusive, read-only
m_database.Open(m_strDatabase, FALSE, FALSE);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
EndWaitCursor();
e->Delete();
return;
}
m_strDatabase.Empty();
m_strDatabase = m_database.GetName();
TRACE("databasename = %s, connect = %s\n",
(const char*) m_strDatabase, (const char*) m_strConnect);
nTables= m_database.GetTableDefCount();
CDaoTableDefInfotdi;
for(int i = 0;i < nTables;i ++)
{
m_database.GetTableDefInfo(i,tdi);
TRACE("tablename = %s\n",(constchar*)tdi.m_strName);
if(tdi.m_strName.Left(4) != "MSys") {
m_nTables++;
}
}
tbDef= new CDaoTableDef(&m_database);
EndWaitCursor();
}
void CTestDoc::OpenMdb()
{
BeginWaitCursor();
inti = 0;
m_arrayTableName[4]= m_strReaderID;
m_strQuery.Format("select* from [%s]", m_arrayTableName[m_nTableNO]);
m_pRecordset= new CDaoRecordset(&m_database);
try{
m_pRecordset->Open(dbOpenDynaset, m_strQuery);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
UpdateAllViews(NULL);
e->Delete();
return;
}
if(!m_pRecordset->IsBOF())
{
m_pRecordset->MoveLast();
}
m_nRowCount= m_pRecordset->GetAbsolutePosition() + 2;
TRACE("m_nRowCount= %d\n",m_nRowCount);
m_nFields= (int)m_pRecordset->GetFieldCount();
m_arrayFieldName.SetSize(m_nFields);
m_arrayFieldSize.SetSize(m_nFields);
CDaoFieldInfofi;
for(i= 0;i < m_nFields;i ++)
{
m_pRecordset->GetFieldInfo(i,fi);
m_arrayFieldName[i] = fi.m_strName;
m_arrayFieldSize[i]= (short)max(min(fi.m_lSize,50),fi.m_strName.GetLength());
TRACE("fieldname = %s\n",fi.m_strName);
}
UpdateAllViews(NULL);
EndWaitCursor();
}
void CTestDoc::CloseMdb()
{
m_pRecordset->Close();
m_database.Close();
m_pRecordset= NULL;
m_nRowCount= 0;
m_nFields= 0;
m_nTables= 0;
UpdateAllViews(NULL);
}
void CTestDoc::OnUserNext()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->MoveLast();
GetValue();
}
void CTestDoc::OnUserPrev()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MovePrev();
if(m_pRecordset->IsBOF())
m_pRecordset->MoveFirst();
GetValue();
}
void CTestDoc::OnUserFirst()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MoveFirst();
GetValue();
}
void CTestDoc::OnUserLast()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MoveLast();
GetValue();
}
void CTestDoc::OnUserAdd()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
try
{
m_pRecordset->AddNew();
SetValue();
m_pRecordset->Update();
m_nRowCount++;
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t add the record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserDel()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
if(m_pRecordset->IsBOF()|| m_pRecordset->IsEOF())
return;
try
{
m_pRecordset->Delete();
m_nRowCount--;
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t delete current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
return;
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserEdit()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
try
{
m_pRecordset->Edit();
SetValue();
m_pRecordset->Update();
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t modify current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserRequery()
{
try
{
m_pRecordset->Requery();
UpdateAllViews(NULL);
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t requery current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
}
void CTestDoc::OnUserAddTable()
{
CDaoTableDef*m_newdef;
m_newdef= NULL;
if(createNewTableDef(&m_database,&m_newdef,m_arrayFieldValue[5]))
{
OnUserAddField(m_newdef);
appendTableDef(&m_database,m_newdef);
}
}
void CTestDoc::OnUserAddField(CDaoTableDef*newdef)
{
CDaoFieldInfom_newfieldinfo;
inti;
for(i= 0;i < 3;i ++)
{
m_newfieldinfo.m_strName= _T(m_arrayField[i]);
m_newfieldinfo.m_nType= dbText;
m_newfieldinfo.m_lSize= 100;
m_newfieldinfo.m_strDefaultValue= _T("");
m_newfieldinfo.m_bRequired= FALSE;
m_newfieldinfo.m_lAttributes= dbUpdatableField;
m_newfieldinfo.m_bAllowZeroLength= TRUE;
m_newfieldinfo.m_strValidationRule= _T("");
m_newfieldinfo.m_strValidationText= _T("");
createNewField(newdef,&m_newfieldinfo);
}
}
void CTestDoc::OnUserGetValue()
{
try{
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
if(m_pRecordset->GetAbsolutePosition()== (m_nRows - 1))
{
GetValue();
}
}
catch(CDaoException*e)
{
if(e->m_pErrorInfo->m_lErrorCode== 3167)
AfxMessageBox("***RECORDDELETED***");
else{
m_pRecordset->MoveLast();
}
e->Delete();
}
}
void CTestDoc::GetValue()
{
COleVariantvar;
if(m_arrayFieldValue.GetSize()!= m_nFields)
m_arrayFieldValue.SetSize(m_nFields);
for(inti = 0;i < m_nFields;i ++)
{
var= m_pRecordset->GetFieldValue(i);
switch(var.vt)
{
caseVT_BSTR:
m_arrayFieldValue[i]= (LPCSTR) var.bstrVal; // narrow characters in DAO
break;
caseVT_I2:
m_arrayFieldValue[i].Format("%d",(int) var.iVal);
break;
caseVT_I4:
m_arrayFieldValue[i].Format("%d",var.lVal);
break;
caseVT_R4:
m_arrayFieldValue[i].Format("%10.2f", (double) var.fltVal);
break;
caseVT_R8:
m_arrayFieldValue[i].Format("%10.2f", var.dblVal);
break;
caseVT_CY:
m_arrayFieldValue[i]= COleCurrency(var).Format();
break;
caseVT_DATE:
m_arrayFieldValue[i]= COleDateTime(var).Format();
break;
caseVT_BOOL:
m_arrayFieldValue[i]= (var.boolVal == 0) ? "FALSE" : "TRUE";
break;
caseVT_NULL:
m_arrayFieldValue[i] = "----";
break;
default:
m_arrayFieldValue[i].Format("Unktype %d\n", var.vt);
TRACE("Unknowntype %d\n", var.vt);
}
}
}
void CTestDoc::SetValue()
{
COleVariant tmpVal = COleVariant(m_arrayFieldValue[0],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[0],tmpVal);
tmpVal= COleVariant(m_arrayFieldValue[1],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[1],tmpVal);
tmpVal= COleVariant(m_arrayFieldValue[2],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[2],tmpVal);
if(m_arrayFieldValue.GetSize()> 3)
{
tmpVal= COleVariant(m_arrayFieldValue[3],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[3],tmpVal);
tmpVal= COleVariant(m_arrayFieldValue[4],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[4],tmpVal);
if(m_arrayFieldValue.GetSize()> 5)
{
tmpVal= COleVariant(m_arrayFieldValue[5],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[5],tmpVal);
}
}
}
void CTestDoc::OnInit()
{
}
void CTestDoc::OnCodeChange()
{
BeginWaitCursor();
inti = 0;
if(m_nPurview== 2)
m_strQuery.Format("select* from [%s]", "读者信息");
else
m_strQuery.Format("select* from [%s]", "员工信息");
m_pRecordset= new CDaoRecordset(&m_database);
try{
m_pRecordset->Open(dbOpenDynaset, m_strQuery);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
UpdateAllViews(NULL);
e->Delete();
return;
}
if(!m_pRecordset->IsBOF())
{
m_pRecordset->MoveLast();
}
m_nFields= (int)m_pRecordset->GetFieldCount();
m_arrayFieldName.SetSize(m_nFields);
CDaoFieldInfofi;
for(i= 0;i < m_nFields;i ++)
{
m_pRecordset->GetFieldInfo(i,fi);
m_arrayFieldName[i]= fi.m_strName;
if(fi.m_strName == "ID")
m_nID= i;
if(fi.m_strName== "密码")
m_nCode= i;
}
Search();
EndWaitCursor();
}
void CTestDoc::Search()
{
COleVariantvar;
CStringfield;
m_pRecordset->MoveFirst();
while(!m_pRecordset->IsEOF())
{
GetValue();
if(m_arrayFieldValue[m_nID]== m_strUserID)
{
m_arrayFieldValue[m_nCode]= m_strNewCode;
try
{
m_pRecordset->Edit();
SetValue();
m_pRecordset->Update();
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t modify current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
break;
}
m_pRecordset->MoveNext();
}
}
void CTestDoc::OnDemand()
{
try{
m_pRecordset->SetAbsolutePosition(m_nFieldRow- 1);
if(m_pRecordset->GetAbsolutePosition()== (m_nFieldRow - 1))
{
GetValue();
}
}
catch(CDaoException*e)
{
if(e->m_pErrorInfo->m_lErrorCode== 3167)
AfxMessageBox("***RECORDDELETED***");
else{
m_pRecordset->MoveLast();
}
e->Delete();
}
OnUserAddTable();
}
// testView.cpp : implementation of theCTestView class
//
#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "testView.h"
#include "StudentDlg.h"
#include "StudentThread.h"
#include "AddThread.h"
#include "BookThread.h"
#include "CodeThread.h"
#include "StaffThread.h"
#include "MessageThread.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CStringArray m_arrayFieldName;
extern CStringArray m_arrayFieldValue;
extern CWordArray m_arrayFieldSize;
extern CString m_strDatabase;
extern CString m_strConnect;
extern CString m_strQuery;
extern int m_nRowCount;
extern int m_nFields;
extern int m_nTables;
extern int m_nRows;
extern int m_nPurview;
extern BOOL m_bAccept;
extern int flag;
extern CString m_strInfo;
extern CString m_strReaderID;
int m_nTableNO;
int m_nFieldRow;
HWND hwnd;
2014年11月12日 09点11分
1
#include "test.h"
#include "MainFrm.h"
#include "testDoc.h"
#include "testView.h"
#include "LoadDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern int m_nPurview;
extern BOOL m_bAccept;
/////////////////////////////////////////////////////////////////////////////
// CTestApp
BEGIN_MESSAGE_MAP(CTestApp, CWinApp)
//{{AFX_MSG_MAP(CTestApp)
ON_COMMAND(ID_APP_ABOUT,OnAppAbout)
//NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks ofgenerated code!
//}}AFX_MSG_MAP
//Standard file based document commands
ON_COMMAND(ID_FILE_NEW,CWinApp::OnFileNew)
ON_COMMAND(ID_FILE_OPEN,CWinApp::OnFileOpen)
//Standard print setup command
ON_COMMAND(ID_FILE_PRINT_SETUP,CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestApp construction
CTestApp::CTestApp()
{
//TODO: add construction code here,
//Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CTestApp object
CTestApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CTestApp initialization
BOOL CTestApp::InitInstance()
{
AfxEnableControlContainer();
//Standard initialization
//If you are not using these features and wish to reduce the size
// of your final executable, you should removefrom the following
// the specific initialization routines you donot need.
CLoadDlg dlg;
aa: if(dlg.DoModal()== IDOK)
{
if(!m_bAccept)
{
AfxMessageBox("用户名或密码错误,请重新输入!");
gotoaa;
}
}
else
returnFALSE;
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFCin a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
//Change the registry key under which our settings are stored.
//TODO: You should modify this string to be something appropriate
//such as the name of your company or organization.
SetRegistryKey(_T("LocalAppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (includingMRU)
//Register the application*s document templates. Document templates
// serve as the connection between documents,frame windows and views.
CSingleDocTemplate*pDocTemplate;
pDocTemplate= new CSingleDocTemplate(
IDR_MAINFRAME,
RUNTIME_CLASS(CTestDoc),
RUNTIME_CLASS(CMainFrame), // main SDI frame window
RUNTIME_CLASS(CTestView));
AddDocTemplate(pDocTemplate);
//Parse command line for standard shell commands, DDE, file open
CCommandLineInfocmdInfo;
ParseCommandLine(cmdInfo);
//Dispatch commands specified on the command line
if(!ProcessShellCommand(cmdInfo))
returnFALSE;
//The one and only window has been initialized, so show and update it.
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 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:
virtualvoid DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//No message handlers
//}}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()
// App command to run the dialog
void CTestApp::OnAppAbout()
{
CAboutDlgaboutDlg;
aboutDlg.DoModal();
}
// testDoc.cpp : implementation of theCTestDoc class
//
#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "database.h"
#include "field.h"
#include "index.h"
#include "tabledef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CStringArray m_arrayFieldName;
CStringArray m_arrayFieldValue;
CWordArray m_arrayFieldSize;
CString m_strDatabase;
CString m_strConnect;
CString m_strQuery;
int m_nRowCount;
int m_nFields;
int m_nTables;
int m_nRows = 0;
extern int m_nPurview;
extern CString m_strOldCode;
extern CString m_strUserID;
extern CString m_strNewCode;
extern BOOL m_bAccept;
extern int m_nTableNO;
extern CString m_strReaderID;
extern CString m_strBookNumber;
extern int m_nFieldRow;
int flag;
/////////////////////////////////////////////////////////////////////////////
// CTestDoc
void DaoErrorMsg(CDaoException* e)
{
char errorMsg[301];
wsprintf(errorMsg, "DAO error %d, SOURCE= %s, DESCR = %s",
e->m_pErrorInfo->m_lErrorCode,
(const char*) e->m_pErrorInfo->m_strSource,
(const char*) e->m_pErrorInfo->m_strDescription);
AfxMessageBox(errorMsg);
}
IMPLEMENT_DYNCREATE(CTestDoc, CDocument)
BEGIN_MESSAGE_MAP(CTestDoc, CDocument)
//{{AFX_MSG_MAP(CTestDoc)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDoc construction/destruction
CTestDoc::CTestDoc()
{
m_arrayTableName.SetSize(5);
m_arrayTableName[0]= "读者信息";
m_arrayTableName[1]= "库存图书";
m_arrayTableName[2]= "员工信息";
m_arrayTableName[3]= "库存图书";
m_arrayTableName[4]= "";
m_arrayField.SetSize(3);
m_arrayField[0]= "ID";
m_arrayField[1]= "书名";
m_arrayField[2]= "借阅日期";
}
CTestDoc::~CTestDoc()
{
if(m_database.IsOpen())
CloseMdb();
}
BOOL CTestDoc::OnNewDocument()
{
if(!CDocument::OnNewDocument())
returnFALSE;
ConnectMdb();
returnTRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CTestDoc serialization
void CTestDoc::Serialize(CArchive& ar)
{
if(ar.IsStoring())
{
//TODO: add storing code here
}
else
{
//TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CTestDoc diagnostics
#ifdef _DEBUG
void CTestDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CTestDoc::Dump(CDumpContext& dc)const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTestDoc commands
void CTestDoc::ConnectMdb()
{
intnTables;
intn = 0;
BeginWaitCursor();
try {
// nonexclusive, read-only
m_database.Open(m_strDatabase, FALSE, FALSE);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
EndWaitCursor();
e->Delete();
return;
}
m_strDatabase.Empty();
m_strDatabase = m_database.GetName();
TRACE("databasename = %s, connect = %s\n",
(const char*) m_strDatabase, (const char*) m_strConnect);
nTables= m_database.GetTableDefCount();
CDaoTableDefInfotdi;
for(int i = 0;i < nTables;i ++)
{
m_database.GetTableDefInfo(i,tdi);
TRACE("tablename = %s\n",(constchar*)tdi.m_strName);
if(tdi.m_strName.Left(4) != "MSys") {
m_nTables++;
}
}
tbDef= new CDaoTableDef(&m_database);
EndWaitCursor();
}
void CTestDoc::OpenMdb()
{
BeginWaitCursor();
inti = 0;
m_arrayTableName[4]= m_strReaderID;
m_strQuery.Format("select* from [%s]", m_arrayTableName[m_nTableNO]);
m_pRecordset= new CDaoRecordset(&m_database);
try{
m_pRecordset->Open(dbOpenDynaset, m_strQuery);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
UpdateAllViews(NULL);
e->Delete();
return;
}
if(!m_pRecordset->IsBOF())
{
m_pRecordset->MoveLast();
}
m_nRowCount= m_pRecordset->GetAbsolutePosition() + 2;
TRACE("m_nRowCount= %d\n",m_nRowCount);
m_nFields= (int)m_pRecordset->GetFieldCount();
m_arrayFieldName.SetSize(m_nFields);
m_arrayFieldSize.SetSize(m_nFields);
CDaoFieldInfofi;
for(i= 0;i < m_nFields;i ++)
{
m_pRecordset->GetFieldInfo(i,fi);
m_arrayFieldName[i] = fi.m_strName;
m_arrayFieldSize[i]= (short)max(min(fi.m_lSize,50),fi.m_strName.GetLength());
TRACE("fieldname = %s\n",fi.m_strName);
}
UpdateAllViews(NULL);
EndWaitCursor();
}
void CTestDoc::CloseMdb()
{
m_pRecordset->Close();
m_database.Close();
m_pRecordset= NULL;
m_nRowCount= 0;
m_nFields= 0;
m_nTables= 0;
UpdateAllViews(NULL);
}
void CTestDoc::OnUserNext()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->MoveLast();
GetValue();
}
void CTestDoc::OnUserPrev()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MovePrev();
if(m_pRecordset->IsBOF())
m_pRecordset->MoveFirst();
GetValue();
}
void CTestDoc::OnUserFirst()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MoveFirst();
GetValue();
}
void CTestDoc::OnUserLast()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
m_pRecordset->MoveLast();
GetValue();
}
void CTestDoc::OnUserAdd()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
try
{
m_pRecordset->AddNew();
SetValue();
m_pRecordset->Update();
m_nRowCount++;
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t add the record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserDel()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
if(m_pRecordset->IsBOF()|| m_pRecordset->IsEOF())
return;
try
{
m_pRecordset->Delete();
m_nRowCount--;
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t delete current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
return;
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserEdit()
{
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
try
{
m_pRecordset->Edit();
SetValue();
m_pRecordset->Update();
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t modify current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
UpdateAllViews(NULL);
}
void CTestDoc::OnUserRequery()
{
try
{
m_pRecordset->Requery();
UpdateAllViews(NULL);
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t requery current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
if(flag== 1)
{
m_pRecordset->MoveNext();
if(m_pRecordset->IsEOF())
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
flag= 0;
}
}
void CTestDoc::OnUserAddTable()
{
CDaoTableDef*m_newdef;
m_newdef= NULL;
if(createNewTableDef(&m_database,&m_newdef,m_arrayFieldValue[5]))
{
OnUserAddField(m_newdef);
appendTableDef(&m_database,m_newdef);
}
}
void CTestDoc::OnUserAddField(CDaoTableDef*newdef)
{
CDaoFieldInfom_newfieldinfo;
inti;
for(i= 0;i < 3;i ++)
{
m_newfieldinfo.m_strName= _T(m_arrayField[i]);
m_newfieldinfo.m_nType= dbText;
m_newfieldinfo.m_lSize= 100;
m_newfieldinfo.m_strDefaultValue= _T("");
m_newfieldinfo.m_bRequired= FALSE;
m_newfieldinfo.m_lAttributes= dbUpdatableField;
m_newfieldinfo.m_bAllowZeroLength= TRUE;
m_newfieldinfo.m_strValidationRule= _T("");
m_newfieldinfo.m_strValidationText= _T("");
createNewField(newdef,&m_newfieldinfo);
}
}
void CTestDoc::OnUserGetValue()
{
try{
m_pRecordset->SetAbsolutePosition(m_nRows- 1);
if(m_pRecordset->GetAbsolutePosition()== (m_nRows - 1))
{
GetValue();
}
}
catch(CDaoException*e)
{
if(e->m_pErrorInfo->m_lErrorCode== 3167)
AfxMessageBox("***RECORDDELETED***");
else{
m_pRecordset->MoveLast();
}
e->Delete();
}
}
void CTestDoc::GetValue()
{
COleVariantvar;
if(m_arrayFieldValue.GetSize()!= m_nFields)
m_arrayFieldValue.SetSize(m_nFields);
for(inti = 0;i < m_nFields;i ++)
{
var= m_pRecordset->GetFieldValue(i);
switch(var.vt)
{
caseVT_BSTR:
m_arrayFieldValue[i]= (LPCSTR) var.bstrVal; // narrow characters in DAO
break;
caseVT_I2:
m_arrayFieldValue[i].Format("%d",(int) var.iVal);
break;
caseVT_I4:
m_arrayFieldValue[i].Format("%d",var.lVal);
break;
caseVT_R4:
m_arrayFieldValue[i].Format("%10.2f", (double) var.fltVal);
break;
caseVT_R8:
m_arrayFieldValue[i].Format("%10.2f", var.dblVal);
break;
caseVT_CY:
m_arrayFieldValue[i]= COleCurrency(var).Format();
break;
caseVT_DATE:
m_arrayFieldValue[i]= COleDateTime(var).Format();
break;
caseVT_BOOL:
m_arrayFieldValue[i]= (var.boolVal == 0) ? "FALSE" : "TRUE";
break;
caseVT_NULL:
m_arrayFieldValue[i] = "----";
break;
default:
m_arrayFieldValue[i].Format("Unktype %d\n", var.vt);
TRACE("Unknowntype %d\n", var.vt);
}
}
}
void CTestDoc::SetValue()
{
COleVariant tmpVal = COleVariant(m_arrayFieldValue[0],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[0],tmpVal);
tmpVal= COleVariant(m_arrayFieldValue[1],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[1],tmpVal);
tmpVal= COleVariant(m_arrayFieldValue[2],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[2],tmpVal);
if(m_arrayFieldValue.GetSize()> 3)
{
tmpVal= COleVariant(m_arrayFieldValue[3],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[3],tmpVal);
tmpVal= COleVariant(m_arrayFieldValue[4],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[4],tmpVal);
if(m_arrayFieldValue.GetSize()> 5)
{
tmpVal= COleVariant(m_arrayFieldValue[5],VT_BSTRT);
m_pRecordset->SetFieldValue(m_arrayFieldName[5],tmpVal);
}
}
}
void CTestDoc::OnInit()
{
}
void CTestDoc::OnCodeChange()
{
BeginWaitCursor();
inti = 0;
if(m_nPurview== 2)
m_strQuery.Format("select* from [%s]", "读者信息");
else
m_strQuery.Format("select* from [%s]", "员工信息");
m_pRecordset= new CDaoRecordset(&m_database);
try{
m_pRecordset->Open(dbOpenDynaset, m_strQuery);
}
catch (CDaoException* e) {
::DaoErrorMsg(e);
UpdateAllViews(NULL);
e->Delete();
return;
}
if(!m_pRecordset->IsBOF())
{
m_pRecordset->MoveLast();
}
m_nFields= (int)m_pRecordset->GetFieldCount();
m_arrayFieldName.SetSize(m_nFields);
CDaoFieldInfofi;
for(i= 0;i < m_nFields;i ++)
{
m_pRecordset->GetFieldInfo(i,fi);
m_arrayFieldName[i]= fi.m_strName;
if(fi.m_strName == "ID")
m_nID= i;
if(fi.m_strName== "密码")
m_nCode= i;
}
Search();
EndWaitCursor();
}
void CTestDoc::Search()
{
COleVariantvar;
CStringfield;
m_pRecordset->MoveFirst();
while(!m_pRecordset->IsEOF())
{
GetValue();
if(m_arrayFieldValue[m_nID]== m_strUserID)
{
m_arrayFieldValue[m_nCode]= m_strNewCode;
try
{
m_pRecordset->Edit();
SetValue();
m_pRecordset->Update();
}
catch(CDaoException*e)
{
CStringstrMessage = _T("Couldn*t modify current record--Exception:");
strMessage+= e->m_pErrorInfo->m_strDescription;
AfxMessageBox(strMessage);
e->Delete();
}
break;
}
m_pRecordset->MoveNext();
}
}
void CTestDoc::OnDemand()
{
try{
m_pRecordset->SetAbsolutePosition(m_nFieldRow- 1);
if(m_pRecordset->GetAbsolutePosition()== (m_nFieldRow - 1))
{
GetValue();
}
}
catch(CDaoException*e)
{
if(e->m_pErrorInfo->m_lErrorCode== 3167)
AfxMessageBox("***RECORDDELETED***");
else{
m_pRecordset->MoveLast();
}
e->Delete();
}
OnUserAddTable();
}
// testView.cpp : implementation of theCTestView class
//
#include "stdafx.h"
#include "test.h"
#include "testDoc.h"
#include "testView.h"
#include "StudentDlg.h"
#include "StudentThread.h"
#include "AddThread.h"
#include "BookThread.h"
#include "CodeThread.h"
#include "StaffThread.h"
#include "MessageThread.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CStringArray m_arrayFieldName;
extern CStringArray m_arrayFieldValue;
extern CWordArray m_arrayFieldSize;
extern CString m_strDatabase;
extern CString m_strConnect;
extern CString m_strQuery;
extern int m_nRowCount;
extern int m_nFields;
extern int m_nTables;
extern int m_nRows;
extern int m_nPurview;
extern BOOL m_bAccept;
extern int flag;
extern CString m_strInfo;
extern CString m_strReaderID;
int m_nTableNO;
int m_nFieldRow;
HWND hwnd;