www.pudn.com > 导入xml.rar > MyAdoDlg.cpp
// MyAdoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyAdo.h"
#include "MyAdoDlg.h"
#include "GridDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyAdoDlg dialog
CMyAdoDlg::CMyAdoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyAdoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyAdoDlg)
m_strTel = _T("");
m_strName = _T("");
m_nAge = 0;
m_strSex = _T("");
m_cyMoney = COleCurrency(0, 0);
m_dtDate = COleDateTime::GetCurrentTime();
m_strOther = _T("");
m_fLength = 0.0f;
m_lPostion = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyAdoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyAdoDlg)
DDX_Text(pDX, IDC_EDIT_TEL, m_strTel);
DDX_Text(pDX, IDC_EDIT_NAME, m_strName);
DDX_Text(pDX, IDC_EDIT_AGE, m_nAge);
DDX_CBString(pDX, IDC_COMBO_SEX, m_strSex);
DDX_Text(pDX, IDC_EDIT_MONEY, m_cyMoney);
DDX_DateTimeCtrl(pDX, IDC_DATETIMEPICKER_BORTH, m_dtDate);
DDX_Text(pDX, IDC_EDIT_OTHER, m_strOther);
DDX_Text(pDX, IDC_EDIT_LEGTH, m_fLength);
DDX_Text(pDX, IDC_EDIT_POSTION, m_lPostion);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyAdoDlg, CDialog)
//{{AFX_MSG_MAP(CMyAdoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_BACK, OnButtonBack)
ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
ON_BN_CLICKED(IDC_BUTTON_FORWARD, OnButtonForward)
ON_BN_CLICKED(IDC_BUTTON_UPDATEPHOTO, OnButtonUpdatephoto)
ON_BN_CLICKED(IDC_BUTTON_CANCELUPDATE, OnButtonCancelupdate)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
ON_BN_CLICKED(IDC_BUTTON_NEW, OnButtonNew)
ON_BN_CLICKED(IDC_BUTTON_BOOKMARK, OnButtonBookmark)
ON_BN_CLICKED(IDC_BUTTON_GOBOOKMARK, OnButtonGobookmark)
ON_BN_CLICKED(IDC_PASS, OnPass)
ON_BN_CLICKED(IDC_SORT, OnSort)
ON_BN_CLICKED(IDC_BUTTON_IMPORT, OnButtonImport)
ON_BN_CLICKED(IDC_BUTTON_EXPORT, OnButtonExport)
ON_BN_CLICKED(IDC_BUTTON_UPDATEINFO, OnButtonUpdateinfo)
ON_BN_CLICKED(IDC_BUTTON_BACK2, OnButtonBack2)
ON_BN_CLICKED(IDC_BUTTON_FORWARD2, OnButtonForward2)
ON_BN_CLICKED(IDC_BUTTON_UPDATEFIELDINFO, OnButtonUpdatefieldinfo)
ON_BN_CLICKED(IDC_BUTTON_UPDATECONNECT, OnButtonUpdateconnect)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_FIND, OnFind)
ON_BN_CLICKED(IDC_FINDNEXT, OnFindnext)
ON_BN_CLICKED(IDC_GRID, OnGrid)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyAdoDlg message handlers
BOOL CMyAdoDlg::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
if (m_adoConnection.ConnectAccess("database.mdb"))
{
m_adoRecordSet.SetAdoConnection(&m_adoConnection);
m_adoRecordSet.SetCursorLocation();
m_adoRecordSet.Open("ado", adCmdTable);
m_adoConnection.BeginTrans();
UpdateRecord(TRUE);
OnButtonUpdatefieldinfo();
}
else
{
MessageBox("数据库连接失败!");
}
return TRUE; // return TRUE unless you set the focus to a control
}
// 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 CMyAdoDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
if (IsIconic())
{
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();
}
if (m_Bitmap.GetSafeHandle() != NULL)
{
CDC memDC;
BITMAP bm;
m_Bitmap.GetBitmap(&bm);
memDC.CreateCompatibleDC(&dc);
CBitmap *oldbitmap = memDC.SelectObject(&m_Bitmap);
dc.BitBlt(370, 100, 130, 160, &memDC, 0, 0, SRCCOPY);
memDC.SelectObject(oldbitmap);
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMyAdoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CMyAdoDlg::UpdateRecord(BOOL bLoad)
{
if (m_adoRecordSet.GetRecordCount() < 1) return FALSE;
if (bLoad)
{
int Sex;
m_adoRecordSet.GetCollect("性别", Sex);
m_strSex = (Sex == -1) ? "男" : "女";
m_Bitmap.DeleteObject();
if (!m_adoRecordSet.GetCollect("姓名", m_strName)
|| !m_adoRecordSet.GetCollect("年龄", m_nAge)
|| !m_adoRecordSet.GetCollect("身高", m_fLength)
|| !m_adoRecordSet.GetCollect("出生年月", m_dtDate)
|| !m_adoRecordSet.GetCollect("总资产", m_cyMoney)
|| !m_adoRecordSet.GetCollect("电话", m_strTel)
|| !m_adoRecordSet.GetChunk("玉照", m_Bitmap))
{
;
}
CRect rect(370, 100, 500, 260);
InvalidateRect(rect);
m_lPostion = m_adoRecordSet.GetAbsolutePosition();
UpdateData(FALSE);
return TRUE;
}
else
{
if (!UpdateData()) return FALSE;
int bSex = 0;
if (m_strSex == "男") bSex = -1;
if (!m_adoRecordSet.PutCollect("年龄", m_nAge)
|| !m_adoRecordSet.PutCollect("身高", m_fLength)
|| !m_adoRecordSet.PutCollect("姓名", m_strName)
|| !m_adoRecordSet.PutCollect("电话", m_strTel)
|| !m_adoRecordSet.PutCollect("总资产", m_cyMoney)
|| !m_adoRecordSet.PutCollect("出生年月", m_dtDate)
|| !m_adoRecordSet.PutCollect("性别", bSex))
{
AfxMessageBox("Error!");
}
return TRUE;
}
}
void CMyAdoDlg::OnButtonBack()
{
try
{
if (m_adoRecordSet.IsBOF()) return;
m_adoRecordSet.MovePrevious();
if (m_adoRecordSet.IsBOF())
{
if (!m_adoRecordSet.IsEOF())
{
m_adoRecordSet.MoveLast();
}
}
m_strPathName = "";
UpdateRecord();
}
catch (...)
{
}
}
void CMyAdoDlg::OnButtonUpdate()
{
if (UpdateRecord(FALSE))
{
m_strPathName = "";
if (!m_adoRecordSet.Update())
{
AfxMessageBox("更新失败,请正确填写相应的信息!");
}
}
}
void CMyAdoDlg::OnButtonForward()
{
if (m_adoRecordSet.IsEOF()) return;
m_adoRecordSet.MoveNext();
if (m_adoRecordSet.IsEOF())
{
if (!m_adoRecordSet.IsBOF())
{
m_adoRecordSet.MoveFirst();
}
}
m_strPathName = "";
UpdateRecord();
}
void CMyAdoDlg::OnOK()
{
m_adoConnection.CommitTrans();
CDialog::OnOK();
}
void CMyAdoDlg::OnButtonUpdatephoto()
{
if (m_adoRecordSet.GetRecordCount() < 1) return ;
static char BASED_CODE szFilter[] = "BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*||";
CFileDialog dlg(TRUE,"BMP",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if (dlg.DoModal() == IDOK)
{
m_strPathName = dlg.GetPathName();
if (m_strPathName != "")
{
if (m_adoRecordSet.AppendChunk("玉照", LPCTSTR(m_strPathName)))
{
CFile file;
if (file.Open(LPCTSTR(m_strPathName), CFile::modeRead))
{
long length = (long)file.GetLength();
char *pdata = new char[length + 1];
if (file.Read(pdata, length) == (DWORD)length)
{
BITMAPFILEHEADER bmfHeader;
DWORD bmfHeaderLen = sizeof(bmfHeader);
strncpy((LPSTR)&bmfHeader, (LPSTR)pdata, bmfHeaderLen);
if (bmfHeader.bfType == (*(WORD*)"BM") )
{
LPSTR lpDIB = pdata + bmfHeaderLen;
BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)lpDIB;
BITMAPINFO &bmInfo = *(LPBITMAPINFO)lpDIB;
LPVOID lpDIBBits = pdata + ((BITMAPFILEHEADER *)pdata)->bfOffBits;
CWindowDC dc(this);
HBITMAP hBmp = CreateDIBitmap(dc.m_hDC, &bmiHeader, CBM_INIT, lpDIBBits, &bmInfo, DIB_RGB_COLORS);
if (m_Bitmap.GetSafeHandle() != NULL) m_Bitmap.DeleteObject();
m_Bitmap.Attach(hBmp);
Invalidate();
}
}
delete[] pdata;
pdata = NULL;
}
} // if (m_adoRecordSet.AppendChun
} // if (m_strPathName != "")
}
Invalidate();
}
void CMyAdoDlg::OnCancel()
{
m_adoConnection.RollbackTrans();
CDialog::OnCancel();
}
void CMyAdoDlg::OnButtonCancelupdate()
{
m_adoRecordSet.CancelUpdate();
}
void CMyAdoDlg::OnButtonDelete()
{
m_adoRecordSet.Delete();
OnButtonForward();
}
void CMyAdoDlg::OnButtonNew()
{
CAdoCommand cmd;
m_adoRecordSet.AddNew();
UpdateRecord();
}
void CMyAdoDlg::OnButtonBookmark()
{
m_adoRecordSet.GetBookmark();
}
void CMyAdoDlg::OnButtonGobookmark()
{
m_adoRecordSet.SetBookmark(m_adoRecordSet.m_varBookmark);
// m_adoRecordSet.Move(1);
UpdateRecord();
}
void CMyAdoDlg::OnPass()
{
UpdateData();
m_adoRecordSet.SetFilter(LPCTSTR(m_strOther));
}
void CMyAdoDlg::OnSort()
{
UpdateData();
m_adoRecordSet.SetSort(LPCTSTR(m_strOther));
}
void CMyAdoDlg::OnButtonImport()
{
static char BASED_CODE szFilter[] = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||";
CFileDialog dlg(TRUE,"XML",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if (dlg.DoModal() == IDOK)
{
CString strfilename = dlg.GetPathName();
m_adoRecordSet.Load(LPCTSTR(strfilename));
UpdateRecord();
}
}
void CMyAdoDlg::OnButtonExport()
{
static char BASED_CODE szFilter[] = "XML Files (*.xml)|*.xml|All Files (*.*)|*.*||";
CFileDialog dlg(FALSE,"XML",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
if (dlg.DoModal() == IDOK)
{
CString strfilename = dlg.GetPathName();
m_adoRecordSet.Save(LPCTSTR(strfilename));
}
}
void CMyAdoDlg::OnButtonUpdateinfo()
{
CString strText;
strText.Format("[状态: 0x%x ] ", m_adoRecordSet.GetState());
m_strInfo = strText + "\r\n 0: adStateClosed 指示 Recordset 对象为关闭状态"
"\r\n 1: adStateOpen 指示 Recordset 对象为打开状态"
"\r\n 2: adStateConnecting 指示 Recordset 对象正在连接. "
"\r\n 4: adStateExecuting 指示 Recordset 对象正在执行命令. "
"\r\n 8: adStateFetching 指示 Recordset 对象的行正在被读取.";
strText.Format("\r\n\r\n[状态2: 0x%x ] ", m_adoRecordSet.GetStatus());
m_strInfo += strText;
strText.Format("\r\n\r\n[每页记录数: %d ] ", m_adoRecordSet.GetPageSize());
m_strInfo += strText;
strText.Format("\r\n\r\n[总页数: %d ] ", m_adoRecordSet.GetPageCount());
m_strInfo += strText;
strText.Format("\r\n\r\n[记录数目: %d ] ", m_adoRecordSet.GetRecordCount());
m_strInfo += strText;
strText.Format("\r\n\r\n[字段数目: %d ]", m_adoRecordSet.GetFieldsCount());
m_strInfo += strText;
strText.Format("\r\n\r\n[是否记录集头: %d ] ", m_adoRecordSet.IsEOF());
m_strInfo += strText;
strText.Format("\r\n\r\n[是否记录集尾: %d ] ", m_adoRecordSet.IsBOF());
m_strInfo += strText;
strText.Format("\r\n\r\n[当前页: %d ] ", m_adoRecordSet.GetAbsolutePage());
m_strInfo += strText;
strText.Format("\r\n\r\n[当前位置: %d ] ", m_adoRecordSet.GetAbsolutePosition());
m_strInfo += strText + "\r\n -1: adPosUnknown Recordset 为空,当前位置未知,或者提供者不支持 AbsolutePage 属性。 "
"\r\n -2: adPosBOF 当前记录指针位于 BOF(即 BOF 属性为 True)。 "
"\r\n -3: adPosEOF 当前记录指针位于 EOF(即 EOF 属性为 True)。";
strText.Format("\r\n\r\n[光标位置: %d ] ", m_adoRecordSet.GetCursorLocation());
m_strInfo += strText + "\r\n 1: adUseNone 没有使用游标服务。(该常量已过时并且只为了向后兼容才出现)。 "
"\r\n 3: adUseClient 使用由本地游标库提供的客户端游标。本地游标服务通常允许使用的许多功能可能是驱动程序提供的游标无法使用的,因此使用该设置对于那些将要启用的功能是有好处的。AdUseClient 具有向后兼容性,也支持同义的 adUseClientBatch。 "
"\r\n 2: adUseServer 默认值。使用数据提供者的或驱动程序提供的游标。这些游标有时非常灵活,对于其他用户对数据源所作的更改具有额外的敏感性。但是,Microsoft Client Cursor Provider(如已断开关联的记录集)的某些功能无法由服务器端游标模拟,通过该设置将无法使用这些功能。 ";
strText.Format("\r\n\r\n[光标类型: %d ] ", m_adoRecordSet.GetCursorType());
m_strInfo += strText + "\r\n-1: adOpenUnspecified"
"\r\n 0: adOpenForwardOnly 仅向前游标,默认值。除了只能在记录中向前滚动外,与静态游标相同。当只需要在记录集中单向移动时,使用它可提高性能。"
"\r\n 1: adOpenKeyset 键集游标。尽管从您的记录集不能访问其他用户删除的记录,但除无法查看其他用户添加的记录外,键集游标与动态游标相似。仍然可以看见其他用户更改的数据。"
"\r\n 2: adOpenDynamic 动态游标。可以看见其他用户所作的添加、更改和删除。允许在记录集中进行所有类型的移动,但不包括提供者不支持的书签操作。"
"\r\n 3: adOpenStatic = 3;静态游标。可以用来查找数据或生成报告的记录集合的静态副本。另外,对其他用户所作的添加、更改或删除不可见。";
strText.Format("\r\n\r\n[最大记录数: %d ] ", m_adoRecordSet.GetMaxRecordCount());
m_strInfo += strText + "\r\n 0 表示没有限制";
strText.Format("\r\n\r\n[编辑模试: %d ] ", m_adoRecordSet.GetEditMode());
m_strInfo += strText + "\r\n 0: adEditNone 指示当前没有编辑操作"
"\r\n 1: adEditInProgress 指示当前记录中的数据已被修改但未保存"
"\r\n 2: adEditAdd 指示 AddNew 方法已被调用,且复制缓冲区中的当前记录是尚未保存到数据库中的新记录"
"\r\n 3: adEditDelete 指示当前记录已被删除";
GetDlgItem(IDC_DBINFO)->SetWindowText(m_strInfo);
}
void CMyAdoDlg::OnButtonBack2()
{
if (m_adoRecordSet.IsEOF() && m_adoRecordSet.IsBOF()) return;
m_adoRecordSet.MoveFirst();
m_strPathName = "";
UpdateRecord();
}
void CMyAdoDlg::OnButtonForward2()
{
if (m_adoRecordSet.IsEOF() && m_adoRecordSet.IsBOF()) return;
m_adoRecordSet.MoveLast();
m_strPathName = "";
UpdateRecord();
}
void CMyAdoDlg::OnButtonUpdatefieldinfo()
{
CString strText;
m_strInfo = " 字段名 定义长度 实际长度 属性 数据类型\r\n";
m_strInfo += "-----------------------------------------------------------------------\r\n";
for (long i = 0; i < m_adoRecordSet.GetFieldsCount(); i++)
{
strText.Format("%12s", m_adoRecordSet.GetFieldName(i));
m_strInfo += strText;
strText.Format("%12d ", m_adoRecordSet.GetFieldDefineSize(i));
m_strInfo += strText;
strText.Format("%12d", m_adoRecordSet.GetFieldActualSize(i));
m_strInfo += strText;
strText.Format("%12x", m_adoRecordSet.GetFieldAttributes(i));
m_strInfo += strText;
strText.Format("%12d", m_adoRecordSet.GetFieldType(i));
m_strInfo += strText;
m_strInfo += "\r\n";
}
m_strInfo += "\r\n 各字段的值\r\n[字段名] - [通过GetCollect方法取得的值] - [字段实际值] - [字段数据类型]:";
long count = m_adoRecordSet.GetFieldsCount();
int lvalue = 0;
m_strInfo += "\r\n [整型值]:-----------------------------------------";
for (i = 0; i < count; i++)
{
if (!m_adoRecordSet.GetCollect(i, lvalue)) lvalue = 0;
strText.Format("\r\n%8s: %20d", m_adoRecordSet.GetFieldName(i), lvalue);
m_strInfo += strText;
_variant_t vt;
try
{
vt = m_adoRecordSet.GetRecordset()->GetCollect(_variant_t(i));
lvalue = long(vt);
strText.Format("%20d", lvalue);
m_strInfo += " : " + strText;
}
catch (...)
{
}
strText.Format(" 变体值类型(VT_...): %5d", vt.vt);
m_strInfo += strText;
}
double dbvalue = 0;
m_strInfo += "\r\n [双精度值]:-----------------------------------------";
for (i = 0; i < count; i++)
{
try
{
if (!m_adoRecordSet.GetCollect(i, dbvalue)) dbvalue = 0;
strText.Format("\r\n%8s: %16.4f", m_adoRecordSet.GetFieldName(i), dbvalue);
m_strInfo += strText;
_variant_t vt = m_adoRecordSet.GetRecordset()->GetCollect(_variant_t(i));
dbvalue = double(vt);
strText.Format("%16.4f", dbvalue);
m_strInfo += " : " + strText;
strText.Format(" 变体值类型(VT_...): %5d", vt.vt);
m_strInfo += strText;
}
catch (...)
{
}
}
CString strvalue = "";
m_strInfo += "\r\n [字符串]:-----------------------------------------";
for (i = 0; i < count; i++)
{
try
{
if (!m_adoRecordSet.GetCollect(i, strvalue)) strvalue = "";
strText.Format("\r\n%8s: %20s", m_adoRecordSet.GetFieldName(i), strvalue);
m_strInfo += strText;
_variant_t vt = m_adoRecordSet.GetRecordset()->GetCollect(_variant_t(i));
strvalue = LPCTSTR(_bstr_t(vt));
strText.Format("%20s", strvalue);
m_strInfo += " : " + strText;
strText.Format(" 变体值类型(VT_...): %5d", vt.vt);
m_strInfo += strText;
}
catch (...)
{
}
}
GetDlgItem(IDC_DBINFO)->SetWindowText(m_strInfo);
}
void CMyAdoDlg::OnButtonUpdateconnect()
{
CString strText;
m_strInfo = _T("数据库引擎名称: ") + m_adoConnection.GetProviderName() + "\r\n";
m_strInfo += _T("ADO版本: ") + m_adoConnection.GetVersion() +"\r\n";
strText.Format("Mode: 0x%x\r\n", m_adoConnection.GetMode());
m_strInfo += strText;
strText.Format("State: 0x%x\r\n", m_adoConnection.GetState());
m_strInfo += strText;
strText.Format("TimeOut: %d\r\n", m_adoConnection.GetConnectTimeOut());
m_strInfo += strText;
GetDlgItem(IDC_DBINFO)->SetWindowText(m_strInfo);
}
void CMyAdoDlg::OnReset()
{
if (!m_adoRecordSet.Resync())
{
AfxMessageBox("_adoRecordSet");
}
m_strPathName = "";
UpdateRecord();
}
void CMyAdoDlg::OnFind()
{
UpdateData();
if (m_adoRecordSet.Find(LPCTSTR(m_strOther)))
UpdateRecord();
}
void CMyAdoDlg::OnFindnext()
{
UpdateData();
if (m_adoRecordSet.FindNext())
UpdateRecord();
}
void CMyAdoDlg::OnGrid()
{
CGridDlg dlg;
dlg.m_adoRecordSet = &m_adoRecordSet;
dlg.DoModal();
}