由于没有做过这方面的,我上网上找了一个程序,pudn上下载的,用的是vc.net2005开发环境,我用的是VS2008,先转换成VS2008的版本,但发现一运行就死了,后来发现是初始化连接是时候出错了,原代码如下:
BOOL CADOConn::OnInitADOConn(_bstr_t DB_Name)
{
::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->PutCursorLocation(adUseClient);
_bstr_t connectionstring = "Provider=sqloledb;Data Source=";
connectionstring += _T("(LOCAL)");
connectionstring += ";Initial Catalog=";
connectionstring += DB_Name;
connectionstring += ";User Id=sa";
connectionstring += ";Password=82877882";
connectionstring += ";";
m_pConnection->Open(connectionstring,"","",adConnectUnspecified);
/* m_pConnection->ConnectionString="driver={SQL Server};server="";datebase="+DB_Name;
m_pConnection->Open("","","",NULL);*/
}
catch (...)
{
AfxMessageBox(_T("初始化出错"));
return false;
}
return TRUE;
}
原来是Data Source初始化的问题,我把它改成了我SQL服务器的名字也就是WIDOWSXP-CC3F79,问题就解决了。代码如下:
BOOL CADOConn::OnInitADOConn(_bstr_t DB_Name)
{
::CoInitialize(NULL);
try
{
m_pConnection.CreateInstance(__uuidof(Connection));
m_pConnection->PutCursorLocation(adUseClient);
_bstr_t connectionstring = "Provider=sqloledb;Data Source=";
connectionstring += _T("WIDOWSXP-CC3F79");
connectionstring += ";Initial Catalog=";
connectionstring += DB_Name;
connectionstring += ";User Id=sa";
connectionstring += ";Password=82877882";
connectionstring += ";";
m_pConnection->Open(connectionstring,"","",adConnectUnspecified);
/* m_pConnection->ConnectionString="driver={SQL Server};server="";datebase="+DB_Name;
m_pConnection->Open("","","",NULL);*/
}
catch (...)
{
AfxMessageBox(_T("初始化出错"));
return false;
}
return TRUE;
}
现在初始化没问题了,但运行的时候会有一个break,发生在m_pRecordset->Update();这条语句上,然后我打开SQL,发现我表中的第一个字段不允许为空,而我只把第四个字段picture更新了。把第一个字段改成允许为空解决。
图片存储于读取的代码如下:
void CImageTestDlg::OnBnClickedBrowse()
{
//保存在"bm"数据库中的"我的图片"表,字段名"图片"
CString strSQL;
CADOConn m_CAdoConn;
_RecordsetPtr m_pRecordset;
//删除数据库所有记录
strSQL=_T("delete personalmessage");
m_pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL,(_bstr_t)("management"));
//重新添加一个新的记录
strSQL=_T("select * from personalmessage");
m_pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL,(_bstr_t)("management"));
CFile f;
CString FilePathName;
CFileException e;
CFileDialog dlg(TRUE,NULL,NULL,0,NULL/*"jpg Files (*.jpg)|*.jpg||"*/,this);
if(dlg.DoModal()==IDOK)
{
FilePathName=dlg.GetPathName();
if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData();
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];
m_pRecordset->AddNew();
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);
}
m_pRecordset->Update();
(m_Pic.LoadPictureData(pBuffer, nSize));//接作调用函数读pBuffer的数据准备显示
delete [] pBuffer; //删掉堆上申请的那一块内存
pBuf=0; //以防二次乱用
}
m_CAdoConn.ExitConnect();
}
CClientDC dc(this);
m_Pic.UpdateSizeOnDC(&dc);
m_Pic.Show(&dc, CRect(0,50,m_Pic.m_Width,m_Pic.m_Height) );//显示出来
}
}
void CImageTestDlg::OnBnClickedRead()
{
CString strSQL;
CADOConn m_CAdoConn;
_RecordsetPtr m_pRecordset;
strSQL=_T("select * from personalmessage");
m_pRecordset=m_CAdoConn.GetRecordset((_bstr_t)strSQL,(_bstr_t)("management"));
try
{
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);
(m_Pic.LoadPictureData(pBuffer, nSize));
delete [] pBuffer;
pBuf=0;
//显示图片
CClientDC dc(this);
m_Pic.UpdateSizeOnDC(&dc);
m_Pic.Show(&dc, CRect(0,50,m_Pic.m_Width,m_Pic.m_Height) );
}
//输出文件
_variant_t varChunk;
HRESULT hr;
BYTE *pBuf = NULL;
pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED,nSize);
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
CFile outFile("D://aaa.bmp",CFile::modeCreate|CFile::modeWrite);
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)pBuf);
outFile.Write(buffer,nSize);
GlobalUnlock((HGLOBAL)pBuf);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
}
}
catch (_com_error e)
{
}
}