//读配置
          GetPrivateProfileString("FILE","Data_name" ,"", tmp, 120, cfg_name.c_str());
Data_name=string(tmp);
GetPrivateProfileString("FILE","User_name" ,"", tmp, 120, cfg_name.c_str());
User_name=string(tmp);
GetPrivateProfileString("FILE","Password" ,"", tmp, 120, cfg_name.c_str());
Password=string(tmp);
         CoInitialize(NULL);
string  sConn;
sConn="Provider=MSDAORA.1;Persist Security Info=FalseData Source=";
sConn+=Data_name;
_bstr_t  m_sConn=sConn.c_str();
_bstr_t  Username=User_name.c_str();
_bstr_t  password=Password.c_str();

pConn.CreateInstance(__uuidof(Connection));
pConn->CursorLocation =adUseClient;
HRESULT  hr=pConn->Open(m_sConn,Username,password,adConnectUnspecified); //这里出错了 hr返回值
if(FAILED(hr))  

string scon = m_sConn; 
printf("不能连接数据库:%s\n",scon.c_str());
return -1;
}
pCmd.CreateInstance(__uuidof(Command)); 
pCmd->ActiveConnection=pConn; 

解决方案 »

  1.   

    AdoConnectionDB.exe 中的 0x7c812a6b 处未处理的异常: Microsoft C++ exception: _com_error @ 0x0012f618 。
      

  2.   

    把它catch出来啊。
    _bstr_t bstr_myConnectString=myConnectString;
    try
    {
    ....
    }
    catch(_com_error &e)
    {
    isConnected_ = false;

                            //把error打出来 e.Description();
                            
    }
    return true;
      

  3.   

    CoInitialize(NULL);这个地方打开了,后面没见到释放啊,那样可能会引起问题
      

  4.   

    Description='指定的字符串不符合OLE DB规定'
      

  5.   

    问题就在 string转 _bstr_t  该怎么转  
      

  6.   


    _bstr_t bstr_myConnectString=(char*)(myConnectString.c_str());
      

  7.   

    按你说的做了也不行 还是Description='指定的字符串不符合OLE DB规定'
      

  8.   

    try
    {
    cfg_name=cfg_filename;
    char tmp[1024];
    GetPrivateProfileString("FILE","Data_name" ,"", tmp, 120, cfg_name.c_str());
    Data_name=string(tmp);
    GetPrivateProfileString("FILE","User_name" ,"", tmp, 120, cfg_name.c_str());
    User_name=string(tmp);
    GetPrivateProfileString("FILE","Password" ,"", tmp, 120, cfg_name.c_str());
    Password=string(tmp);
    CoInitialize(NULL); string  sConn;
    sConn="Provider=MSDAORA.1;Persist Security Info=FalseData Source=";
    sConn+=Data_name;
    _bstr_t  m_sConn=_bstr_t(sConn.c_str());
    bstr_t m_sConn=(char*)(sConn.c_str());
    cout<<"m_sConn="<<m_sConn<<endl;
    pConn.CreateInstance(__uuidof(Connection));//建立连接指针
    pConn->CursorLocation =adUseClient;//连接方式
    HRESULT  hr=pConn->Open(m_sConn,_bstr_t(User_name.c_str()),_bstr_t(Password.c_str()),adConnectUnspecified); //打开连接
    if(FAILED(hr))  

    string scon = m_sConn; 
    printf("不能连接数据库:%s\n",scon.c_str());
    return -1;
    }
    pCmd.CreateInstance(__uuidof(Command)); //建立命令指针
    pCmd->ActiveConnection=pConn; //激活这个命令指针
    }
    catch (_com_error &e)
    {
    printf("连接数据库失败Description = '%s'\n", (char*) e.Description());
    return -1; }
    return  0;
    代码是这个
      

  9.   

    _bstr_t m_sConn=_bstr_t(sConn.c_str());//这个事被注释掉了的
      

  10.   

    cout<<"m_sConn="<<m_sConn<<endl;
    这个不一定能打出来吧。
    你把sConn打出来就行啊。我们得看看它的内容,不然怎么判断是什么错。
      

  11.   

    能打印出来是没错的  但在OPEN 这里就开始报错了
      

  12.   

    sigh,你还是没明白我的意思。错误信息里已经明确告诉你你的连接串有问题。我要你的程序干什么。
    你先用sqlplus <Username>/<password>@<datasource name>去连你的数据库吧,连通了,你的程序照着这三个参数改就完了。
      

  13.   

    问题是我想把要连接的数据库 用户名 密码改成配置的不行 我直接带值进去时可以的链接的  就是string转
    _bstr_t 这里出问题 就是找不出什么问题
      

  14.   

    不好意思是我自己问题没看清 是string 那少了个; 哎真是粗心呀 谢谢楼上的所有人