关于用 VC++6.0新建access需要注意的

网上有很多人写了怎么用vc生成access,都不太完整,搬来运行,都有些毛病,现理一份完整的,可以直接运行的,说明如下:(大部分代码都来自网上,有些细节提示一下)

使用的环境:vc++6.0 sp6,office2007(不知道其他的是否可以,没试过)

以下代码转自:http://www.cnblogs.com/suhuaiqiang/archive/2010/09/06/1819352.html
************************************************************************************************************

用ADOX创建access数据库方法很简单,只需要创建一个Catalog对象,然后调用它的Create方法就可以了。

例程ADOXCreateDatabase演示如何使用ADOX创建一个ACCESS数据库。

打开VC++ 6.0,新建一个基于对话框的工程ADOXCreateDatabase。在对话框IDD_ADOXCREATEDATABASE_DIALOG中添加一个编辑框IDC_DBNAME和一个按钮IDC_BTN_CREATE,编辑框用以输入数据库名称。

使用ClassWizard给编辑框创建一个CString变量m_dbName。

双击IDC_BTN_CREATE按钮,并编辑OnBtnCreate()函数如下:

void CADOXCreateDatabaseDlg::OnBtnCreate() 

{

    //使输入到编辑框IDC_DBNAME的内容更新到m_dbName变量中

    UpdateData(TRUE); 

    CString str;

    str="d:\\"+m_dbName+".mdb"; 

    //检查该数据库是否已经存在,如果该数据库已经存在,弹出消息框,返回

    //使用API函数PathFileExists()检查路径文件是否存在

    //请注意:为了使用API函数PathFileExists(),需要加入

    //#include "Shlwapi.h"

    //#pragma comment(lib,"shlwapi.lib")

    if(PathFileExists(str))

    {

       CString strTemp;

       strTemp.Format("%s已存在!",str);

       AfxMessageBox(strTemp);

       return ;

    }

 

    //定义ADOX对象指针并初始化为NULL

    //用ADOX创建access数据库方法很简单,

    //只需要新建一个Catalog对象,然后调用它的Create方法就可以了。

    //Catalog是 ADOX 的一个对象,它包含描述数据源模式目录的集合。

    //在这里,您只需知道创建数据库时使用这个对象就可以了。

    //注意用try...catch组合捕捉错误

    _CatalogPtr m_pCatalog = NULL;

    CString DBName="Provider=Microsoft.JET.OLEDB.4.0;Data source=";

    DBName=DBName+str;

    try

    {

       m_pCatalog.CreateInstance(__uuidof(Catalog));

       m_pCatalog->Create(_bstr_t((LPCTSTR)DBName));

    }

    catch(_com_error &e)

    {

       AfxMessageBox(e.ErrorMessage());

       return ;

 

    }   

}

使用ADOX,需要引入ADOX的动态链接库msadox.dll,即在stdafx.h中加入如下语句:

#import "C:\Program Files\Common Files\system\ado\msadox.dll" no_namespace rename("EOF","adoEOF")

另外,ADOX属于COM对象,所以要在CADOXCreateDatabaseApp::InitInstance()函数中加入:

    if(!AfxOleInit())

    {

       AfxMessageBox("OLE初始化出错!");

       return FALSE;

    }

初始化COM。

好了,编译并运行该例程,对于编译过程中弹出的4146号警告不要理会。在编辑框中输入一个数据库名称,点击“创建数据库”按钮,该数据库将在d盘根目录下创建,再次输入该数据库名称并点击“创建数据库”按钮,将弹出警告对话框。

在vc中使用ADO的时候会得到4146号警告信息,我们可以不去理会,也可以通过#pragma warning指令解决,方法为:

将在stdafx.h中加入的语句:

#import "C:\Program Files\Common Files\system\ado\msadox.dll" no_namespace rename("EOF","adoEOF")

前后再加一条语句,修改后为:

#pragma warning (disable:4146)

#import "C:\Program Files\Common Files\system\ado\msadox.dll" no_namespace rename("EOF","adoEOF")

#pragma warning (default:4146)

 

指令#pragma warning (disable:4146) 暂时屏蔽编译时4146警告信息

指令#pragma warning (default:4146) 重置编译器的4146警告到默认状态

******************************************************************************************************





需要指出的是:放在stdAfx.h 代码位置:
===================================

#if !defined(AFX_STDAFX_H__F07EB268_7907_41D9_A1A6_0DDCD8751BF6__INCLUDED_)
#define AFX_STDAFX_H__F07EB268_7907_41D9_A1A6_0DDCD8751BF6__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

#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")

#pragma warning (disable:4146)
#import "c:\Program Files\Common Files\system\ado\msadox.dll"
#import "c:\Program Files\Common Files\system\ado\msado15.dll" no_namespace rename("EOF","adoEOF")
#pragma warning (default:4146)
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__F07EB268_7907_41D9_A1A6_0DDCD8751BF6__INCLUDED_)


放在CADOXCreateDatabaseApp::InitInstance()的位置:
================================================


{
AfxEnableControlContainer();

    if(!AfxOleInit())
    {
AfxMessageBox("OLE初始化出错!");
return FALSE;
    }

// 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.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif

CSmpl01Dlg 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;
}


=========================================================================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值