我的连接窗口ok按钮代码如下!
void CConnectDlg::OnOK() 
{
// TODO: Add extra validation here
UpdateData(true);

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
//避免和其他库的常量名冲突
#include "icrsint.h"
inline void TESTHR(HRESULT x){if FAILED(x) _com_issue_error(x);};
#define DATEFMT CString("%s")
_ConnectionPtr pConn=NULL;
try
{
 pConn.CreateInstance(_uuidof(Connection));
 CString conn_;
 conn_.Format("DSN=%s;UID=%s;PWD=%s",m_strDB,m_strUSER,m_strPWD);
 _bstr_t conn=conn_
}
catch(_com_error &e)
{
CString err;
err.Format("%s",(char*)(e.Description()));
AfxMessageBox(err);
//AfxMessageBox(e.ErrorMessage());
}
AfxMessageBox("已经连接到数据库"+m_strDB);
CDialog::OnOK();
}为什么会提示很多错误?
#import only permitted at global or namespace scope
……
'_connectionPtr':undeclared identifier
......
怎么回事?

解决方案 »

  1.   

    #import "c:\\program file\\common files\\system\\ado\\msado15.dll" no_namespace rename("EOF","adoEOF")
    不能放在函数体内。
    在调用CreateInstance(_uuidof(Connection))前要用::CoInitialize(NULL)进行初始化。
    一路顺风。
      

  2.   

    来晚了,楼上说的正确。#import一般在头文件或者实现文件开头使用。使用COM需要初始化。
      

  3.   

    大概还要再app的init里面调用afxoleinit()
      

  4.   

    stdafx.h
    #include "stdafx.h"#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
    //避免和其他库的常量名冲突
    #include "icrsint.h"
    inline void TESTHR(HRESULT x){if FAILED(x) _com_issue_error(x);};
    #define DATEFMT CString("%s")
    我的登陆窗口
    void CConnectDlg::OnOK() 
    {
    // TODO: Add extra validation here
    UpdateData(true);



    _ConnectionPtr pConn=NULL;
    if(FAILED(::CoInitialize(NULL)))
            return;
    try
    {
     pConn.CreateInstance(_uuidof(Connection));
     CString conn_;
     conn_.Format("DSN=%s;UID=%s;PWD=%s",m_strDB,m_strUSER,m_strPWD);
     _bstr_t conn=conn_
    }
    catch(_com_error &e)
    {
    CString err;
    err.Format("%s",(char*)(e.Description()));
    AfxMessageBox(err);
    //AfxMessageBox(e.ErrorMessage());
    }*/
    AfxMessageBox("已经连接到数据库"+m_strDB);
    CDialog::OnOK();
    }
    还是有错误,很多!
    '_ConnectionPtr':undeclared identifier
    ......
    'pConn':undeclared identifier
    ......
    还是不行啊!大家帮忙!!!
      

  5.   

    成功了谢谢大家!!我把代码一紧张放在stdafx.cpp里了!呵呵!应该放在stdafx.h里!对了,我是做一个选课系统,选择ado对吗??
    如何实现对界面显示的表的一条信息用鼠标点选,然后对应的按钮进行操作??
    是用GetSelectionMark()吗??
      

  6.   

    又有一个问题!!
    void CConnectDlg::OnOK() 
    {
    // TODO: Add extra validation here
    UpdateData(true);



    _ConnectionPtr pConn=NULL;
    if(FAILED(::CoInitialize(NULL)))
            return;
    try
    {
     pConn.CreateInstance(_uuidof(Connection));
     CString conn_;
     conn_.Format("DSN=%s;UID=%s;PWD=%s",m_strDB,m_strUSER,m_strPWD);
     _bstr_t conn=conn_;
    }
    catch(_com_error &e)
    {
    CString err;
    err.Format("%s",(char*)(e.Description()));
    AfxMessageBox(err);
    this->EndDialog(0);
    //exit(1);
    //AfxMessageBox(e.ErrorMessage());
    }
    //AfxMessageBox("已经连接到数据库"+m_strDB);
    CDialog::OnOK();
    }为什么连接失败它不提示错误哪?还是执行下一个窗口?
    这个登陆窗口我是在主窗口的初始化里调用的,有关系吗?