请教各位ado编程菜鸟问题

xijhee 2002-03-08 09:42:32
想编写一个用ADO来访问数据库的程序,但不知到错误在那里,请教给位高手,谢谢!
----------- 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__B4B11017_5F87_4732_8F84_BDA823C53394__INCLUDED_)
#define AFX_STDAFX_H__B4B11017_5F87_4732_8F84_BDA823C53394__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

//peter add begin
#include <comdef.h>
#pragma warning(disable:4146)
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace rename ("EOF","adoEOF")
#pragma warning(default:4146)
//#include "adoid.h"
//#include "adoint.h"
//#include "icrsint.h"
//peter add end


//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__B4B11017_5F87_4732_8F84_BDA823C53394__INCLUDED_)

----------- ADOTESTDLG.CPP
BOOL CAdoTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 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
//peter add begin
try{
m_pConnection.CreateInstance(__uuidof(Connetion));
m_pConnection.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\GCL6\\Data\\Test.mdb;",
"","",-1); //在"="两边莫加空格!
catch(...)
{
AfxMessageBox("数据库连接失败!");
}
//peter add end
return TRUE; // return TRUE unless you set the focus to a control
}


错误提示:
--------------------Configuration: AdoTest - Win32 Debug--------------------
Compiling...
AdoTestDlg.cpp
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(122) : error C2065: 'Connetion' : undeclared identifier
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(123) : error C2039: 'Open' : is not a member of '_com_ptr_t<class _com_IIID<struct _Connection,&struct __s_GUID _GUID_00000550_0000_0010_8000_00aa006d2ea4> >'
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(125) : error C2318: no try block associated with this catch handler
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(133) : error C2317: 'try' block starting on line '121' has no catch handlers
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(134) : error C2601: 'OnSysCommand' : local function definitions are illegal
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(151) : error C2601: 'OnPaint' : local function definitions are illegal
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(178) : error C2601: 'OnQueryDragIcon' : local function definitions are illegal
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(183) : error C2601: 'OnInitDialog' : local function definitions are illegal
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(251) : error C2601: 'OnButton1' : local function definitions are illegal
C:\Documents and Settings\Administrator\桌面\3\AdoTest\AdoTestDlg.cpp(258) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

AdoTest.exe - 10 error(s), 0 warning(s)
...全文
54 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xijhee 2002-03-09
  • 打赏
  • 举报
回复
已按以上的例子改过来,运行没错,但总是显示 "数据库连接失败!"。
C:\\ADOTest.mdb 文件存在(access97)。
请教!
try{
m_pConnection.CreateInstance("ADODB.Connection");
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\ADOTest.mdb;","","",-1);
AfxMessageBox("aaa");

}
catch(...)
{
AfxMessageBox("数据库连接失败!");
}
njc77 2002-03-08
  • 打赏
  • 举报
回复
你的stdafx.h没有问题,只要将msado15.dll文件所在路径写对即可
ADOTESTDLG.CPP 中有几个错误
1.
try
{
}//你少了此括号
catch(...)
{
}
2.正确的connect连接格式应该这样写,我已经编译通过,可以使用,你看与你的有何区别
_ConnectionPtr m_pConnection;//声明一个连接,可在adotestdlg.h中定义
try
{
m_pConnection.CreateInstance("ADODB.Connection");
m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Test.mdb;","","",-1);
}//数据源格式查一下msdn即可知如何连接access
catch(...)
{
AfxMessageBox("数据库连接失败!");
}

xijhee 2002-03-08
  • 打赏
  • 举报
回复
多多请教!

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧