没有合适的资源?快使用搜索试试~ 我知道了~
vc 用ado连接access数据库 (在要连接的 类中加入 extern C****App theApp;)
4星 · 超过85%的资源 需积分: 10 481 下载量 76 浏览量
2010-01-10
14:03:04
上传
评论 2
收藏 5KB TXT 举报
温馨提示
试读
6页
vc 用ado连接access数据库 自己整理的希望大家有用
资源推荐
资源详情
资源评论
//////////////////////////////////////希望这些能帮到大家,我开始连也是痛苦很久。。。///////////////////////////
首先 在头文件 StdAfx.h 中加入:
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")//
然后在App 类 中定义变量:
public:
///////数据库连接变量///////////
_ConnectionPtr m_pConnection;
再初始化:在App 类 的initlnstance 函数中(代码中 ”map.mdb“ 中的 “map” 是数据库名字)
////////////连数据库////////////////////
//初始化
AfxOleInit();
// 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.
HRESULT hr; //The HRESULT data type is a 32-bit value that is used to describe an error or warning.
try
{
hr=m_pConnection.CreateInstance("ADODB.Connection"); ///创建Connextin对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=map.mdb","","",adModeUnknown); ///连接数据库
//上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要乞讨为://Provider=Microsoft.Jet.OLEDB.3.51;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据失败!\r\n错误码信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);//显示错误信息
return FALSE;
}
接着在你需要的类中定义变量:
public:
//////////////连接数据库变量/////////////////
_RecordsetPtr m_pRecordset;
再在此类中的你要连数据库的函数中加:
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM 表名 " , // 查询ParkTab表中所有字段
_variant_t((IDispatch*)theApp.m_pConnection,true),
adOpenStatic,
adLockOptimistic,
{
hr = m_pConnection->Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=map.mdb","","",adModeUnknown); ///连接数据库
//上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要乞讨为://Provider=Microsoft.Jet.OLEDB.3.51;
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据失败!\r\n错误码信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);//显示错误信息
return FALSE;
}
接着在你需要的类中定义变量:
public:
//////////////连接数据库变量/////////////////
_RecordsetPtr m_pRecordset;
再在此类中的你要连数据库的函数中加:
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM 表名 " , // 查询ParkTab表中所有字段
_variant_t((IDispatch*)theApp.m_pConnection,true),
adOpenStatic,
adLockOptimistic,
剩余5页未读,继续阅读
wwwxhb
- 粉丝: 30
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页