代码:
COleDateTime t(2000,2,3,0,0,0);
CDateTimeCtrl *dlg;
dlg=(CDateTimeCtrl *)GetDlgItem(IDC_DATETIMEPICKER1);
dlg->SetTime(t);
首先这段代码能正常运行,我新建工程试过,没任何问题
      
但在我的一个ADO工程中就有问题了
        我的那个函数为:
void CADODatabaseDlg::OnBnClickedAlter()
{
// TODO: Add your control notification handler code here
int nSelRows=m_ListCtrl.GetSelectedCount();
if(!nSelRows)
return; // Member.Time=t;
struct Fileds Member;
int nYear,nMonth,nDay;
CString DateTemp;
CString timeTemp;
POSITION pos=m_ListCtrl.GetFirstSelectedItemPosition();
int i=m_ListCtrl.GetNextSelectedItem(pos); //用一个结构体来保存选中的记录
Member.Name=m_ListCtrl.GetItemText(i,0);
Member.Age=(int)atoi((const char *)m_ListCtrl.GetItemText(i,1).GetBuffer(100));
Member.Tel=m_ListCtrl.GetItemText(i,3);
Member.Location=m_ListCtrl.GetItemText(i,4);
Member.Msn=m_ListCtrl.GetItemText(i,5);
Member.Other=m_ListCtrl.GetItemText(i,6);
timeTemp=m_ListCtrl.GetItemText(i,2); sscanf((char *)timeTemp.GetBuffer(100),"%d-%d-%d",&nYear,&nMonth,&nDay); CDlg dlg;
dlg.m_Name=Member.Name;
dlg.m_Age=Member.Age;
// CTime t(nYear,nMonth,nDay,0,0,0);
COleDateTime t(nYear,nMonth,nDay,0,0,0);
try{
dlg.m_Date.SetTime(t);
}
catch (_com_error e){
MessageBox(_T("ERROR"));
}
dlg.m_Tel=Member.Tel;
dlg.m_Location=Member.Location;
dlg.m_Other=Member.Other;
dlg.m_Msn=Member.Msn;
if(dlg.DoModal()==IDOK){
m_pRecordset.CreateInstance(__uuidof(Recordset));
try{
m_pRecordset->Open("SELECT * FROM Star",
m_pConnection.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch (_com_error *e)
{
MessageBox(e->ErrorMessage());
return;
}
if(!m_pRecordset->BOF)
m_pRecordset->MoveFirst(); //在数据库中找出这条记录,并删除

//把更改过的添加进去
//从窗口中读出时间
_variant_t value;
CString strName;
while(!m_pRecordset->adoEOF){
value=m_pRecordset->GetCollect("NAME");
strName=value.bstrVal;
if(strName==Member.Name){
try{
m_pRecordset->Delete(adAffectCurrent);
}
catch(_com_error *e)
{
MessageBox(e->ErrorMessage());
return;
}
break;
}
m_pRecordset->MoveNext();
}
try{
m_pRecordset->AddNew();
m_pRecordset->PutCollect("NAME",_variant_t(dlg.m_Name) );
m_pRecordset->PutCollect("AGE",dlg.m_Age);
m_pRecordset->PutCollect("BIRTHDAY",_variant_t(dlg.m_Date) );
m_pRecordset->PutCollect("TEL",_variant_t(dlg.m_Tel) );
m_pRecordset->PutCollect("LOCATION",_variant_t(dlg.m_Location) );
m_pRecordset->PutCollect("MSN",_variant_t(dlg.m_Msn) );
m_pRecordset->PutCollect("OTHER",_variant_t(dlg.m_Other) );
m_pRecordset->Update();
}
catch (_com_error *e)
{
MessageBox(e->ErrorMessage());
return;
}
}
this->OnBnClickedRefresh();
UpdateData(FALSE);
}运行到代码dlg.m_Date.SetTime(t);就出问题,就是那种无法预知的错误,之后再跟踪就到了下面:
BOOL CDateTimeCtrl::SetTime(const COleDateTime& timeNew)
{
BOOL bRetVal = FALSE; // make sure the time isn't invalid
ASSERT(timeNew.GetStatus() != COleDateTime::invalid);
ASSERT(::IsWindow(m_hWnd));

解决方案 »

  1.   

    sscanf((char *)timeTemp.GetBuffer(100),"%d-%d-%d",&nYear,&nMonth,&nDay);
    感觉你这里有点问题
      

  2.   

    你的m_Date成员应该是CDateTimeCtrl对象,可能你用错了地方,在DoModal调用前用SetTime是会出错的,因为这个时间对话框还没有创建,控件就更没有创建,只是为它分配了内存,所以,你可以在对话框中设一个COleDateTime对象,DoModal之前为其赋值,在对话框的初始化函数OnInitDialog中对m_Date使用SetTime。