vc 连接access2000数据库错误
我使用ADO连接数据库,编码没有错误但是得不到结果,总是数据库连接失败,求原因
#include <iostream.h>
//语句no_namespace说明ADO对象不使用命名空间,rename ("EOF", "adoEOF")说明将ADO中结束标志EOF改为adoEOF,以避免和其它库中命名相冲突。
#import "c:\program files\common files\system\ado\msado15.dll" \
no_namespace rename ("EOF", "adoEOF")
int main(){
//使用ADO连接数据库...
_ConnectionPtr m_pConnection;
CoInitialize(NULL);
m_pConnection.CreateInstance(__uuidof(Connection));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些想不到的错误。
try
{
/**连接数据库 打开本地Access库test.mdb
* 12.0对应access数据库的版本的2007
* 4.0对应access数据库的版本的2003
*/
m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=e:\\test.mdb;Persist Security Info=False;",
"","",adModeUnknown);
}
// 数据库路径e:\\test.mdb
catch(_com_error e)
{
cout<<"数据库连接失败,确认数据库test.mdb是否在当前路径下!"<<endl;
return FALSE;
}
//-------------------------------------------------------------------------------------
//建立数据集
//-------------------------------------------------------------------------------------
//声明数据库集合变量
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance(__uuidof(Recordset));
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。
try
{
m_pRecordset->Open("SELECT * FROM test",
//是数据查询字符串(即所谓的SQL语句)
//通常这些语句分为 数据的查询(select),插入(insert)
//更新(update),删除(delect)
//是否能执行这个命令是由前面(1)确定的
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic, //动态光标。所有数据库的操作都会立即在各用户记录集上反应出来
adLockOptimistic,
//乐观锁定方式。只有在你调用Update方法时才锁定记录。在此之
//前仍然可以做数据的更新、插入、删除等动作
adCmdText); //查询出来的数据是显示在控制台里的
}
catch(_com_error *e)
{
//AfxMessageBox(e->ErrorMessage());
cout<<e->ErrorMessage()<<endl;
}
//--------------------------------------------------------------------------------------
//读取数据
//--------------------------------------------------------------------------------------
_variant_t var;
char *strID,*strX,*strY;
try
{
//m_pRecordset->BOF:用来判断是否在第一个记录前;
if(!m_pRecordset->BOF)
{
m_pRecordset->MoveFirst();
}
else
{
cout<<"表内数据为空"<<endl;
return 1;
}
// 读入库中各字段并加入列表框中
//m_pRecordset-> adoEOF:用来判断数据库的指针是否已经移到结果集的末尾了;
while(!m_pRecordset->adoEOF)
{
var = m_pRecordset->GetCollect("ID");
if(var.vt != VT_NULL)
strID= _com_util::ConvertBSTRToString((_bstr_t)var); //_variant_t转字符串
var = m_pRecordset->GetCollect("username");
if(var.vt != VT_NULL)
strX=_com_util::ConvertBSTRToString((_bstr_t)var);
var = m_pRecordset->GetCollect("password");
if(var.vt != VT_NULL)
strY=_com_util::ConvertBSTRToString((_bstr_t)var);
cout<<strID<<" "<<strX<<" "<<strY<<endl;
m_pRecordset->MoveNext();
}
}
catch(_com_error *e)
{
cout<<e->ErrorMessage()<<endl;
}
//--------------------------------------------------------------------------------------
//关闭数据集
//--------------------------------------------------------------------------------------
m_pRecordset->Close();
m_pRecordset = NULL;
//--------------------------------------------------------------------------------------
//关闭数据库连接
//--------------------------------------------------------------------------------------
if(m_pConnection->State){
m_pConnection->Close();
m_pConnection= NULL;
}
return 0;