记录集打开一个库中的两个表出错

时间:2022-09-30 08:26:38
BOOL C***App::InitInstance()
{
    CString strConnect;
    //创建数据库连接
     try
    {
HRESULT h=m_pConnection.CreateInstance("ADODB.Connection");
strConnect="DRIVER={SQL Server};Server=(local);uid=sa;pwd=;DataBase=ETC;";
if (SUCCEEDED(h))
{
m_pConnection->Open((_bstr_t)strConnect,"","",adModeUnknown);
}
}
catch(_com_error e)
{
CString errormessage;
errormessage.Format("数据库连接失败!错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);
}
}

void C****1View::AddtoSQL()//添加记录正常
{
UpdateData(TRUE);
CString strConnect,strSQL;

CString cardID,tmpstr,freeroadsec;
cardID.Format("%08d",atoi(m_edit_cardID));
freeroadsec.Format("%02d",atoi(m_edit_freeroadsec));


COleDateTime oleDate;// =m_StartDate ;// COleDateTime::GetCurrentTime();
_variant_t vtFld1,vtFld2;
oleDate.SetDateTime(m_startDate.GetYear(),m_startDate.GetMonth(),
m_startDate.GetDay(),m_startDate.GetHour(),m_startDate.GetMinute(),m_startDate.GetSecond());
vtFld1.vt = VT_DATE;
vtFld1.date = oleDate;

oleDate.SetDateTime(m_endDate.GetYear(),m_endDate.GetMonth(),m_endDate.GetDay(),
m_endDate.GetHour(),m_endDate.GetMinute(),m_endDate.GetSecond());
vtFld2.vt = VT_DATE;
vtFld2.date = oleDate;


try
{
strSQL.Format("select * from TABLE1 where CardID= '%s'",m_edit_cardID);
HRESULT h=m_pRecordset.CreateInstance("ADODB.Recordset");
if (SUCCEEDED(h))
{
m_pRecordset->Open(_variant_t(strSQL),_variant_t((IDispatch *)theApp.m_pConnection,true)
,adOpenStatic,adLockOptimistic,adCmdText);
}

if(m_pRecordset->adoEOF)
{
m_pRecordset->AddNew();
m_pRecordset->PutCollect("CardID",_variant_t(cardID));
m_pRecordset->PutCollect("CardType",_variant_t(m_static_cardtype));
m_pRecordset->PutCollect("CardVersionNum",_variant_t(m_static_cardversionnum));
m_pRecordset->PutCollect("UserType",_variant_t(m_edit_usertype));
m_pRecordset->PutCollect("StartTime",vtFld1);
m_pRecordset->PutCollect("EndTime",vtFld2);
m_pRecordset->PutCollect("CardOfFreeRoad",_variant_t(freeroadsec));
m_pRecordset->Update();

}
else
{
sprintf(message,"库中已存在卡此卡记录");
::SendMessage(theApp.m_pMainWnd->m_hWnd,SHOWINFO,1,(LPARAM)message);
}
}

catch(_com_error e)///捕捉异常
{
CString errormessage;
char message[100];
errormessage.Format("创建数据集错误!错误信息:%s",e.ErrorMessage());
sprintf(message,errormessage);
::SendMessage(theApp.m_pMainWnd->m_hWnd,SHOWINFO,1,(LPARAM)message);
}
}

void C***2View::AddtoSQL()            
{
UpdateData(TRUE);

CString strConnect,strSQL;
char PlateNumber[10],vehplatecol[10],vehspecialinfo[100];
CString strtemp;


memset(PlateNumber,0,sizeof(PlateNumber));
strtemp=m_edit_platenumber.GetBuffer(m_edit_platenumber.GetLength());
for (int i=0;i<m_edit_platenumber.GetLength();i++)
{
PlateNumber[i]=strtemp[i];
}

strtemp=m_edit_vehplatecol.GetBuffer(m_edit_vehplatecol.GetLength());
memset(vehplatecol,0,sizeof(vehplatecol));
memset(vehspecialinfo,0,sizeof(vehspecialinfo));
for (i=0;i<m_edit_vehplatecol.GetLength();i++)
{
vehplatecol[i]=strtemp[i];
}

strtemp=m_edit_vehspecialinfo.GetBuffer(m_edit_vehspecialinfo.GetLength());
for (i=0;i<m_edit_vehspecialinfo.GetLength();i++)
{
vehspecialinfo[i]=strtemp[i];
}

COleDateTime oleDate;// =m_StartDate ;// COleDateTime::GetCurrentTime();
_variant_t vtFld1,vtFld2;
oleDate.SetDateTime(m_signDate.GetYear(),m_signDate.GetMonth()
,m_signDate.GetDay(),m_signDate.GetHour(),m_signDate.GetMinute(),
m_signDate.GetSecond());
vtFld1.vt = VT_DATE;
vtFld1.date = oleDate;

oleDate.SetDateTime(m_endDate.GetYear(),m_endDate.GetMonth(),m_endDate.GetDay(),
m_endDate.GetHour(),m_endDate.GetMinute(),m_endDate.GetSecond());
vtFld2.vt = VT_DATE;
vtFld2.date = oleDate;



try
{
strSQL.Format("select * from TABLE2  where ContractSerNum= '%s'",m_edit_contractsernum);
HRESULT h=m_pRecordset1.CreateInstance("ADODB.RecordSet");
if (SUCCEEDED(h))
{
m_pRecordset1->Open(_variant_t(strSQL),_variant_t((IDispatch *)theApp.m_pConnection,true),
adOpenStatic,adLockOptimistic,adCmdText);
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
char message[100];
errormessage.Format("创建数据集错误!错误信息:%s",e.ErrorMessage());
sprintf(message,errormessage);
::SendMessage(theApp.m_pMainWnd->m_hWnd,SHOWINFO,1,(LPARAM)message);

}

if(m_pRecordset1->adoEOF)
{
m_pRecordset1->AddNew();
m_pRecordset1->PutCollect("ContractSerNum",_variant_t(m_edit_contractsernum));
m_pRecordset1->PutCollect("ProviderNum",_variant_t(m_static_providerID));
m_pRecordset1->PutCollect("ContractSignDate",vtFld1);
m_pRecordset1->PutCollect("ContractEndDate",vtFld2);
m_pRecordset1->PutCollect("PlateNumber",_variant_t(PlateNumber));
m_pRecordset1->PutCollect("VehClass",_variant_t(m_edit_vehtype));
m_pRecordset1->PutCollect("VehPlateCol",_variant_t(vehplatecol));
m_pRecordset1->PutCollect("VehDimentions",_variant_t(m_edit_vehdimensions));
m_pRecordset1->PutCollect("WheelsNum",_variant_t(m_edit_wheelsnum));
m_pRecordset1->PutCollect("AxesNum",_variant_t(m_edit_axesnum));
m_pRecordset1->PutCollect("WheelBases",_variant_t(m_edit_wheelsbase));
m_pRecordset1->PutCollect("WeightorSeats",_variant_t(m_edit_limitweight));
m_pRecordset1->PutCollect("UserType",_variant_t(m_edit_usertype));
m_pRecordset1->PutCollect("EngineNumber",_variant_t(m_edit_enginnum));
m_pRecordset1->PutCollect("VehSpecialtyInfo",_variant_t(vehspecialinfo));
m_pRecordset1->Update();

}
else
{
sprintf(message,"库中已存在卡此卡记录");
::SendMessage(theApp.m_pMainWnd->m_hWnd,SHOWINFO,1,(LPARAM)message);
}
theApp.m_pConnection->Close();
theApp.m_pConnection.Release();

}
运行第二的的时候报错,程序终止,请问大家这是为什么呢

2 个解决方案

#1


你所谓的"运行第二的的时候报错"是什么意思?? 说明白点.. 再把报错的信息提供出来...
另外,把你程序中的e.ErrorMessage()改成e.Description()试试..

#2


已搞定,谢谢

#1


你所谓的"运行第二的的时候报错"是什么意思?? 说明白点.. 再把报错的信息提供出来...
另外,把你程序中的e.ErrorMessage()改成e.Description()试试..

#2


已搞定,谢谢