buil时出现warning:'CSampledbDlg::Onsave' : not all control paths return a value
onsave是按扭的消息函数,完成向数据库中添加记录的工作run时,按下该按钮出现error框:runtime error
                             programe:...
                             abnormal programe termination
请教:怎么改才能正常连上数据库

解决方案 »

  1.   

    函数中有的地方没有return
    把代码贴出来看看
      

  2.   

    BOOL CSampledbDlg::Onsave() 
    {
    CString name;
    CString message;
    m_name.GetWindowText(name);
    m_message.GetWindowText(message);
    _ConnectionPtr MyDb;
        MyDb.CreateInstance(__uuidof(Connection));
        MyDb->Open("DSN=sample;UID=sa;PWD=sa","","",-1);
    _RecordsetPtr Myset;    //用于创建一个记录集
    CString strSQL;
    strSQL="INSERT INTO sampledb(name,message) VALUES (name,message)";
    try
    {
    HRESULT hTRes;
    hTRes = Myset.CreateInstance("ADODB.Recordset");
    if (SUCCEEDED(hTRes))
    {

    hTRes = Myset->Open(strSQL.AllocSysString(),
    MyDb.GetInterfacePtr(),
    adOpenDynamic,adLockPessimistic,adCmdText);
    if(SUCCEEDED(hTRes))
    {
    TRACE(_T("连接成功!\n"));

    }

    }
    }
    catch(_com_error e)
    {
    CString str;
    str.Format("%s",e.ErrorMessage());
    AfxMessageBox(str);

    return TRUE;
    }
    请指教!
      

  3.   

    你用_RecordsetPtr 对象的open语句 来执行一个插入语句应该不行的,换用_commandptr
      

  4.   

    Myset.Release();可以用来recordset写插入语句的!
      

  5.   

    现在修改了代码:
    BOOL CSampledbDlg::Onsave() 
    {
    CString name;
    CString message;
    m_name.GetWindowText(name);
    m_message.GetWindowText(message);
    ::CoInitialize(NULL);
    _ConnectionPtr MyDb;
        MyDb.CreateInstance(__uuidof(Connection));
        _variant_t RecordsAffected;
    CString strSQL;
    strSQL="INSERT INTO sampledb(name,message) VALUES (name,message)";
    try
    {

    MyDb->Open("DSN=sample","","",-1);
    MyDb->Execute(strSQL.AllocSysString(),
    &RecordsAffected,adCmdText);
    MyDb->Close();
    }
    catch(_com_error e)
    {
    CString str;
    str.Format("%s",e.ErrorMessage());
    AfxMessageBox(str);
    }
    MyDb=NULL;
    ::CoUninitialize();
    return TRUE;
    }
    没有error也没有warning,可是更奇怪的事情发生了:
    若程序中有::CoInitialize(NULL);这句,按下按钮后弹出错误框“IDispatch error #3092”
    若把这句注释掉,则弹出“无效指针”
    各位大侠帮帮忙!
      

  6.   

    换用全局函数初始化
    AfxOleInit();
      

  7.   

    strSQL="INSERT INTO sampledb(name,message) VALUES(name,message)";//空格符号
      

  8.   

    laiyiling(最熟悉的陌生人) :
        怎么“换用全局函数初始化”,还有后面那段不太明白,可不可以详细说一下?
        感激不尽!
       这程序已经闹了我几天了!
      

  9.   

    我发现是strSQL="INSERT INTO sampledb(name,message) VALUES (name,message)";的问题
    因为name和message是cstring的变量,在sql语句中不能直接写,所以我改成了用记录集操作不过还是有问题
    BOOL CSampledbDlg::Onsave() 
    {
             CString name;
    CString message;
    m_name.GetWindowText(name);
    m_message.GetWindowText(message);
    ::CoInitialize(NULL);
    _ConnectionPtr MyDb;
    _RecordsetPtr MySet;
        MyDb.CreateInstance(__uuidof(Connection));
    MySet.CreateInstance(__uuidof(Recordset));
    try
    {

    MyDb->Open("DSN=sample","","",adModeUnknown);
    MySet->Open("SELECT * FROM sampledb",MyDb.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
    MySet->AddNew();
    MySet->PutCollect("name",_variant_t(name));
    MySet->PutCollect("message",_variant_t(message));
    MySet->Update(); MySet->Close();
    MyDb->Close();
    }
    catch(_com_error e)
    {
    CString str;
    str.Format("%s",e.ErrorMessage());
    AfxMessageBox(str);
    } MySet=NULL;
    MyDb=NULL;
    ::CoUninitialize();
    return TRUE;
    }
    实在头大了,大家不都用得好好得嘛,怎么我的就有问题呢
      

  10.   

    按下按钮后弹错误框“IDispatch error #3127”
    这是什么意思嘛
      

  11.   

    既然,是按钮事件,那么就别返回BOOL值了,
    把执行SQL语句的部分封装成一个函数吗
    (BOOL ExecuteSQL(CString strSQL))?这样OnSave就可以根据返回值处理。
      

  12.   

    1.CAPP::InitInstance(...){      AfxOleInit();
    2.strSQL="INSERT INTO sampledb(name,message) VALUES('name','message')";//空格符号