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

📄 test1dlg.cpp

📁 用VC++ 6.0在SQL server中添加JPG格式图片
💻 CPP
字号:
// test1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "test1.h"
#include "test1Dlg.h"
#include "ADO.h"
#include "Picture.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()

/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg dialog

CTest1Dlg::CTest1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest1Dlg)
	m_jpgid = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTest1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest1Dlg)
	DDX_Text(pDX, IDC_EDIT1, m_jpgid);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest1Dlg, CDialog)
	//{{AFX_MSG_MAP(CTest1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest1Dlg message handlers

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

void CTest1Dlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);

	CFile f;
	CString  FilePathName;
	CFileException e;
	CADO m_ado;

	CString strtemp;
	strtemp.Format("insert into yes_employ(id) values('%s')",m_jpgid);
//	strVQL.Format("insert into employee(id) values('%s')",m_jpgid);
	
	if(m_jpgid!="")
	{
	VARIANT varChunk;
	SAFEARRAY *psa;
	SAFEARRAYBOUND rgsabound[1];

	m_ado.OnInitADOConn();
	m_ado.ExecuteSQL(strtemp);
	strtemp.Format("select * from yes_employ where id=%s",m_jpgid);
	m_ado.m_pRecordset.CreateInstance("ADODB.Recordset");
	m_ado.m_pRecordset->Open((_variant_t)strtemp,_variant_t((IDispatch*) m_ado.m_pConnection),adOpenStatic,adLockOptimistic,adCmdText);



	CFileDialog dlg(TRUE,NULL,NULL,0,"jpg Files (*.jpg)|*.jpg||",this);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
	if(dlg.DoModal()==IDOK)
	{
		FilePathName=dlg.GetPathName();
	}
//	MessageBox(FilePathName);
		if(FilePathName!="")
		{

		f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e);
		int nSize = f.GetLength();
		BYTE * pBuffer = new BYTE [nSize];

		f.Read(pBuffer, nSize);
//	if(pBuffer)
//		MessageBox("husuhjiosdfhj");
	
		BYTE * pBuf = pBuffer;
	
		if(pBuf)
		{
//		MessageBox("husuhjiosdfhj");
			rgsabound[0].lLbound = 0;
			rgsabound[0].cElements = nSize;
			psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
			for (long i = 0; i < (long)nSize; i++)
				SafeArrayPutElement (psa, &i, pBuf++);
			varChunk.vt = VT_ARRAY | VT_UI1;
			varChunk.parray = psa;
			m_ado.m_pRecordset->GetFields()->GetItem("id_image")->AppendChunk(varChunk);


		}
		m_ado.m_pRecordset->Update();

		delete []pBuffer;
	
		f.Close();
		}
	m_ado.ExitConnect();
	}
	m_jpgid.Empty();

	UpdateData(false);
}

void CTest1Dlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CADO m_ado;
	CPicture m_Pic;

	CString strtemp;

	strtemp.Format("SELECT * FROM yes_employ where id=%s",m_jpgid);
	
	
	if(m_jpgid!="")
	{

		m_ado.OnInitADOConn();
		m_ado.m_pRecordset.CreateInstance("ADODB.Recordset");
		m_ado.m_pRecordset->Open((_variant_t)strtemp,_variant_t((IDispatch*) m_ado.m_pConnection),adOpenStatic,adLockOptimistic,adCmdText);

		long nSize = m_ado.m_pRecordset->GetFields()->GetItem("id_image")->ActualSize;
		if(nSize>0)
		{
			_variant_t varBLOB;
			varBLOB = m_ado.m_pRecordset->GetFields()->GetItem("id_image")->GetChunk(nSize);
			if(varBLOB.vt == (VT_ARRAY | VT_UI1))
			{
				if(BYTE *pBuffer = new BYTE [nSize+1])
				{
					void * pBuf = NULL;
					SafeArrayAccessData(varBLOB.parray,(void ** )&pBuf);
					memcpy(pBuffer,pBuf,nSize);				
					SafeArrayUnaccessData (varBLOB.parray);
					if(m_Pic.m_IPicture != NULL) 
						m_Pic.FreePictureData();
					m_Pic.LoadPictureData(pBuffer, nSize);
	


					delete []pBuffer;
					pBuf=0;

	/*				CClientDC dc(this); //定义初始化设备变量

					m_Pic.UpdateSizeOnDC(&dc); // Allocate memory for m_Pic
					m_Pic.Show(&dc,CRect(0,0,200,200)); */ 

					CClientDC dc(this);
					m_Pic.UpdateSizeOnDC(&dc);    // Get Picture Dimentions In Pixels
					m_Pic.Show(&dc, CRect(50,50,500,500) );
				}
			}
		}
		m_ado.ExitConnect();
//		m_pRecordset->Close();
	
	}
	m_jpgid.Empty();
	UpdateData(false);
}


⌨️ 快捷键说明

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