我做了个ado的数据库连接,但是不知为何就是连不上,请哪位有此经验的大哥帮帮忙。
希望把email地址留下,我把源程序发给你,帮我看看是哪儿出了错。
程序极其的简单,除了连接数据库,其他什么都没有。也可以发email给我:[email protected]
我的部分源程序如下:
建立一个名为aaa的对话框程序
用的是ado连接,所有改动过的部分我都贴出来了,各位高手看看吧
stdafx.h中
#if !defined(AFX_STDAFX_H__A25C5752_8A2A_406D_8A11_882BD5403D48__INCLUDED_)
#define AFX_STDAFX_H__A25C5752_8A2A_406D_8A11_882BD5403D48__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//add by me
#import "d:\program files\common files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")in aaa.h
#if !defined(AFX_AAA_H__2D5A915C_1443_4512_9554_F0C511B4B298__INCLUDED_)
#define AFX_AAA_H__2D5A915C_1443_4512_9554_F0C511B4B298__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/////////////////////////////////////////////////////////////////////////////
// CAaaApp:
// See aaa.cpp for the implementation of this class
//class CAaaApp : public CWinApp
{
public:
int ExitInstance(); //add by me
CAaaApp();
_ConnectionPtr m_CP; //add by me
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAaaApp)
public:
virtual BOOL InitInstance();
//}}AFX_VIRTUAL// Implementation//{{AFX_MSG(CAaaApp)
// 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()
};
in aaaDlg.h
// CAaaDlg dialogclass CAaaDlg : public CDialog
{
// Construction
public:
CAaaDlg(CWnd* pParent = NULL);
_RecordsetPtr m_RP; // add by me
......in aaa.cpp...
BOOL CAaaApp::InitInstance()
{
AfxEnableControlContainer();
//add by me 
AfxOleInit();
HRESULT hr;try
{
hr = m_CP.CreateInstance("ADODB.Connection");//创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_CP->Open("Provider=Microsoft.Jet.OLEDB.3.51;Data Source=recording.mdb","","",adModeUnknown);///连接数据库
//上面一句中连接字串中的Provider是针对ACCESS97环境的,对于ACCESS2000,需要改为:Provider=Microsoft.Jet.OLEDB.4.0; }
}
}
catch(_com_error e)//捕捉异常
{
CString errormessage;
errormessage.Format("第一步连接数据库不幸失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return FALSE;
} ......int CAaaApp::ExitInstance()
{
if(m_CP->State) //如果连接是打开的
m_CP->Close(); //关闭它以便下次再连接.不然就会出错!
return CWinApp::ExitInstance();}