解决方案 »

  1.   

    多个线程共用一个Connection,这种情况是要加锁同步或者开事务的,会影响效率
    试试改为一个线程一个Connection
      

  2.   

    另外,每个线程回调函数开头都调用一下AfxOleInit();
      

  3.   


    UINT Thread1(LPVOID lpParam)
    {
    CString str;
    clock_t t_Start , t_End;
    //初始化COM库
    AfxOleInit();
    //连接数据库
    //建立数据库的连接需要使用Connection Object。步骤如下:
    //定义一个_ConnectionPtr类型的指针,代码如下: 
    //_ConnectionPtr m_pConnection;  
    //调用CreateInstance方法实例化: 
    //m_pConnection.CreateInstance(__uuidof(Connection));

    CString strSQL;
    HRESULT hr;
    _ConnectionPtr m_pConnection; 
    try
    {
    hr=m_pConnection.CreateInstance(__uuidof(Connection));
    m_pConnection->CursorLocation=adUseClient;
    strSQL="Provider=SQLOLEDB;Server=1104A01\\MSSQLSERVER1104;Database=DCS;UID=sa;PWD=726";
    if(SUCCEEDED(hr))

    hr=m_pConnection->Open(_bstr_t(strSQL),"","",adModeUnknown);

    } catch(_com_error e) //捕捉异常     
    {
    CString errormessage;
    errormessage.Format(_T("连接数据库1失败!\r\n错误信息:%s",e.ErrorMessage()));
    AfxMessageBox(errormessage);//显示错误信息 return FALSE;     
    }
    _CommandPtr m_pCommand;
    try
    { m_pCommand.CreateInstance(__uuidof(Command));
    m_pCommand->ActiveConnection = m_pConnection;
    m_pCommand->CommandText="Insert_date_data";// 制定调用哪个存储过程
    m_pCommand->CommandType=adCmdStoredProc;     // 制定Sql命令类型是存储过程
    m_pCommand->Parameters->Refresh();m_pCommand->Parameters->GetItem((long)1)->Value ="2013-03-13 21:34:50";//按存储过程的参数顺序给参数赋值
    m_pCommand->Parameters->GetItem((long)2)->Value =100;
    m_pCommand->Parameters->GetItem((long)3)->Value ="1234567890123456789012345678901234567890123456789012345678901234567890";
    } catch(_com_error e) //捕捉异常     
    {
    CString errormessage;
    errormessage.Format(_T("连接数据库1失败!\r\n错误信息:%s",e.ErrorMessage()));
    AfxMessageBox(errormessage);//显示错误信息 return FALSE;     
    }
    t_Start = clock();
    m_pConnection->BeginTrans();
    while(num--)
    {
    m_pCommand->Execute(0,0,adCmdStoredProc);
    }
    m_pConnection->CommitTrans();
    t_End = clock();
    str.Format("线程1所用时间为:%d",(t_End - t_Start));
    ::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_EDIT1,str);
    Sleep(2000);
    return 0;在线程2中我定义了
    _ConnectionPtr   m_pConnection2
    _CommandPtr m_pCommand2
    我用两个编辑框显示每个线程运行的时间,
    但是每次只有一个线程能够运行结束,并显示运行时间,另一个线程一进运行数据一直没有写进数据库,这是为什么?有时候线程1正常,有时候线程2正常,求助啊...
      

  4.   

    我两在两个线程向数据库写数据,为什么有些数据写不进数据库?会丢失一些数据?速度慢的时候,不会出现这种情况,一量MFC写的太快,数据库就丢数据,如何解决
      

  5.   

    我两在两个线程向数据库写数据,为什么有些数据写不进数据库?会丢失一些数据?速度慢的时候,不会出现这种情况,一量MFC写的太快,数据库就丢数据,如何解决求解 问题该如何解决