在线等,给足100分

ydl4444 2006-11-14 08:57:22

void CAddRecord::OnOK()
{
// TODO: Add extra validation here
UpdateData();
CString strSex,strFShip;
m_comboSex.GetWindowText(strSex);
m_comboFShip.GetWindowText(strFShip);
if(m_strName=="")
{
MessageBox("请输入姓名");
return;
}
if(m_strAddress==""||m_strAge==""||m_strCPhone==""||
m_strEmail==""||m_strFPhone==""||m_strMPhone==""
||m_strQQ==""||m_strRemark=="")
{
MessageBox("字段不能为空,请输入!");
return;
}
try
{
m_pRS->Open(_variant_t("tb_Phone"),m_pCon.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdTable);
}
catch(_com_error e)
{
MessageBox("记录出错",e.ErrorMessage());
}
m_pRS->AddNew();
if(m_strPictureName!="")
{
CFile file;
if(m_Pic.m_IPicture!=NULL)
{
m_Pic.FreePictureData();
}
if(file.Open(m_strPictureName,CFile::modeRead|CFile::typeBinary))
{
int length=(int)file.GetLength();
BYTE *pbuf=new BYTE[length];
if(file.Read(pbuf,length)>0)
{
SAFEARRAY FAR *pSafeArray = NULL;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = length;
pSafeArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < length; i++)
{
SafeArrayPutElement(pSafeArray, &i, pbuf++);
}
_variant_t varChunk;
varChunk.vt = VT_ARRAY | VT_UI1;
varChunk.parray = pSafeArray;
m_pRS->GetFields()->GetItem("Picture")->AppendChunk(varChunk);

}
}
}
m_pRS->PutCollect("Name",_variant_t(m_strName));
m_pRS->PutCollect("Age",_variant_t(m_strAge));
m_pRS->PutCollect("Sex",_variant_t(strSex));
m_pRS->PutCollect("FShip",_variant_t(strFShip));
m_pRS->PutCollect("Address",_variant_t(m_strAddress));
m_pRS->PutCollect("FPhone",_variant_t(m_strCPhone));
m_pRS->PutCollect("CPhone",_variant_t(m_strCPhone));
m_pRS->PutCollect("MPhone",_variant_t(m_strMPhone));
m_pRS->PutCollect("Email",_variant_t(m_strEmail));
m_pRS->PutCollect("QQ",_variant_t(m_strQQ));
m_pRS->PutCollect("Remark",_variant_t(m_strRemark));

try
{
m_pRS->Update();
m_pRS->Close();
}
catch(_com_error e)
{
AfxMessageBox(e.Description());
}


CDialog::OnOK();
}

void CAddRecord::OnButtonOpenpicture()
{
// TODO: Add your control notification handler code here
static char BASED_CODE szFilter[] = "JPG Files (*.jpg)|*.jpg||";
CFileDialog dlg(TRUE,"BMP",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,szFilter);
CPicture m_Pic;
if(dlg.DoModal()==IDOK)
{
m_strPicturePath=dlg.GetPathName();
m_strPictureName=dlg.GetFileName();
SetDlgItemText(IDC_EDIT_PICTUREPATH,m_strPicturePath);
SetDlgItemText(IDC_STATIC_PICTURENAME,m_strPictureName);
if(m_strPictureName!="")
{
CFile file;
if(m_Pic.m_IPicture!=NULL)
{
m_Pic.FreePictureData();
}
if(file.Open(m_strPictureName,CFile::modeRead|CFile::typeBinary))
{
int length=(int)file.GetLength();
if(length>=0x0001cd11)
{
MessageBox("请选择小于110KB的图片!");
return;
}
BYTE *pbuf=new BYTE[length];


if(file.Read(pbuf,length)>0)
{
m_Pic.LoadPictureData(pbuf,length);
CClientDC dc(this);
dc.SelectStockObject(BLACK_PEN);
dc.Rectangle(420,135,430+136,145+93);
m_Pic.UpdateSizeOnDC(&dc);
m_Pic.Show(&dc,CRect(425,140,425+136,140+93));

}
delete[] pbuf;
pbuf=NULL;
}
file.Close();
}
else
{
MessageBox("请选择图片!");
return;
}

}

}

BOOL CAddRecord::OnInitDialog()
{
CDialog::OnInitDialog();
::CoInitialize(NULL);
// TODO: Add extra initialization here
try{
m_pCon.CreateInstance(__uuidof(Connection));
m_pRS.CreateInstance(__uuidof(Recordset));
m_pCon->ConnectionString="File Name=my_access.udl";
HRESULT hr=m_pCon->Open("","","",adModeUnknown);
if(hr)
{
AfxMessageBox("数据源连接出错!");
}
}
catch(_com_error e)
{
MessageBox(e.Description());
}

return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CAddRecord::OnDestroy()
{
CDialog::OnDestroy();
m_pCon->Close();
::CoUninitialize();
// TODO: Add your message handler code here

}
第一次运行成功,第二次在连接源那里就不行了,我想了很久找不出答案!
大家帮帮忙忙!!
...全文
338 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ronk 2006-11-18
  • 打赏
  • 举报
回复
关注一下先
syy64 2006-11-17
  • 打赏
  • 举报
回复
BOOL CDlgDBConnect::ConnectAccess()
{
if(m_strDatabase.IsEmpty())
{
::MessageBox(NULL,"Êý¾Ý¿â²»ÄÜΪ¿Õ£¡","Ìáʾ",MB_OK);
return false;
}
theApp.m_pConnection.CreateInstance(__uuidof(Connection));
try
{
theApp.m_strDatabase=m_strDatabase;
m_strDatabase.Replace("\\","\\\\");
_bstr_t strConn;
if(m_strPassword.IsEmpty())
{

strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +m_strDatabase;
theApp.m_pConnection->CursorLocation = adUseClient;
theApp.m_pConnection->Open(strConn,
"",
"",
adModeUnknown);
}
else
{
strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +m_strDatabase+";Persist Security Info=False;Jet OLEDB:DataBase Password="+m_strPassword;
theApp.m_pConnection->Open(strConn,
"",
"",
adModeUnknown);
}
}
catch(_com_error e)
{
::MessageBox(NULL,e.Description(),"Ìáʾ", MB_OK);
return FALSE;
}

::MessageBox(NULL,"Êý¾Ý¿âÁ¬½Ó³É¹¦£¡","Ìáʾ", MB_OK);
return TRUE;


}
stone_oo 2006-11-17
  • 打赏
  • 举报
回复
hr = m_pConnection->Open("Provider=Microsoft.Jet,OELDB.4.0;DataSource=name.mdb","","",adModeUnknown);
试下这个,你的连接可能还有点问题
access2000的环境就是用OELDB.4.0
ydl4444 2006-11-17
  • 打赏
  • 举报
回复
这一句:HRESULT hr=m_pCon->Open("","","",adModeUnknown);
但是该打开的,该关闭的我都做了!
找了一些人看代码,也看不出所以然!
syy64 2006-11-17
  • 打赏
  • 举报
回复
第二次就提示“上下文已经关闭”系统强行关闭!
-------------------------------------------------------------
调试看看是由哪一句引起的?
stone_oo 2006-11-16
  • 打赏
  • 举报
回复
还有,操作access的时候,要确定access已经关闭才可以再操作的.你试试看,以前写ASP的时候是这样的,access不关,它老是抱错的,还有就是权限了,不过用vc这方面应该不用管的
stone_oo 2006-11-16
  • 打赏
  • 举报
回复
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");
if(hr==0)
{
hr=m_pCon->Open("","","",adModeUnknown);
}
}
catch(_com_error e) //
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage); //show error message
}
试试看,我用很稳定的
stone_oo 2006-11-16
  • 打赏
  • 举报
回复
if(hr != 0)
ToperRay 2006-11-16
  • 打赏
  • 举报
回复
在适当的地方release和close
lisypro 2006-11-16
  • 打赏
  • 举报
回复
是不是关闭不彻底
ydl4444 2006-11-16
  • 打赏
  • 举报
回复
我看都关了,我用的是VC6.0+ACCESS2000,是不是这样的组合本身存在问题啊,还有,在这个项目的其他类中我又分别建立了一个_ConnectionPtr、和一个CConnectionSet的封装类!这样又是不是有问题呢?如果我不在数据库中写入图片,运行就没有问题!一写入图片,第一次运行成功,第二次就提示“上下文已经关闭”系统强行关闭!
明天我就结帐了,希望小女子能碰到一个能帮我解决的人!
在这里也谢谢楼上各位的热心帮助!!
LiChenYue 2006-11-15
  • 打赏
  • 举报
回复
来蹭分!第一次行的话,也许是缓冲区的问题,清空它!
T97102003 2006-11-15
  • 打赏
  • 举报
回复
直接写连接串
connectionstring = "provider=....
syy64 2006-11-15
  • 打赏
  • 举报
回复
MessageBox(e.Description());
这里面提示什么性质的错误。
syy64 2006-11-15
  • 打赏
  • 举报
回复
数据库打开没有?
ydl4444 2006-11-15
  • 打赏
  • 举报
回复
太平洋,大哥,帮我解决了,立马给分!
ydl4444 2006-11-15
  • 打赏
  • 举报
回复
打不开记录集,接着就是RUNTIME ERROR!
是什么原因呢?

4,012

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 数据库
社区管理员
  • 数据库
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧