opencv存入数据库图片入门笔记

看的http://blog.csdn.net/hqw7286/article/details/5441000稍有改动

把对数据库的 初始化,获得链接集,执行,退出封装到到一个类中:

头文件:

#pragma once
#include "afx.h"
class CADOConn :public CObject
{
public:
	CADOConn(void);
	~CADOConn(void);
	_RecordsetPtr GetRecordset(_bstr_t bstrSQL);
	BOOL OnInitADOConn();
	BOOL Execute(_bstr_t bstrSQL);
	void ExitConnect(void);
private:
	_ConnectionPtr m_pConnection;
	_RecordsetPtr m_pRecordset;
};
实现文件:
#include "StdAfx.h"
#include "ADOConn.h"

CADOConn::CADOConn(void)
{
}


CADOConn::~CADOConn(void)
{
}


_RecordsetPtr CADOConn::GetRecordset(_bstr_t bstrSQL)
{
	try
	{
		if(m_pConnection==NULL)
			OnInitADOConn();
		m_pRecordset.CreateInstance(__uuidof(Recordset));
		m_pRecordset->Open(bstrSQL,  
			m_pConnection.GetInterfacePtr(),//获取库连接的IDispatch  
			adOpenDynamic,  
			adLockOptimistic,  
			adCmdText);
	}

	catch (_com_error e)
	{
		AfxMessageBox(e.ErrorMessage());
		//return m_pRecordset=NULL;
	}
	return m_pRecordset;
}


BOOL CADOConn::OnInitADOConn()
{
	AfxOleInit();  
	m_pConnection.CreateInstance(__uuidof(Connection));  
		try  
	{  
		m_pConnection->Open("driver={SQL Server}; Server=YuZhiBo-PC;Database=management;UID=sa;PWD=tiger",  
			"","",adModeUnknown);  
	}  
	catch (_com_error* e)  
	{  
		AfxMessageBox(_T("数据库连接失败"));  
		return FALSE;  
	}  
	return TRUE;
}


BOOL CADOConn::Execute(_bstr_t bstrSQL)
{
	try
	{
		if (m_pConnection==NULL)
			OnInitADOConn();
		m_pConnection->Execute(bstrSQL,NULL,adCmdText);
	}
	catch (_com_error e)

	{
		AfxMessageBox(e.ErrorMessage());
		return false;
	}
	return TRUE;
}


void CADOConn::ExitConnect(void)
{
	m_pConnection->Close();
	::CoUninitialize();
}


图片读入数据库:

void CSqlPIcDlg::OnBnClickedInsqlButton()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if(m_name!="")
	{
		CString strSQL;
		CADOConn m_CAdoConn;
		//重新添加一个新的记录
		strSQL=_T("select * from personalmessage");
		m_pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL);

		m_pRecordset->AddNew();
		m_pRecordset->PutCollect((_bstr_t)"name",(_bstr_t)m_name);
		m_pRecordset->PutCollect((_bstr_t)"sex",(_bstr_t)m_sex);
		m_pRecordset->PutCollect((_bstr_t)"student_number",(_bstr_t)m_student_number);
		m_pic1=cvLoadImage("D://SQL//lena.bmp");
		if(m_pic1)
		{
			cvSaveImage("D://SQL//1.bmp",m_pic1);
			//保存在"management"数据库中的"personalmessage"表,字段名"picture"
			CFile f;
			// TODO: Add your control notification handler code here
			CString  FilePathName("D://SQL//1.bmp");
			CFileException e;
			if(f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e))

			{   
				int nSize = f.GetLength();          //先得到文件长度
				BYTE * pBuffer = new BYTE [nSize];  //按文件的大小在堆上申请一块内存
				if (f.Read(pBuffer, nSize) > 0 )    //把文件读到pBuffer(堆上申请一块内存)
				{  
					BYTE *pBuf = pBuffer;     ///下面这一大段是把pBuffer里的数据放到库中
					VARIANT            varBLOB;
					SAFEARRAY     *psa;
					SAFEARRAYBOUND     rgsabound[1];
					if(pBuf)
					{   
						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++);
						varBLOB.vt = VT_ARRAY | VT_UI1;
						varBLOB.parray = psa;
						m_pRecordset->GetFields()->GetItem("picture")->AppendChunk(varBLOB);
					}
					delete [] pBuffer;     //删掉堆上申请的那一块内存
					pBuf=0;                //以防二次乱用
				}
				f.Close();                //这里一定要记得先关闭文件,后面再Remove,否则会出现共享冲突
				CFile::Remove( _T("D://SQL//1.bmp") );
			}
			m_pRecordset->Update();
			m_CAdoConn.ExitConnect();
		}
		MessageBox(L"添加成功");
	}
	else
	{
		MessageBox(L"无信息添加");
	}
	m_student_number=m_name=m_sex="";
	m_pic1=0;
	UpdateData(FALSE);
}

图片由数据库读出到mfc界面:

void CSqlPIcDlg::OnBnClickedOutpicButton()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strSQL=_T("select * from personalmessage");
	CADOConn m_CAdoConn;
	m_pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL);
	long nSize = m_pRecordset->GetFields()->GetItem("picture")->ActualSize;
	if(nSize > 0)
	{
		_variant_t    varBLOB;
		varBLOB = m_pRecordset->GetFields()->GetItem("picture")->GetChunk(nSize);
		if(varBLOB.vt == (VT_ARRAY | VT_UI1))
		{
			if(BYTE *pBuffer = new BYTE [nSize+1])         ///重新申请必要的存储空间
			{  
				char *pBuf = NULL;
				SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
				memcpy(pBuffer,pBuf,nSize);               ///复制数据到缓冲区m_pBMPBuffer
				SafeArrayUnaccessData (varBLOB.parray);
				delete [] pBuffer;
				pBuf=0;
			}
			//输出文件
			_variant_t varChunk;
			HRESULT hr;
			BYTE *pBuf = NULL;  
			pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED,nSize);
			SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
			CFile outFile(L"D://SQL//2.bmp",CFile::modeCreate|CFile::modeWrite);
			LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)pBuf);
			outFile.Write(buffer,nSize);
			GlobalUnlock((HGLOBAL)pBuf);
			outFile.Close();          
			SafeArrayUnaccessData (varBLOB.parray);
		}
		IplImage *img=cvLoadImage("D://SQL//2.bmp");
		DrawPicToHDC(img,IDC_SQLPIC_STATIC);
		CFile::Remove( _T("D://SQL//2.bmp") );
	}
}

void CSqlPIcDlg::DrawPicToHDC(IplImage* img,UINT ID)
{
	CDC *pDC = GetDlgItem(ID)->GetDC();
	HDC hDC= pDC->GetSafeHdc();
	CRect rect;
	GetDlgItem(ID)->GetClientRect(&rect);
	CvvImage cimg;
	cimg.CopyOf(img);
	cimg.DrawToHDC(hDC,&rect); 
	ReleaseDC(pDC);
}




你可能感兴趣的:(SQL)