在线等,给足100分
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
}
第一次运行成功,第二次在连接源那里就不行了,我想了很久找不出答案!
大家帮帮忙忙!!