各位朋友好,我遇到的问题是:我在wince5.0中用ado访问sql ce数据库,平时连接数据库、读写数据库都没问题,我的程序是定时3秒写一个70个字段的数据,当我反复开关设备时(我们的设备是硬关机,即直接关电源,没有关机键),在一定次数后再打开程序,程序无法连接数据库了,open的返回代码是-2147467259,将数据库文件*.sdf用备份的换掉后正常,不知是什么原因,请各位大侠执教。我怀疑是在写数据库的过程中断电了,使写数据的过程没有完成,造成sdf文件损坏,但不知道怎么解决,能否通过优化写数据的方法或其它设置解决此问题?我们的设备只能硬关机。现将代码贴上来,请高人帮我分析一下。万分感谢。
数据库连接:
m_ProgID = _T("ADOCE.Connection.3.1");
m_bInit = FALSE;
//对本地数据库创建连接
m_strDataSource = "Provider = Microsoft.SQLSERVER.OLEDB.CE.2.0;data source = HardDisk\\DataBase\\CanShuYi.sdf";
if(!m_bInit)
{
if(!Initialize())
{
MessageBox(NULL,_T("Database connection create failed!"),_T("Warning"),MB_OK|MB_ICONERROR);
}
}
if(FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
return FALSE;
HRESULT hr;
hr = CLSIDFromProgID(m_ProgID, &m_ClsID);
TCHAR* lpDataSource = new TCHAR[255];
wsprintf(lpDataSource,_T("%s"),m_strDataSource);
if(FAILED(hr))
{
delete[] lpDataSource;
return FALSE;
}
//-----------------------------
hr = CoCreateInstance(m_ClsID, NULL, CLSCTX_INPROC_SERVER, IID__Connection, (LPVOID*) &m_Conn);
if(FAILED(hr))
{
delete[] lpDataSource;
return FALSE;
}
hr = m_Conn->put_Provider(_T("CEDB"));
if(FAILED(hr))
{
delete[] lpDataSource;
return FALSE;
}
hr = m_Conn->Open(lpDataSource,TEXT(""),TEXT(""),adOpenUnspecified);
int a = GetLastError();
if(FAILED(hr))
{
delete[] lpDataSource;
return FALSE;
}
//-------------------------------------------------------------
delete[] lpDataSource;
数据读写执行
BOOL CVORecordset::Open(CString ctrSQL)
{
if(!m_rsRecordSet)
{
TRACE(TEXT("CVORecordset::Open() RecordSet COM Object not initialized\n"));
return FALSE;
}
VARIANT varTSQL,varEmpty;
HRESULT hr;
VariantInit(&varEmpty);
VariantInit(&varTSQL);
varTSQL.bstrVal = SysAllocString(ctrSQL);
varTSQL.vt = VT_BSTR;
if(IsOpen())
m_rsRecordSet->Close();
hr = m_rsRecordSet->Open(varTSQL, varEmpty, adOpenUnspecified, adLockUnspecified,adCmdUnspecified);//adCmdUnknown
m_bIsOpen = (!FAILED(hr));
return m_bIsOpen;
}
写数据
CString str1,str2;
CVORecordset* m_pRecordSet;
m_pRecordSet = new CVORecordset(*m_Conn);
VARIANT vValue;
VariantInit(&vValue);
TRY
{
CTime time=CTime::GetCurrentTime();
str1.Format(_T("%04d-%02d-%02d %02d:%02d:%02d"),time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
SJLX=0;
TCHAR ssss[1000];float v[80];
for(int i=0;i<70;i++)
v=1.0;
wsprintf(ssss,L"insert into LSSJ (RQSJ,SJLX,XZ,LGYL,YHBC,EHBC,SHBC,ZPZS,ZPNJ,TGYL,CKPL,RKMD,CKMD,RKDD,CKDD,RKWD,CKWD,DQNJ,CTJ1,CTJ2,CTJ3,CTJ4,CTJ5,CTJ6,CTJ7,CTJ8,CTJ9,CTJ10,CTJ11,BJTJ,QZYC,GAS1,GAS2,GAS3,GAS4,H2SND1,H2SND2,H2SND3,H2SND4,ZTHL,CO2HL,YYHT,NDef01,NDef02,NDef03,NDef04,NDef05,ZTYL,ZJSZ,DGGD,ZTWZ,BZJS,DGSM,JDSK,FRSD,FYCD,ZTJC,DGSD,ZMZS,RKPL,LJBC1,LJBC2,LJBC3,ZBCS,ZJSJ,ZCTJ,NJYL,DNGL,LLTJ,GKZT,ZJZT,DGSJ) values('%s',%d,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f)",str1,SJLX,v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11],v[12],v[13],v[14],v[15],v[16],v[17],v[18],v[19],v[20],v[21],v[22],v[23],v[24],v[25],v[26],v[27],v[28],v[29],v[30],v[31],v[32],v[33],v[34],v[35],v[36],v[37],v[38],v[39],v[40],v[41],v[42],v[43],v[44],v[45],v[46],v[47],v[48],v[49],v[50],v[51],v[52],v[53],v[54],v[55],v[56],v[57],v[58],v[59],v[60],v[61],v[62],v[63],v[64],v[65],v[66],v[67],v[68],v[69]);
if(!m_pRecordSet->Open(ssss))//执行并返回结果 如果没执行提示
{
AfxMessageBox(L"add CSBDdata failed!",MB_ICONERROR|MB_OK,-1);
}
delete m_pRecordSet;
}
CATCH(CFileException,e)
{
return;
}
END_CATCH