数据库连接关闭了
记录集关闭了再操作时是否有打开?
还是不怎么明白代码发给我看看
[email protected]

解决方案 »

  1.   

    CString dbname = "g.mdb";
    _ConnectionPtr m_pConnect2;//声明连接指针对象
    _RecordsetPtr  m_pSet3;//声明数据集对象
    CString strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbname;//连接字符串
    HRESULT hr;
    hr = m_pConnect2.CreateInstance(__uuidof(Connection));
    if(!SUCCEEDED(hr))
    {
    AfxMessageBox("database error");
    return ;
    }
    try{
    m_pConnect2->Open(_bstr_t(strConnection),"","",adModeUnknown);
    }
    catch(_com_error &e)
    {
    AfxMessageBox(e.ErrorMessage());
    }
    m_pSet3.CreateInstance(__uuidof(Recordset));//创建对象
    CString sqltext = ""; CString buffer = ""; CStringArray dest;
    CJSApp *app = (CJSApp*)AfxGetApp();
    CStdioFile *file = new CStdioFile;
    CFileDialog dlg(TRUE);
    if(dlg.DoModal() != IDOK)
    return;
    if(!file->Open(dlg.GetPathName(),CFile::modeRead))
    {
    AfxMessageBox("文件不能打开!");
    return;
    }
    file->SeekToBegin();
    file->ReadString(buffer);
    while(file->ReadString(buffer))
    {
    decodeCString(buffer,dest,',');
    sqltext = "SELECT * FROM DANWEI WHERE CODE = '";sqltext += dest[0];sqltext += "'";
    try{
    HRESULT hr = m_pSet3->Open(_variant_t(sqltext),m_pConnect2.GetInterfacePtr(),
                            adOpenStatic,
                            adLockOptimistic,
                            adCmdText);
    }
    catch(_com_error &e)
    {
    AfxMessageBox(e.ErrorMessage());
    }
    if(m_pSet3->adoBOF)
    {
    if(m_pSet3)
    {
    m_pSet3->Close();
    }
        continue;
    }
    m_pSet3->MoveFirst();
    for(int i =0; i < app->s_power_data.GetSize(); i++)
    {
    if(dest[i+1].GetLength() == 0)
    dest[i+1] = " ";
    m_pSet3->PutCollect((const _variant_t)app->s_power_data.GetAt(i),_variant_t(dest[i+1]));
    }
    m_pSet3->Update();
    m_pSet3->Close();
    }
    if(m_pConnect2)
    {
    m_pConnect2->Close();m_pConnect2 = NULL;
    } m_pSet3 = NULL;
    AfxMessageBox("数据导入成功!");这就是函数A的代码
    再操作时肯定有打开了
    B函数单独执行是没有问题的,但先A后B就出错(不仅仅是B函数,其他数据库操作也一样出错)
      

  2.   

    可否让connection设置为全局
    只打开一次
      

  3.   

    那应该没什么关系吧。
    比如函数B:
    {
    StartDB() 
    ..... 
    EndDB() 
    }
    函数C:
    {
    StartDB() 
    ..... 
    EndDB() 
    }
    无论先执行B再执行C还是先执行C再执行B都没问题,
    但只要执行了A再执行B或C就不行。执行了B或C后再执行A也不行
    感觉就是A里面set打开关闭多次后就不行了
      

  4.   

    说不定
    connection关闭并非马上关闭
      

  5.   

    事实上也确实没用,我测试过了,设置为类的成员变量,结果还是一样。(ABC均为类的成员函数)
      

  6.   

    你把
    catch(_com_error   &e) 

    AfxMessageBox(e.ErrorMessage()); 

    改成
    catch(_com_error   &e) 

    AfxMessageBox(e.Description()); 
    }
    试试先..