在进行vc++与 ACCESS数据库连接的时候出现的问题
关键代码如下:
//stdafx.h  引入ADO库文件
#import"C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","EndOfFile") rename("BOF","FirstOfFile")::CoInitialize(NULL); //初始化OLE/COM库环境_ConnectionPtr pConnection;
  _RecordsetPtr pRecordset;
  _CommandPtr pCommand;  pConnection.CreateInstance(__uuidof(Connection));
  pRecordset.CreateInstance(__uuidof(Recordset));
  pCommand.CreateInstance(__uuidof(Command));  pConnection->ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=data_base.mdb;";
  pConnection->Open(L"",L"",L"",adCmdUnspecified);//单步调试过程中,在这个地方出错出错提示:unhandle exception in test.exe(KERNELDLL) microsoft  c++ exception请问是为什么?谢谢!

解决方案 »

  1.   

    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=records.mdb
      

  2.   

    try
    {
    }
    catch(_com_error &e)
    {
    AfxMessageBox(e.ErrorMessage());
    }看看什么错
      

  3.   

    在界面设计上添加一个ado的ActiveX控件,用向导生成一个连接字符串看看
      

  4.   

    try 


    catch(_com_error &e) 

    AfxMessageBox(e.ErrorMessage()); 

    用这个出现的错误是“未指定的错误!”
    我把AfxMessageBox(e.ErrorMessage()); 换成 e.Description();   后可以直接编译通过,却没有返回相应的错误信息,
    怎么能让它返回错误信息呢?
      

  5.   

    catch一下,看对象创建成功没?
    不要用ConnectionString 直接Open
    Open(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=records.mdb"), _T(""),_T(""));
      

  6.   

    twilightgod 
     
    等 级:
     发表于:2007-10-13 16:55:134楼 得分:0 
    在界面设计上添加一个ado的ActiveX控件,用向导生成一个连接字符串看看 
     
    这样做结果还是提示出现那个错误,不知道是不是过程中弄错了什么啊?
      

  7.   

    奇怪
    按照道理,6楼的Open(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=records.mdb"), _T(""),_T(""),adCmdUnspecified);应该可行啊,我都是这样
      

  8.   

    确实满奇怪的,我测试的只是建了一个基于对话框的MFC应用程序,其次就是加了那些代码,按道理这些不是满复杂的,尝试一下其他的方法看看!
    谢谢各位了
      

  9.   

    恩  好的,麻烦帮忙看看!建了一个基于对话框的MFC应用程序TestAdo,有色字体部分是加的代码;//Resouce.h
    //{{NO_DEPENDENCIES}}
    // Microsoft Visual C++ generated include file.
    // Used by TESTADO.RC
    //
    #define IDR_MAINFRAME 128
    #define IDD_TESTADO_DIALOG 102// Next default values for new objects
    // 
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS#define _APS_NEXT_RESOURCE_VALUE 129
    #define _APS_NEXT_CONTROL_VALUE 1000
    #define _APS_NEXT_SYMED_VALUE 101
    #define _APS_NEXT_COMMAND_VALUE 32771
    #endif
    #endif
    ////////////////////////////////////////////////////////////////////////////////////////
    //StdAfx.h
    // stdafx.h : include file for standard system include files,
    //  or project specific include files that are used frequently, but
    //      are changed infrequently
    //#if !defined(AFX_STDAFX_H__D43B430F_A9CB_4EA9_9967_C90D9508AE1F__INCLUDED_)
    #define AFX_STDAFX_H__D43B430F_A9CB_4EA9_9967_C90D9508AE1F__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers#include <afxwin.h>         // MFC core and standard components
    #include <afxext.h>         // MFC extensions
    #include <afxdisp.h>        // MFC Automation classes
    #include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
    #ifndef _AFX_NO_AFXCMN_SUPPORT
    #include <afxcmn.h> // MFC support for Windows Common Controls
    #endif // _AFX_NO_AFXCMN_SUPPORT#import"C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","EndOfFile") rename("BOF","FirstOfFile")//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_STDAFX_H__D43B430F_A9CB_4EA9_9967_C90D9508AE1F__INCLUDED_)/////////////////////////////////////////////////////////////////////////////////////////TestAdo.h
    // TestAdo.h : main header file for the TESTADO application
    //#if !defined(AFX_TESTADO_H__F0E77C54_C6F6_4848_8A68_80DA726E0642__INCLUDED_)
    #define AFX_TESTADO_H__F0E77C54_C6F6_4848_8A68_80DA726E0642__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/////////////////////////////////////////////////////////////////////////////
    // CTestAdoApp:
    // See TestAdo.cpp for the implementation of this class
    //class CTestAdoApp : public CWinApp
    {
    public:
    CTestAdoApp();// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CTestAdoApp)
    public:
    virtual BOOL InitInstance();
    //}}AFX_VIRTUAL// Implementation //{{AFX_MSG(CTestAdoApp)
    // 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_TESTADO_H__F0E77C54_C6F6_4848_8A68_80DA726E0642__INCLUDED_)/////////////////////////////////////////////TestAdoDlg.h// TestAdoDlg.h : header file
    //#if !defined(AFX_TESTADODLG_H__96F0EB66_E579_4D3B_BE79_60C8817DC1C0__INCLUDED_)
    #define AFX_TESTADODLG_H__96F0EB66_E579_4D3B_BE79_60C8817DC1C0__INCLUDED_#if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000/////////////////////////////////////////////////////////////////////////////
    // CTestAdoDlg dialogclass CTestAdoDlg : public CDialog
    {
    // Construction
    public:
    CTestAdoDlg(CWnd* pParent = NULL); // standard constructor// Dialog Data
    //{{AFX_DATA(CTestAdoDlg)
    enum { IDD = IDD_TESTADO_DIALOG };
    // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CTestAdoDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
    //}}AFX_VIRTUAL// Implementation
    protected:
    HICON m_hIcon; // Generated message map functions
    //{{AFX_MSG(CTestAdoDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };//{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_TESTADODLG_H__96F0EB66_E579_4D3B_BE79_60C8817DC1C0__INCLUDED_)
    //////////////////////////////////////////////////////
    //StdAfx.cpp
    // stdafx.cpp : source file that includes just the standard includes
    // TestAdo.pch will be the pre-compiled header
    // stdafx.obj will contain the pre-compiled type information#include "stdafx.h"//////////////////////////////////////
    //TestAdo.cpp
    // TestAdo.cpp : Defines the class behaviors for the application.
    //#include "stdafx.h"
    #include "TestAdo.h"
    #include "TestAdoDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTestAdoAppBEGIN_MESSAGE_MAP(CTestAdoApp, CWinApp)
    //{{AFX_MSG_MAP(CTestAdoApp)
    // NOTE - the ClassWizard will add and remove mapping macros here.
    //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG
    ON_COMMAND(ID_HELP, CWinApp::OnHelp)
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTestAdoApp constructionCTestAdoApp::CTestAdoApp()
    {
    // TODO: add construction code here,
    // Place all significant initialization in InitInstance
    }/////////////////////////////////////////////////////////////////////////////
    // The one and only CTestAdoApp objectCTestAdoApp theApp;/////////////////////////////////////////////////////////////////////////////
    // CTestAdoApp initializationBOOL CTestAdoApp::InitInstance()
    {
    AfxEnableControlContainer();
    // Standard initialization
    // If you are not using these features and wish to reduce the size
    //  of your final executable, you should remove from the following
    //  the specific initialization routines you do not need.
    ::CoInitialize(NULL);

       _ConnectionPtr pConnection;
      _RecordsetPtr pRecordset;
      _CommandPtr pCommand;
     
      pConnection.CreateInstance(__uuidof(Connection));
      pRecordset.CreateInstance(__uuidof(Recordset));
      pCommand.CreateInstance(__uuidof(Command));   pConnection->ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=data_base.mdb;";   try{
       // pConnection->Open(L"",L"",L"",adCmdUnspecified);
       pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\data_base.mdb","","",adModeUnknown);
       }
      catch(_com_error &e)   {
      AfxMessageBox(e.ErrorMessage());    e.Description();   
      }
      
    #ifdef _AFXDLL
    Enable3dControls(); // Call this when using MFC in a shared DLL
    #else
    Enable3dControlsStatic(); // Call this when linking to MFC statically
    #endif CTestAdoDlg dlg;
    m_pMainWnd = &dlg;
    int nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
    }
    else if (nResponse == IDCANCEL)
    {
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
    } // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;
    }/////////////////////////////////////
      

  10.   

    //TestAdoDlg.cpp// TestAdoDlg.cpp : implementation file
    //#include "stdafx.h"
    #include "TestAdo.h"
    #include "TestAdoDlg.h"#ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif/////////////////////////////////////////////////////////////////////////////
    // CTestAdoDlg dialogCTestAdoDlg::CTestAdoDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CTestAdoDlg::IDD, pParent)
    {
    //{{AFX_DATA_INIT(CTestAdoDlg)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }void CTestAdoDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CTestAdoDlg)
    // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
    }BEGIN_MESSAGE_MAP(CTestAdoDlg, CDialog)
    //{{AFX_MSG_MAP(CTestAdoDlg)
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CTestAdoDlg message handlersBOOL CTestAdoDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here

    return TRUE;  // return TRUE  unless you set the focus to a control
    }// If you add a minimize button to your dialog, you will need the code below
    //  to draw the icon.  For MFC applications using the document/view model,
    //  this is automatically done for you by the framework.void CTestAdoDlg::OnPaint() 
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
    int cxIcon = GetSystemMetrics(SM_CXICON);
    int cyIcon = GetSystemMetrics(SM_CYICON);
    CRect rect;
    GetClientRect(&rect);
    int x = (rect.Width() - cxIcon + 1) / 2;
    int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
    dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
    CDialog::OnPaint();
    }
    }// The system calls this to obtain the cursor to display while the user drags
    //  the minimized window.
    HCURSOR CTestAdoDlg::OnQueryDragIcon()
    {
    return (HCURSOR) m_hIcon;
    }
      

  11.   

    如果不捕获异常,就会提示unhandle exception in test.exe(KERNELDLL) microsoft  c++ exception 
    AfxMessageBox(e.ErrorMessage());   // e.Description();    
    忘了要注释一个了
      

  12.   

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\data_base.mdb"
    这个地方错了,
    1)例外,access是否有密码?
    2)这种方式连接比较危险,如果,我的计算上没有E:或者我的E是光驱,则没有办法使用LZ的程序了
      

  13.   

    #import "msado15.dll" rename("EOF", "ADOEOF") rename("BOF", "ADOBOF")
    把"msado15.dll"拷贝到工程目录下,我就是这么做的