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

📄 myadodlg.cpp

📁 这是我改写的把jpg图片存放到数据库的源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 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();
	}
	

	CRect rc;
	GetDlgItem(IDC_STA1)->GetClientRect(&rc);
	CClientDC dcphoto(GetDlgItem(IDC_STA1));        
	m_Pic.UpdateSizeOnDC(&dcphoto); // Get Picture Dimentions In Pixels
	m_Pic.Show(&dcphoto, rc );


}

// 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_Pic.m_IPicture != NULL) m_Pic.FreePictureData();
	if (m_adoRecordSet.GetRecordCount() < 1) return FALSE;
	if (bLoad)
	{
		int Sex;
		m_adoRecordSet.GetCollect("性别", Sex);
		m_strSex = (Sex == -1) ? "男" : "女";
		
		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);
		long nSize=m_adoRecordSet.GetFieldActualSize("玉照");
		BYTE *pBuffer = new BYTE [nSize+1];
		m_adoRecordSet.GetChunk("玉照",(LPVOID) pBuffer);
		m_Pic.LoadPictureData(pBuffer, nSize);
		
		CRect rc;
		GetDlgItem(IDC_STA1)->GetClientRect(&rc);
		CClientDC dc(GetDlgItem(IDC_STA1));        
		
		InvalidateRect(rc);
		m_lPostion = m_adoRecordSet.GetAbsolutePosition();
		UpdateData(FALSE);
		delete []pBuffer;
		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[] = "JPG Files (*.jpg)|*.jpg|All Files (*.*)|*.*||";
	CFileDialog dlg(TRUE,"JPG",NULL,  OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
	if (dlg.DoModal() == IDOK)
	{
		m_strPathName = dlg.GetPathName();
		if (m_strPathName != "")
		{
			if (m_adoRecordSet.AppendChunk("玉照", LPCTSTR(m_strPathName)))
			{ if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData();  
				CFile file;
				if (file.Open(LPCTSTR(m_strPathName), CFile::modeRead))
				{
					long	 nSize = (long)file.GetLength();
					BYTE*	pBuffer = new BYTE[nSize + 1];
					if (file.Read(pBuffer, nSize) == (DWORD)nSize)
					{
						m_Pic.LoadPictureData(pBuffer, nSize);
						
					}
					delete[] pBuffer;
					pBuffer = NULL;
					
				}
				file.Close();	
			}
			
		} 
	} 
	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();
		//if (m_adoRecordSet.IsOpen()) 
	//	{m_adoRecordSet.Close();}
			CAdoConnection m_adoConnection1;
	CAdoRecordSet  m_adoRecordSet1;
		m_adoRecordSet1.Load(LPCTSTR(strfilename));
		CString strName;
	 int nAge;
	 float fLength;
	 COleCurrency	 cyMoney;
	COleDateTime	 dtDate;
	CString	strTel;
	CBitmap Bitmap;
     for(int i=1;i<=m_adoRecordSet1.GetRecordCount();i++)
	 {
		 m_adoRecordSet1.GetCollect("姓名", strName);
		 m_adoRecordSet1.GetCollect("年龄", nAge);
		 m_adoRecordSet1.GetCollect("身高", fLength);
		 m_adoRecordSet1.GetCollect("出生年月", dtDate);
		 m_adoRecordSet1.GetCollect("总资产", cyMoney);
		 m_adoRecordSet1.GetCollect("电话", strTel);
		 
		 long nSize=m_adoRecordSet1.GetFieldActualSize("玉照");
		BYTE *pBuffer = new BYTE [nSize+1];
		m_adoRecordSet1.GetChunk("玉照",(LPVOID) pBuffer);
		 
		 
		 
		 m_adoRecordSet1.MoveNext ();
		 m_adoRecordSet.AddNew();
		 m_adoRecordSet.PutCollect("姓名", strName);
		 m_adoRecordSet.PutCollect("年龄", nAge);
		 m_adoRecordSet.PutCollect("身高", fLength);
		 m_adoRecordSet.PutCollect("出生年月", dtDate);
		 m_adoRecordSet.PutCollect("总资产", cyMoney);
		 m_adoRecordSet.PutCollect("电话", strTel);
		
		 
		 
		 m_adoRecordSet.AppendChunk("玉照",(LPVOID) pBuffer,nSize);
		 m_adoRecordSet.Update();
	 delete []pBuffer;

⌨️ 快捷键说明

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