当前位置:Gxlcms > mysql > ado操作access

ado操作access

时间:2021-07-01 10:21:17 帮助过:66人阅读

有一个1.mdb的数据库,有一个zy的表,如何用ado操作。 先封转一个类,如下: .h class CDataSource {public:void FreeData();void InitData();CDataSource();virtual ~CDataSource();_RecordsetPtr m_pRecordset;_ConnectionPtr m_pConn;}; .cpp CDataSource

有一个1.mdb的数据库,有一个zy的表,如何用ado操作。

先封转一个类,如下:

.h

class CDataSource  
{
public:
	void FreeData();
	void InitData();
	CDataSource();
	virtual ~CDataSource();
	_RecordsetPtr m_pRecordset;
	_ConnectionPtr m_pConn;
};

.cpp

CDataSource::CDataSource()
{

}

CDataSource::~CDataSource()
{
FreeData();
}

void CDataSource::InitData()
{
	::CoInitialize(NULL);
      m_pConn.CreateInstance("ADODB.Connection");
    //初始化记录集对象
	m_pRecordset.CreateInstance("ADODB.Recordset");
	  try
	{
		   m_pConn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:1.mdb", "", "", adModeUnknown);
     // m_pConn->ConnectionString="provider=SQLOLEDB.1;uid=sa;pwd=;Database=ff";
      // m_pConn->Open("", "", "", adModeUnknown);
	  m_pRecordset->Open(  "select * from zy", _variant_t((IDispatch *) m_pConn, true),adOpenStatic, adLockReadOnly, adCmdText); 
	  }
	catch(_com_error &e)
	{
		::AfxMessageBox(e.ErrorMessage());
	}
}


void CDataSource::FreeData()
{
    if (m_pConn)
	{
		m_pConn->Close();
		m_pRecordset.Release();
		m_pConn.Release();
		CoUninitialize();
	}
}


在主对话框里创建一个对象CDataSource m_ds;,在对话框起来的时候m_ds.InitData();

添加:

m_ds.m_pConn->Execute((_bstr_t)“查询语句”,NULL,adCmdText);
m_ds.m_pRecordset->Requery(adCmdUnknown);
删除:

m_ds.m_pConn->Execute((_bstr_t)“删除语句”,NULL,adCmdText);
m_ds.m_pRecordset->Requery(adCmdUnknown);

修改:

m_ds.m_pConn->Execute((_bstr_t)“修改语句”,NULL,adCmdText);
m_ds.m_pRecordset->Requery(adCmdUnknown);

遍历:

有个链表CListCtrl m_list;

while (!m_ds.m_pRecordset->EndOfFile)
{
m_list.InsertItem(i,"sdf");
m_list.SetItemText(i,0,(LPCTSTR)(_bstr_t)m_ds.m_pRecordset->GetCollect("id"));
m_list.SetItemText(i,1,(LPCTSTR)(_bstr_t)m_ds.m_pRecordset->GetCollect("name"));
m_list.SetItemText(i++,2,(LPCTSTR)(_bstr_t)m_ds.m_pRecordset->GetCollect("hobby"));
m_ds.m_pRecordset->MoveNext();
}
m_ds.m_pRecordset->MoveFirst();

问题:

Win7上编译的ADO程序无法在低版本系统上运行,创建ADO时提示错误:0x80004002

如下语句:
hr1 = m_pConnection.CreateInstance(__uuidof(Connection));
解决办法:
下载msado60_Backcompat.tlb,地址见:http://download.csdn.net/detail/evsqiezi/8244221,相关代码导入修改为:
#import "msado60_Backcompat.tlb" no_namespace rename("EOF","EndOfFile")

人气教程排行