MFC ADO操作access数据库连接问题

时间:2021-05-06 21:43:04
运行到m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";这句时就报错无效指针,下面是源码:
 

void CEx_ADOView::OnInitialUpdate()
{
CListView::OnInitialUpdate();

try
{
m_pConnection=NULL;
m_pConnection.CreateInstance(__uuidof(Connection));
// CString strCon="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";
m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";
m_pConnection->ConnectionTimeout=30;
HRESULT hr=S_OK;
hr=m_pConnection->Open("","","",adModeUnknown);
//(_bstr_t)strCon
//  if (hr!=S_OK)
//  {
//  AfxMessageBox("Fails!");
//  }
}
catch (_com_error e)
{
AfxMessageBox(e.ErrorMessage());
}

m_pRecordset.CreateInstance(__uuidof(Recordset));
m_pCommand.CreateInstance(__uuidof(Command));

//  if (hr!=S_OK)
//  {
//  ::AfxMessageBox("Link Fails!");
//  }

_RecordsetPtr pRstSchema=NULL;

pRstSchema=m_pConnection->OpenSchema(adSchemaColumns);//inquire columns information

CListCtrl& m_ListCtrl=GetListCtrl();

CString strHeader[3]={"序号","TABLE_NAME","COLUMN_NAME"};

for (int i=0;i<3;i++)
{
m_ListCtrl.InsertColumn(i,strHeader[i],LVCFMT_LEFT,120);
}

int nItem=0;
CString str;
_bstr_t value;

while (!(pRstSchema->adoEOF))
{
str.Format("%d",nItem+1);
m_ListCtrl.InsertItem(nItem,str);

for (int i=1;i<3;i++)
{
value=pRstSchema->Fields->GetItem((_bstr_t)(LPCSTR)strHeader[i])->Value;
m_ListCtrl.SetItemText(nItem,i,value);
}

pRstSchema->MoveNext();
nItem++;
}

pRstSchema->Close();

// TODO: You may populate your ListView with items by directly accessing
//  its list control through a call to GetListCtrl().
}

9 个解决方案

#1


把这个注释掉
m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";

然后把连接字符串写到下面第一个参数里
hr=m_pConnection->Open(连接串,"","",adModeUnknown);

#2


10年前用过,早忘了,来顶一下,哈哈哈

#3


是否初始化 COM?
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="test.accdb";"
试下这个

#4


个人认为使用accdb数据库不如mdb好用

你可以尝试下在无ACSESS2007的机器上运行下  看是否会打不开。。。。

#5


m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb","","",adModeUnknown);//Access2007

#6




::CoInitialize(NULL);//初始化COM环境
_ConnectionPtr con(__uuidof(Connection));
_RecordsetPtr rst(__uuidof(Recordset));
_CommandPtr cmd(__uuidof(Command));
try
{
con->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xxx.accdb","","",adModeUnknown);
rst->Open(_variant_t("select *from xxxx"),_variant_t((IDispatch*) con),adOpenDynamic,adLockOptimistic,-1);
}
catch(_com_error e)
{
AfxMessageBox (L"数据库打开失败!");
return;
}
………………
if(con->State) //释放数据库
con->Close();
con =NULL;

#7


引用 1 楼 yeah2000 的回复:
把这个注释掉
m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";

然后把连接字符串写到下面第一个参数里
hr=m_pConnection->Open(连接串,"","",adModeUnknow……

单步跟踪了下,发现是m_pConnection.CreateInstance(__uuidof(Connection));没有执行成功,而且按照你说的也试了下,但是运行到Open方法时还是报错,所以现在的问题是为啥创建实例会失败,这是系统的函数啊  知道是咋回事部??/

#8


引用 3 楼 rlexyum 的回复:
是否初始化 COM?
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="test.accdb";"
试下这个


谢谢了  是初始化位置写错了,应该写在开头的 结果写在中间了 

#9


哈哈  我是871121

引用 8 楼 sanhu871123 的回复:
引用 3 楼 rlexyum 的回复:
是否初始化 COM?
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="test.accdb";"
试下这个


谢谢了  是初始化位置写错了,应该写在开头的 结果写在中间了

#1


把这个注释掉
m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";

然后把连接字符串写到下面第一个参数里
hr=m_pConnection->Open(连接串,"","",adModeUnknown);

#2


10年前用过,早忘了,来顶一下,哈哈哈

#3


是否初始化 COM?
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="test.accdb";"
试下这个

#4


个人认为使用accdb数据库不如mdb好用

你可以尝试下在无ACSESS2007的机器上运行下  看是否会打不开。。。。

#5


m_pConnection->Open("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb","","",adModeUnknown);//Access2007

#6




::CoInitialize(NULL);//初始化COM环境
_ConnectionPtr con(__uuidof(Connection));
_RecordsetPtr rst(__uuidof(Recordset));
_CommandPtr cmd(__uuidof(Command));
try
{
con->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xxx.accdb","","",adModeUnknown);
rst->Open(_variant_t("select *from xxxx"),_variant_t((IDispatch*) con),adOpenDynamic,adLockOptimistic,-1);
}
catch(_com_error e)
{
AfxMessageBox (L"数据库打开失败!");
return;
}
………………
if(con->State) //释放数据库
con->Close();
con =NULL;

#7


引用 1 楼 yeah2000 的回复:
把这个注释掉
m_pConnection->ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;Data Source=Student.accdb;";

然后把连接字符串写到下面第一个参数里
hr=m_pConnection->Open(连接串,"","",adModeUnknow……

单步跟踪了下,发现是m_pConnection.CreateInstance(__uuidof(Connection));没有执行成功,而且按照你说的也试了下,但是运行到Open方法时还是报错,所以现在的问题是为啥创建实例会失败,这是系统的函数啊  知道是咋回事部??/

#8


引用 3 楼 rlexyum 的回复:
是否初始化 COM?
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="test.accdb";"
试下这个


谢谢了  是初始化位置写错了,应该写在开头的 结果写在中间了 

#9


哈哈  我是871121

引用 8 楼 sanhu871123 的回复:
引用 3 楼 rlexyum 的回复:
是否初始化 COM?
"Provider=Microsoft.ACE.OLEDB.12.0; Data Source="test.accdb";"
试下这个


谢谢了  是初始化位置写错了,应该写在开头的 结果写在中间了