我在stdafx.h文件中添加如下代码:
#import "c:\Program Files\Common Files\System\ado\msado15.dll"\ no_namespace \rename("EOF","adoEOF") rename("BOF","adoBOF")
但是程序里却还是不认识_ConnectionPtr这个类型的变量?为什么啊?

解决方案 »

  1.   

    1,确认你的C盘下有c:\program files\common files\system\ado\msado15.dll这个文件;
    2,确定你定义变量的文件中有#include "stdafx.h"的语句;
      

  2.   

    错误如下:
    :\vc数据库\mytest\mytest\mytest.h(27) : error C2146: syntax error : missing ';' before identifier 'm_pConnection'
    f:\vc数据库\mytest\mytest\mytest.h(27) : error C2501: '_ConnectionPtr' : missing storage-class or type specifiers
    f:\vc数据库\mytest\mytest\mytest.h(27) : error C2501: 'm_pConnection' : missing storage-class or type specifiers
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(59) : error C2065: 'm_pConnection' : undeclared identifier
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(59) : error C2228: left of '.CreateInstance' must have class/struct/union type
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(59) : error C2065: 'Connection' : undeclared identifier
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(62) : error C2227: left of '->Open' must point to class/struct/union
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(64) : error C2061: syntax error : identifier '_com_error'
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(64) : error C2310: catch handlers must specify one type
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(66) : error C2143: syntax error : missing ')' before ';'
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(67) : error C2065: 'FLASE' : undeclared identifier
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(70) : error C2317: 'try' block starting on line '61' has no catch handlers
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(93) : error C2227: left of '->state' must point to class/struct/union
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(95) : error C2227: left of '->Close' must point to class/struct/union
    F:\VC数据库\MyTest\MyTest\MyTest.cpp(95) : error C2059: syntax error : ')'
      

  3.   

    _ConnectionPtr跳到定义,看看有没有包含头文件,看看是不是可以用其他符号替换这个类型
      

  4.   

    把mytest.cpp头文件以及_ConnectionPtr定义的代码贴出来看看~
      

  5.   

    我也不是很懂,仅供参考#include "stdafx.h"
    #include <windows.h>
    #include <atlstr.h>
    #include <iostream>
    using namespace std;#import "C:\Program Files\Common Files\SYSTEM\ADO\msado15.dll" no_namespace rename("EOF","adoEOF")DWORD WINAPI GetUserInfo();int main()
    {
    ::CoInitialize(NULL);
    _ConnectionPtr pConn = NULL;

    //sode是数据库服务器192.168.3.9上的一个数据库
    CString strConn = "Provider=SQLOLEDB.1;Data Source=192.168.3.9,1433;Initial Catalog=sode";
    if (S_OK != pConn.CreateInstance("ADODB.Connection"))
    {
    cout << "create instance failed..." << endl;
    return -1;
    }

    try
    {
    HRESULT hr = pConn->Open((_bstr_t)strConn, "sa", "sa",adConnectUnspecified);
    if (FAILED(hr))
    {
    cout << "connect failed!" << endl;
    return   FALSE;
    }
    }
    catch(_com_error e)
    {
    cout << "connect exception..." << endl;
    return FALSE;
    }

    cout << "connect succ!" << endl;

    return 0;
    }DWORD WINAPI GetUserInfo()
    {
    return 0;
    }
    要么使用MSVCRT.LIB,要么使用libcmtd.lib
    1  Ignore all default libraries然后手动添加是一种办法但是比较繁琐。   
    2  因为MSVCRT.lib(MSVCRT.dll)是Dll不能Ignore它,应该Ignore的是libcmtd.lib  在Project->settings->Link中Category中选择“input"选项在Ignore libraries 中将与MSVCRT.lib冲突的libcmtd.lib写入就行了
    3 或者project-->setting-->link-->project   options里面不是加入/NODEFAULTLIB:MSVCRT.lib 而是加入/NODEFAULTLIB:libcmtd.lib
      

  6.   

    // MyTest.h : main header file for the MYTEST application
    //#if !defined(AFX_MYTEST_H__9DD5DA5D_3A25_4C96_959E_07034220C57F__INCLUDED_)
    #define AFX_MYTEST_H__9DD5DA5D_3A25_4C96_959E_07034220C57F__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#ifndef __AFXWIN_H__
    #error include 'stdafx.h' before including this file for PCH
    #endif#include "resource.h" // main symbols/////////////////////////////////////////////////////////////////////////////
    // CMyTestApp:
    // See MyTest.cpp for the implementation of this class
    //class CMyTestApp : public CWinApp
    {
    public:
    CMyTestApp();
    //定义ADO连接变量指针
    _ConnectionPtr m_pConnection;// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyTestApp)
    public:
    virtual BOOL InitInstance();
    virtual int ExitInstance();
    //}}AFX_VIRTUAL// Implementation //{{AFX_MSG(CMyTestApp)
    // NOTE - the ClassWizard will add and remove member functions here.
    //    DO NOT EDIT what you see in these blocks of generated code !
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };
    ///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_MYTEST_H__9DD5DA5D_3A25_4C96_959E_07034220C57F__INCLUDED_)
      

  7.   

    没有包含stdafx.h#error include 'stdafx.h' before including this file for PCH
    换成
    #include "stdafx.h"
      

  8.   

    还是不对啊!在mytest.cpp里有#include "stdafx.h"
      

  9.   

    _ConnectionPtr m_pConnection;
    不应该在这里定义,在你的mytest.cpp里定义吧~
      

  10.   

    除非你在这里包含了stdafx.h
    还要防止在mytest.cpp中重复include
      

  11.   

    在MyTest.h中试试:
    将#error include 'stdafx.h' before including this file for PCH
    修改为:
    #define __AFXWIN_H__
    #include "stdafx.h"
      

  12.   

    我知道了 你可以差这个语句 
    AfxOleInit();
    你加上试试
      

  13.   

    在APP 中的 InitInstance 下添加
      

  14.   

    可能问题出现在你的Connection对象都没有创建,不应该为NULL的,可以改为
    _ConnectionPtr  p(_uuidof(Connection))
      

  15.   

    刚才俺试过了,加上#include是可以运行的~
    不过,最好别声明为公有成员变量,否则将破坏其封装性,就你的代码本身而言,也不需要这么做,全局或局部变量都是可以的,完全可以在.cpp中声明~也不会出现你说的错误~